Decompress a zip file using ZipInputStream : ZipInputStream « File « Java Tutorial






import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;

    while ((entry = zis.getNextEntry()) != null) {
      System.out.println("Unzipping: " + entry.getName());

      int size;
      byte[] buffer = new byte[2048];

      FileOutputStream fos = new FileOutputStream(entry.getName());
      BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

      while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
        bos.write(buffer, 0, size);
      }
      bos.flush();
      bos.close();
    }
    zis.close();
    fis.close();
  }
}








11.61.ZipInputStream
11.61.1.Create ZipInputStream from FileInputStream
11.61.2.Unzip using the ZipInputStream
11.61.3.Read zip file with ZipInputStream
11.61.4.Decompress a zip file using ZipInputStream