3

I want to read data from lets say 4 zip files called zip1, zip2, zip3, zip4. All of these zip files are split from this 1 big zip file called "BigZip". I want to combine the zip files into one and then compare the bytes if the 1 bigzip file matches the size of bytes with the combined zip file of (zip1+zip2+zip3+zip4). I am getting a very small file size when I combine the size of 4 zip files. What am I doing wrong?

Here is my code for the same:

targetFilePath1, targetFilePath2, targetFilePath3, targetFilePath4 belongs to path of 4 zip files. sourceFilePath is the path to BigZip file

class Test {


public static void main(String args[]) {

ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceBigZip));

            readZip(sourceFilePath, targetFilePath1);
            readZip(sourceFilePath, targetFilePath2);
            readZip(sourceFilePath, targetFilePath3);
            readZip(sourceFilePath, targetFilePath4);
  outStream.close();
}

static void readZip(String sourceBigZip, String targetFile) throws Exception {
    ZipInputStream inStream = new ZipInputStream(new FileInputStream(targetFile));
    byte[] buffer = new byte[1024];
    int len = inStream.read(buffer);
    while (len != -1) {
        outStream.write(buffer, 0, len);
        len = inStream.read(buffer);
        System.out.print(len);
    }

    inStream.close();
}
}
fscore
  • 2,567
  • 7
  • 40
  • 74

2 Answers2

3

Create ZipOutputStream once and pass it to readZip() method, like:

public static void main(String args[]) {
    ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceFilePath));
    readZip(outStream , targetFilePath1);
    readZip(outStream , targetFilePath2);
    readZip(outStream , targetFilePath3);
    readZip(outStream , targetFilePath4);
}

Then you have an error dealing with copying the data from one zip to another... You need to copy each file in the zip file like this:

static void readZip(ZipOutputStream outStream, String targetFile)
        throws Exception {
    ZipInputStream inStream = new ZipInputStream(new FileInputStream(
            targetFile));
    byte[] buffer = new byte[1024];
    int len = 0;

    for (ZipEntry e; (e = inStream.getNextEntry()) != null;) {
        outStream.putNextEntry(e);
        while ((len = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, len);
        }
    }
    inStream.close();
}

}

Thanador
  • 31
  • 3
-1

Every time you call new ZipOutputStream, it creates a new empty file, and wipes out everything you have written to it before. You have to create the stream outside of readZip, and pass it in to each call rather than creating a new stream every time.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • you mean something like my edited code now? please check my edit? – fscore Dec 05 '14 at 18:26
  • almost ... except, `outStream` is undefined in `readZip`. You have to pass it in as a parameter instead of `sourceBigZip`, which is now unused. Also, your naming of parameters seems rather confusing: source should be where you read from, and target is where you write, shouldn't them? – Dima Dec 05 '14 at 18:31
  • Note that zip is a structured archive, you can't read and write it simply as a stream. Checkout the docs for `ZipInputStream` and `ZipOutputStream`. The archive consists of entries, so, before reading the stream, you have to position it at the actual entry (with `stream.getNextEntry()`). There can be multiple entries in the same archive, and it will return eof (-1) at the end of each of them. That is what happens in your example, because you are not positioning it right, you get a -1 right away and quit. When writing the output, you also need to do a `.putNewEntry` before starting to write. – Dima Dec 05 '14 at 19:27