Thursday, December 20, 2007

Unzipping using Package

How to unzip using Package ?
How to unzip using JAVA API ?.

Create ZipInputStream using any of the InputStream and read one by one ZIPEntry. Make if check ZIPEntry points to file object and the read bytes from ZipInputStream upto -1 byte found.


public class Unzip {

private static final String FILESEPARATOR = File.separator;

public static void storeZipStream(InputStream inputStream, 
String dir)
throws IOException {

ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry entry = null;
int countEntry = 0;
if (!dir.endsWith(FILESEPARATOR))

// check inputStream is ZIP or not
if ((entry = zis.getNextEntry()) != null) {
do {
String entryName = entry.getName();
// Directory Entry should end with FileSeparator
if (!entry.isDirectory()) {
// Directory will be created while creating file with in it.
String fileName = dir + entryName;
createFile(zis, fileName);
} while ((entry = zis.getNextEntry()) != null);
System.out.println("No of files Extracted : " + countEntry);

} else {
throw new IOException("Given file is not a Compressed one");

public static void createFile(InputStream is, 
String absoluteFileName)
throws IOException {
File f = new File(absoluteFileName);

if (!f.getParentFile().exists())
OutputStream out = new FileOutputStream(absoluteFileName);
byte[] buf = new byte[1024];
int len = 0;
while ((len = > 0) {
out.write(buf, 0, len);
// Close the streams

public static void main(String args[]) throws Exception {

if (args.length < 1) {
System.out.println("Syntax : Unzip zipfile [extractlocation]");

FileInputStream zis = new FileInputStream(new File(args[0]));
String dir = System.getProperty("");
if (args.length == 2) {
dir = args[1].replace('\\', '/');
System.out.println("Extracted to "+dir);
storeZipStream(zis, dir);

To run:

java Unzip /tmp/a content will be extracted to /tmp/a folder.


zamkinos said...

Thanks for your example.

santosh said...

thanks you,

haniffa said...

thank you very much.
your code is very much helpful to me and thanks a lot.

Anonymous said...

Wow this is really amazing....i was struggling to unzip when folders were present directly inside the zip file.....

this is awesome code...thanks a lot pal....

JSmithLA said...

worked of the few useful java zip extraction snippets on the web!

Donald Ferreira said...

Hi Krishna,

I came across your java unzip code. It is really useful,and thats exactly what I was looking for. I am completely new to Java.

In addition to the unzip code, I am looking for the following java code. If you could please be kind to help me out. Below are the listing of class with passing parameters:

1. Check if file exists(p_directory, p_filename)
2.Unzip File(p_directory, p_filename)
3. Delete File(p_directory, p_filename)
4. Rename File(p_directory, p_filename)
5. Rename and Move File(p_directory_from, p_directory_to, p_filename_to_move)

Thanks in advance

Kind regards

Post a Comment

Recent Posts

Unix Commands | List all My Posts


This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer