Thursday, January 6, 2011

How to extract password protected zip file using java program

After the without password zip file extraction process, I start hunting password protected zip file extraction process. After Two days research I found out a web site, which is available free and open source jar file .From that jar file we can extract a password protected zip file. That jar file is zip4j.jar file .you can find out this jar file and it’s information in the following URL.

http://lingala.net/zip4j/

I have downloaded the zip4j.jar file
And I have implemented the following source code
=================================================



import java.io.IOException;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.Zip4jInputStream;

/**
*
* @author Administrator
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
try{
ZipFile zipFile = new ZipFile("c:\\zip\\29111001.124.zip");
// Check to see if the zip file is password protected
if (zipFile.isEncrypted()) {
// if yes, then set the password for the zip file
zipFile.setPassword("abcd");
}
zipFile.extractFile("29111001.124.txt", "c:\\zip\\12\\");

}catch(ZipException ex){
System.out.println("error "+ ex );
}
}

}
=======================================================
In this program
zip file is available at c:\\zip\\29111001.124.zip
and extracted file is available at c:\\zip\\12\\ folder
Here "abcd" is the password used to extract zip file.

Note :- i have write down this source code for benefit of all the java programmer.

4 comments:

Unknown said...

thanks, really helped me

adrian said...

Thanks for nice and easy code.

Unknown said...

sir above code is not working for me.I am not able to extract the password protected zip file using zip4j

Joel Mathews said...

Thanks it helped me, but how to handle the exception when user specifies wrong password for the file.Ex: I zipped the file with passwd abcd and try to extract it with passed 1234, it gives zip exception wrong password for the file how to handle it??