Unzip File Using Java

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

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