Home Tutorial Java Corejava Zip How do Unzip a directory and all its contents using ZipEntry class in java.

 
 

Share on Google+Share on Google+
How do Unzip a directory and all its contents using ZipEntry class in java.
Posted on: July 20, 2010 at 12:00 AM
Advertisement
In this tutorial you will see how do Unzip a directory and all its contents using ZipEntry class in java.

How do Unzip a directory and all its contents using ZipEntry class in java.

In this Example, We will discuss the use isDirectory method of ZipEntry class. The
  ZipEntry class is used show the single entry in ZIP file. The ZipEntery class provides
 methods for retrieving informationrelated to entry. The ZipEntry class is available in
java.util.zip package. The isDirectory method of ZipEntry class returns Boolean
value  either true or false, if given file is directory then returns true otherwise returns false.

In this Example, We will discuss about the isDirectory method and how to extract zip file. 
The ZipFele class create an object of given zip file and read entries. The Enumeration class
 create an object . The entries() method of ZipFile class returns enumeration of the zip file. 
The hasMoreElements() of method of Enumeration class checks availability of entry. The
  nextElements() of Enumeration class returns next elements of enumeration.  The isDirectory
method of ZipEntry class check given file is directory or not.The getName method of ZipEntry  
class return the name of associated entry. The mkdir() method of File class creates directory 
of specific name. 

About ZIPEntry API:

Return Type Method Description 
Boolean  isDirectory() The isDirectory() Method returns true if zip file is directory otherwise false.
String getName() The method getName() returns name of the associated entry.
void write(byte buffer, int offset, int length) The write(..) method writes an array of bytes onto the associated stream.

code

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ZipDirectory {
  public static void extractFile(InputStream inStream,
 OutputStream outStream
)
  throws IOException {
    byte[] buf = new byte[1024];
    int l;
    while ((l = inStream.read(buf)) >= 0) {
      outStream.write(buf, 0, l);
    }
    inStream.close();
    outStream.close();
  }
  public static void main(String[] args) {
    Enumeration enumEntries;
    ZipFile zip;

    if (args.length != 1) {
      System.out.println("There is no zip file :");
      return;
    }
    try {
      zip = new ZipFile(args[0]);
      enumEntries = zip.entries();
      while (enumEntries.hasMoreElements()) {
     ZipEntry zipentry = (ZipEntryenumEntries.nextElement();
        if (zipentry.isDirectory()) {
          System.out.println("Name of Extract directory : "
              + zipentry.getName());
          (new File(zipentry.getName())).mkdir();
          continue;
        }
        System.out.println("Name of Extract fille : "
            + zipentry.getName());
extractFile(zip.getInputStream(zipentry)new FileOutputStream(
            zipentry.getName()));
      }
      zip.close();
    catch (IOException ioe) {

    System.out.println("There is an IoException Occured :" + ioe);
      ioe.printStackTrace();
      return;
    }
  }

}

Following is the output if you run the application:

C:\>java ZipDirectory C:\Work\Bharat\today\directory\file.zip
Name of Extract directory : data/
Name of Extract directory : data/New Folder/
Name of Extract fille : data/New Folder/url.txt
Name of Extract directory : design/
Name of Extract fille : design/dark_pixel.gif
Name of Extract fille : design/donate.gif

Download this code

Advertisement

Related Tags for How do Unzip a directory and all its contents using ZipEntry class in java.:


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: July 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com