How do I decompress a zip file using ZipFile class?

In this example we use the java.util.zip.ZipFile class to decompress and extract a zip file.

package org.kodejava.util.zip;

import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.Enumeration;
import java.io.*;

public class ZipFileUnzipDemo {
    public static void main(String[] args) throws Exception {
        String zipName = "data.zip";

        ZipFile zip = new ZipFile(zipName);
        Enumeration<? extends ZipEntry> enumeration = zip.entries();

        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
            System.out.println("Unzipping: " + zipEntry.getName());

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

            try (BufferedInputStream bis =
                         new BufferedInputStream(zip.getInputStream(zipEntry));
                 FileOutputStream fos =
                         new FileOutputStream(zipEntry.getName());
                 BufferedOutputStream bos =
                         new BufferedOutputStream(fos, buffer.length)) {

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.