Home Java Example Java Io Unzip File Using Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Unzip File Using Java

Advertisement
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[])
  {
  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();
 }
 
 }

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 19, 2007

Ask Questions?    Discuss: Unzip File Using Java   View All Comments

Post your Comment


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

Hi, what is a[0] here?
jaymala
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.
DMCA.com