How to create Uncompressed Zip archive in Java

Keya picture Keya · Jul 30, 2009 · Viewed 17.5k times · Source

I am using the Zip utility package of Java and wanted to know how to create a zip file with no compression at all. Setting the level to 0 doesn't help. Is this right?

Also, when I used the STORED method, it throws following exception:

java.util.zip.ZipException: STORED entry missing size, compressed size, or crc-32

I can set the size but now following exception is thrown:

java.util.zip.ZipException: invalid entry crc-32 

I am just following all the examples available by searching on the web and I am not able to really understand it properly I guess. It would be great if someone can help me on this and provide me suggestion to correct the problem I might be doing.

Answer

PMorganCA picture PMorganCA · Nov 30, 2012

I'm leery of aperkins solution (since deleted), but I know why it worked. The line (which has since been corrected in his answer)

zipOut.setLevel(ZipOutputStream.STORED); // accidentally right

was using the static value ZipOutputStream.STORED, which coincidentally equals 0. So what that line is doing is setting the level used by the default DEFLATED method to zero compression (this is obviously what you want to do, but happened to only work by luck). So to get what you want explicitly and safely, use this instead:

zipOut.setMethod(ZipOutputStream.DEFLATED); // this line optional
zipOut.setLevel(0);

or

zipOut.setLevel(Deflater.NO_COMPRESSION);

If you use

zipOut.setMethod(ZipOutputStream.STORED);
zipOut.setLevel(Deflater.NO_COMPRESSION);

you'll probably get the Exception that Keya noted in the original question. I believe Christian Schlichtherle is right; you are getting the Exceptions because you are not setting the CRC in the entry. The repercussions of that is that to use the STORED method, you have to read the entire entry file first, or find some other way to set the size, compressed size (must be equal) and the CRC before you call zipOut.putNextEntry(). Otherwise, you'll run into more exceptions if you overrun the size attribute by writing too many bytes to the output stream. It appears that the ZIP specs say that if you are writing STORED data then it has to write the header [which includes the CRC-32 and length] "up front" before the data itself, hence the java API requiring these be set before it can start, since it basically only supports streaming out to the final zip file.