Java IO Tutorial - Java ZipFile .getInputStream (ZipEntry entry)








Syntax

ZipFile.getInputStream(ZipEntry entry) has the following syntax.

public InputStream getInputStream(ZipEntry entry)    throws IOException

Example

In the following code shows how to use ZipFile.getInputStream(ZipEntry entry) method.

//w  ww  .ja  v  a  2s . co m
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
  public static void main(String[] args) throws Exception {
    ZipFile zip = new ZipFile(new File("sample.zip"));

    for (Enumeration e = zip.entries(); e.hasMoreElements();) {
      ZipEntry entry = (ZipEntry) e.nextElement();
      System.out.println("File name: " + entry.getName() + "; size: " + entry.getSize()
          + "; compressed size: " + entry.getCompressedSize());
      InputStream is = zip.getInputStream(entry);
      InputStreamReader isr = new InputStreamReader(is);

      char[] buffer = new char[1024];
      while (isr.read(buffer, 0, buffer.length) != -1) {
        String s = new String(buffer);
        System.out.println(s.trim());
      }
    }
  }
}