How to create Uncompressed Zip archive in Java

前端 未结 3 1894
心在旅途
心在旅途 2020-12-30 00:55

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?

相关标签:
3条回答
  • 2020-12-30 01:18

    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.

    0 讨论(0)
  • 2020-12-30 01:29

    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.

    0 讨论(0)
  • 2020-12-30 01:29

    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);
    
    0 讨论(0)
提交回复
热议问题