Saturday 21 July 2012

Unzip File Using ZipInputStream in Java


Lets learn how to unzip a file using java.The java.util.zip package provides classes for data compression and decompression. Decompressing a ZIP file is a matter of reading data from an input stream. The java.util.zip package provides a ZipInputStream class for reading ZIP files. A ZipInputStream can be created just like any other input stream. For example, the following segment of code can be used to create an input stream for reading data from a ZIP file format:

FileInputStream fis = new FileInputStream("figs.zip");
ZipInputStream zin = new 
  ZipInputStream(new BufferedInputStream(fis));


Once a ZIP input stream is opened, you can read the zip entries using the getNextEntry method which returns a ZipEntry object. If the end-of-file is reached, getNextEntry returns null:


ZipEntry entry;
while((entry = zin.getNextEntry()) != null) {
   // extract data
   // open output streams
}


Now, it is time to set up a decompressed output stream, which can be done as follows:


int BUFFER = 2048;
FileOutputStream fos = new 
  FileOutputStream("dest dirPath"+entry.getName());
BufferedOutputStream dest = new 
  BufferedOutputStream(fos, BUFFER);


In this segment of code, a file output stream is created using the entry's name, which can be retrieved using the entry.getName method. Source zipped data is then read and written to the decompressed stream:


while ((count = zin.read(data, 0, BUFFER)) != -1) {
   //System.out.write(x);
   dest.write(data, 0, count);
}
dest.close();


And finally, close the input stream:


zin.close();


The following source code shows how to decompress and extract files from a ZIP archive. 

import java.io.*;
import java.util.zip.*;

public class UnZip {
 final static int BUFFER = 2048;

 /**
  * Command line arguments :
  * argv[0]-----> Source zip file.
  * argv[1]-----> Destination directory.
  **/
 public static void main(String argv[]) {
  try {

   /** create a ZipInputStream object. **/

   FileInputStream fis = new FileInputStream(argv[0]);
   ZipInputStream zis = new ZipInputStream(
     new BufferedInputStream(fis));

   ZipEntry entry;

   /** Read the zip entries using the getNextEntry method **/

   while ((entry = zis.getNextEntry()) != null) {

    System.out.println("Extracting: " + entry.getName());

    /** If the entry is a directory, create the directory. **/

    if (entry.isDirectory()) {

     File f = new File(argv[1] + entry.getName());
     f.mkdirs();
    }
    /**
     * If the entry is a file,write the decompressed file to the
     * disk and close destination stream.
     **/
    else {
     int count;
     byte data[] = new byte[BUFFER];

     FileOutputStream fos = new FileOutputStream(argv[1]
       + entry.getName());
     BufferedOutputStream dest = new BufferedOutputStream(fos,
       BUFFER);
     while ((count = zis.read(data, 0, BUFFER)) != -1) {
      dest.write(data, 0, count);
     }
     dest.close();
    }
   }

   /** close the input stream **/

   zis.close();
   System.out.println("unzip completed successfully!!");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


Unzip File Using ZipFile in Java

The unzip can also be performed using java.util.zip.ZipFile class.Please visit Unzip File Using ZipFile in Java  to learn more about it.

No comments:

Post a Comment