How to create Uncompressed Zip archive in Java

2020-02-24 13:13发布

问题:

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.

回答1:

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.



回答2:

You need to use the STORED method, but this requires that you set the size, compressedSize and crc32 properties of the corresponding ZipEntry before you can call putNextEntry on the ZipOutputStream. You can precompute the CRC-32 by using a Crc32OutputStream.



回答3:

FYI:

In JDK Source of the method [java.util.zip.ZipOutputStream.setLevel(int)]:

public void setLevel(int level) {
    def.setLevel(level);
}

It simply redirect the compression level setting to the field variable [def], which is an instance of [java.util.zip.Deflater].

And in the source code of the class [java.util.zip.Deflater]:

/**
 * Compression level for no compression.
 */
public static final int NO_COMPRESSION = 0;

/**
 * Compression level for fastest compression.
 */
public static final int BEST_SPEED = 1;

/**
 * Compression level for best compression.
 */
public static final int BEST_COMPRESSION = 9;

/**
 * Default compression level.
 */
public static final int DEFAULT_COMPRESSION = -1;

So, I think it will be more readable if you use the constant value [Deflater.NO_COMPRESSION]:

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


标签: java zip crc32