3

I am currently working on a program that, at one point, has to merge two .zip files (originalZip and newZip should be merged to moddedZip, newZip overwriting files if they allready exist). Since I did not working with .zip files in any of my previous projects I googled for a while, until I found this. Although it was just about appending single files, I thought I migh be able to use it to merge files.

This is what I have right now:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ModZip {   
    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    // copy input to output stream   
    public static void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public static void patch(String path) throws Exception {
        // read the original zip
        ZipFile originalZip = new ZipFile(path);
        
        
        // write the modded zip with new Name
        ZipOutputStream moddedZip = new ZipOutputStream(new FileOutputStream(path.substring(0, (path.length()-4))+"-modded.zip"));

        // copy contents from original zip to the modded zip
        Enumeration<? extends ZipEntry> entries = originalZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            System.out.println("copy: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(originalZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }

        // replace the original zip-files with new ones       
        ZipFile newZip = new ZipFile("/Users/user/Desktop/NEW.zip");
        Enumeration<? extends ZipEntry> newentries = newZip.entries();
        System.out.println(newentries);
        while (newentries.hasMoreElements()) {
            ZipEntry e = newentries.nextElement();
            System.out.println("append: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(newZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }
        
        System.out.println("appending done ");

        // close
        originalZip.close();
        newZip.close();
        moddedZip.close();
        System.out.println("all done");
    }
}

Copying originalZip into moddedZip works fine, however after appending the first entry, that is a file (not a directory), of newZip, the program seems to crash. I do not get any exeption, it just does not seem to go on.(forgot to print the stacktrace), it stop because it refuses to overwrite existing files.

This is the syso:

copy: test.txt

putnextEntry done

copy: __MACOSX/

putnextEntry done

copy: __MACOSX/._test.txt

putnextEntry done

java.util.zip.ZipFile$1@1fa83e7e

append: item/

putnextEntry done

append: item/.DS_Store

putnextEntry done

append: __MACOSX/

Regards DNSYeti

EDIT: Thanks to jlordo I was able to solve the problem myself, here is the new code:

When copying the originial .zip file, it checks if any of the elements it contains also are in the new .zip file. If they are, they wont be copied into the modded .zip file (so I do not have to delete them again when overwriting them).

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ModZip {   
    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    // copy input to output stream   
    public static void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public static void patch(String path) throws Exception {
        // read the zips
        ZipFile originalZip = new ZipFile(path);
        ZipFile newZip = new ZipFile("/Users/user/Desktop/NEW.zip");


        // write the modded zip with new Name
        ZipOutputStream moddedZip = new ZipOutputStream(new FileOutputStream(path.substring(0, (path.length()-4))+"-modded.zip"));

        // copy contents from original zip to the modded zip
        Enumeration<? extends ZipEntry> entries = originalZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            
            String name = e.getName();
            if(newZip.getEntry(name) == null) {
                System.out.println("copy: " + e.getName());
                moddedZip.putNextEntry(e);
                System.out.println("putnextEntry done");
                if (!e.isDirectory()) {
                    copy(originalZip.getInputStream(e), moddedZip);
                }
                moddedZip.closeEntry();
            }
        }

        // replace the original zip-files with new ones       
        
        Enumeration<? extends ZipEntry> newentries = newZip.entries();
        System.out.println(newentries);
        while (newentries.hasMoreElements()) {
            ZipEntry e = newentries.nextElement();
            System.out.println("append: " + e.getName());
            moddedZip.putNextEntry(e);
            System.out.println("putnextEntry done");
            if (!e.isDirectory()) {
                copy(newZip.getInputStream(e), moddedZip);
            }
            moddedZip.closeEntry();
        }

        System.out.println("appending done ");

        // close
        originalZip.close();
        newZip.close();
        moddedZip.close();
        System.out.println("all done");
    }
}
Community
  • 1
  • 1
DNSYeti
  • 31
  • 1
  • 3

0 Answers0