Unzip File Using Java

Ads
 

Unzip File Using Java

Here you will learn how to unzip a 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[])
  {
  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.

Ads

Ads