Home Tutorial Java Corejava Zip Use of size and getInputStream method of ZipFile.

 
 

Share on Google+Share on Google+
Use of size and getInputStream method of ZipFile.
Posted on: July 21, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of size and getInputStream method of ZipFile.

Use of size and getInputStream method of ZipFile.

In this tutorial, We will discuss the use of size and getInputStream method. These methods 
are available in ZipFile class. The ZipFile class is used to read entries from zip files. 

In this Example, The size() method of ZipFile class returns number of entries in zip file, and 
the getInputStream method returns a input stream for reading the contents of zip file entry. 
The getName method of ZipEntry class return the name of associated entry. The
 hasMoreElements method of Enumeration class checks enumeration  has
 more elements, and nextElements returns next elements.

About ZIPEntry API:

Return Type Method Description 
int size() The method size() returns the number of entry in a associated zip file.
InputStream geInputStream() The method getInputStream() returns a input stream for reading the contents of associated zip file entry.

code

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

public class ZipGetInStream {
  public static final void main(String[] args) {
    try {
      ZipFile zipFileName = new ZipFile("test.zip");
      Enumeration enumEntries = zipFileName.entries();
  System.out.println("Total number of entries in a zip file :"
          + zipFileName.size());
      while (enumEntries.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntryenumEntries.nextElement();
        System.out.println("Name of Extract file :"
            + zipEntry.getName());
  FileOutputStream fout = new FileOutputStream(zipEntry.getName());
        doInStream(zipFileName.getInputStream(zipEntry), fout);
      }
      zipFileName.close();
    catch (IOException ioe) {
      System.out.println("IOException :");
      ioe.printStackTrace();
      return;
    }
  }

  public static final void doInStream(InputStream inStream,
      OutputStream outStreamthrows IOException {
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inStream.read(buffer)) >= 0)
      outStream.write(buffer, 0, length);
    inStream.close();
    outStream.close();
  }

}

Following is the output if you run the application

C:\java ZipGetInStream
Total number of entries in a zip file :2
Name of Extract file :Document.txt
Name of Extract file :bharat.txt

Download this code

Advertisement

Related Tags for Use of size and getInputStream method of ZipFile.:


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 21, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com