Saturday 21 July 2012

Unzip File Using ZipFile in Java

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


Difference between ZIPInputStream and ZipFile


The ZipInputStream class reads ZIP files sequentially. The class ZipFile, however, reads the contents of a ZIP file using a random access file internally so that the entries of the ZIP file do not have to be read sequentially.


Another fundamental difference between ZIPInputStream and ZipFile is in terms of caching. Zip entries are not cached when the file is read using a combination ofZipInputStream and FileInputStream. However, if the file is opened using ZipFile(fileName) then it is cached internally, so if ZipFile(fileName) is called again the file is opened only once. The cached value is used on the second open and therefore the performance of ZipFile is superior to ZipInputStream. If the contents of the same zip file, however, are be to frequently changed and reloaded during program execution, then using ZipInputStream is preferred.

This is how a ZIP file can be decompressed using the ZipFile class:

Step: 1

Create a ZipFile object by specifying the ZIP file to be read either as a String filename or as a File object: 

ZipFile zipfile = new ZipFile("figs.zip");

Step: 2

Use the entries method, returns an Enumeration object, to loop through all the ZipEntry objects of the file:

Enumeration e = zipfile.entries();
while(e.hasMoreElements()) {
   entry = (ZipEntry) e.nextElement();
   // read contents and save them
}

Step: 3

Read the contents of a specific ZipEntry within the ZIP file by passing the ZipEntry to getInputStream, which will return an InputStream object from which you can read the entry's contents:

is = new 
  BufferedInputStream(zipfile.getInputStream(entry));

Step: 4

Retrieve the entry's filename and create an output stream to save it:

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

Step: 5

Finally, close all input and output streams:

is.close();
dest.close();


The complete source program is shown in given below:


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

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 ZipFile object. **/

   ZipFile zipfile = new ZipFile(argv[0]);

   ZipEntry entry;

   Enumeration e = zipfile.entries();

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

   while (e.hasMoreElements()) {

    entry = (ZipEntry) e.nextElement();

    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.
     **/
    else {
     int count;
     byte data[] = new byte[BUFFER];
     BufferedInputStream zis = new BufferedInputStream(
       zipfile.getInputStream(entry));

     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);
     }

     /** close the input & output streams **/

     dest.close();
     zis.close();
    }
   }

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

No comments:

Post a Comment