Search code examples
javacompressioncodec7ziprecordreader

Java Code to Open a password protected zip file which is opening only with 7zx and keka in mac OS


I have a password protected zip file which is opening only with 7zx and keka in mac. I have to write the code in java to decompress password protected zip file and then do some operation on it. I have tried using sevenz api in apache.commons.compress but I am not able to compress it getting exception, bad 7z signature.Is there any api support for decompressing the zip files?

unzip -P test@123 abcd.zip
Archive:  abcd.zip
   skipping: abcd.txt  need PK compat. v5.1 (can do v2.1)

Solution

  • As far as i remember there is a library namely zip4j, check out this link

    And try this code:

    //zip password
    String pass="abc";
    try {
        ZipFile zipFile = new ZipFile("dir/xyz.zip");
          if (zipFile.isEncrypted()) {
             zipFile.setPassword(pass);
        }
    //extract somewhere in directory
    zipFile.extractAll("dir/abc");
    } 
     catch (ZipException e) {
     e.printStackTrace();
    }