1

I want to return boolean to check if my zip file is compressed . Will be an advantage if i can also get what Compression method has been used. For now I am just checking is it is Encrypted and is valid zip. Please help if it is possible using zip4j library.

public static boolean isPackageCompressed(String path) throws ZipException{
    boolean isPackageCompressed = false;

    ZipFile zipFile = new ZipFile(path);

    System.out.println(zipFile.isEncrypted());       
    System.out.println(zipFile.isValidZipFile());        

    // TODO. There is no method like zipFile.getCompressionMethod() . 

    return isPackageCompressed; 
}

public static void main(String[] args) {
    try {
        isPackageCompressed("D:\\some.ZIP");
    } catch (ZipException e) {
        e.printStackTrace();
    }
}
Tushar Khanna
  • 428
  • 6
  • 22
  • 1
    I'm not familiar with zip4j, but with the ZIP format. In ZIPs the compression method is a property of each entry, it is perfectly valid to mix compression methods in a single archive and you'll encounter a mix of STORED (not compressed at all) and DEFLATEd entries quite often. You will probably need to iterate over the entries and see whether those are compressed. – Stefan Bodewig Feb 10 '15 at 09:29
  • @Stefan Bodewig : Can you please help by sharing some code? – Tushar Khanna Feb 10 '15 at 11:40
  • 1
    Using Apache Commons Compress, I could. :-) Like I said, I'm not familiar with zip4j. – Stefan Bodewig Feb 10 '15 at 11:42
  • @Stefan Bodewig : I couldn't find here any method in Apache Commons also. http://commons.apache.org/proper/commons-compress/apidocs/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.html – Tushar Khanna Feb 10 '15 at 12:06
  • @Stefan Bodewig : I got it from http://www.java-examples.com/get-compression-method-zip-entry-example , Thanks for the idea. :) – Tushar Khanna Feb 10 '15 at 12:09

1 Answers1

1

Check this out may be helpful (Not the complete answer )

ZipParameters zp = new ZipParameters();
zp.setFileNameInZip("sample.zip");
System.out.println(zp.getCompressionMethod());
System.out.println(Zip4jConstants.COMP_DEFLATE);

OutPut:
8
8
  • Thanks But I don't have any requirement for opening Zip ; just i want to check with what compression method zip file is compressed. – Tushar Khanna Feb 10 '15 at 08:15
  • Thanks :) Just for Reference for others : Compression method : 0 - The file is stored (no compression) 1 - The file is Shrunk 2 - The file is Reduced with compression factor 1 3 - The file is Reduced with compression factor 2 4 - The file is Reduced with compression factor 3 5 - The file is Reduced with compression factor 4 6 - The file is Imploded 7 - Reserved for Tokenizing compression algorithm 8 - The file is Deflated – Tushar Khanna Feb 10 '15 at 14:54