Thursday, December 20, 2007

Unzipping using java.util.zip Package

How to unzip using java.util.zip 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.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

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))
dir += 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);
countEntry++;
}
} 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())
f.getParentFile().mkdirs();
OutputStream out = new FileOutputStream(absoluteFileName);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Close the streams
out.close();
}

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

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

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

To run:

java Unzip a.zip /tmp/a

a.zip content will be extracted to /tmp/a folder.

Reactions:

6 comments:

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

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....

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

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
Donald

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

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 blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More