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.

2 comments:

hengky said...

thanks, really helped me

adrian said...

Thanks for nice and easy code.