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

2.7k views Asked by At

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)
1

There are 1 answers

3
Tahir Hussain Mir On BEST ANSWER

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();
}