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.

How to extract a zip file (without password protected) using java Program

I have assigned a job, which has a module to read a password protected zip file using java program. At the beginning I was able to write a java using ZipInputStream class.( java.util.zip package).In that program I can extract zip file ,which does not have password protection. But when it comes to password protected zip file extraction, java.util.zip package does not support. For the zip file without password it works very well.

Here is the sample code i have use for that

==============================================================
import java.io.*;
import java.util.zip.*;
public class UnzipExample
{
public static void main(String a[])
{
try
{
BufferedOutputStream out = null;
ZipInputStream in = new ZipInputStream
(new BufferedInputStream(new FileInputStream(a[0])));
ZipEntry entry;
while((entry = in.getNextEntry()) != null)
{
int count;
byte data[] = new byte[1000];
out = new BufferedOutputStream(new
FileOutputStream("out.txt"),1000);
while ((count = in.read(data,0,1000)) != -1)
{
out.write(data,0,count);
}
out.flush();
out.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}

===========================================================

According to this source code we have to pass the zip file name ,when it run the program (run time).

===================================
java UnzipExample 11.zip
===================================

In this program, "out.txt" is the output file