Decompress a zip file using ZipFile in Java

Description

The following code shows how to decompress a zip file using ZipFile.

Example


//w ww  .  j  a  v  a 2 s.c  om
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
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 {
    String zipname = "data.zip";
    ZipFile zipFile = new ZipFile(zipname);
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
      ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
      System.out.println("Unzipping: " + zipEntry.getName());
      BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
      int size;
      byte[] buffer = new byte[2048];
      BufferedOutputStream bos = new BufferedOutputStream(
          new FileOutputStream(zipEntry.getName()), buffer.length);
      while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
        bos.write(buffer, 0, size);
      }
      bos.flush();
      bos.close();
      bis.close();
    }
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip