Thursday, January 6, 2011

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

No comments: