How to create Uncompressed Zip archive in Java

前端 未结 3 1915
心在旅途
心在旅途 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: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);
    

提交回复
热议问题