Unzip File Using Java

Here you will learn how to unzip a file using java.

Unzipping a File


Lets learn how to unzip a file using java.

The java.util.zip.ZipEntry class is used to represent a zip file entry. The java.util.zip.ZipEntry class implements java.util.zip.ZipConstants interface. Here we passing the zipped file name to be unzipped using command line argument .To unzip a file first create an object of byte stream then take the array of any size as per your convenience (Here we are taking 1000 as the size of byte array ) then create a text file. First convert the zipped file into the byte array and then write the zipped file into it, To flush and close the file call  flush() and close() respectively.
If it throws any exception then catch it in try/catch block. We are printing the whole exception using printStrackTrace() method.

Here is the code of the program :

import java.io.*;
import java.util.zip.*;
public class UnzipExample
public static void main(String a[])
 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 
 while ((count = in.read(data,0,1000)) != -1)
 }catch(Exception e)

Download this example.

Share on Google+Share on Google+

Unzip File Using Java

Posted on: June 19, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Unzip File Using Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 11, 2012
Error in the code

Hi, what is a[0] here?
March 12, 2012
How to unzip password protected ZIP File

If Zip file is password protected then, the above mentioned steps are not working. Please guide, to unzip the password protected ZIP file.