How do I decompress a zip file using ZipFile?

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


import java.util.Enumeration;

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

        ZipFile zip = new ZipFile(zipName);
        Enumeration enumeration = zip.entries();

        while (enumeration.hasMoreElements()) {
            ZipEntry 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 =, 0, buffer.length)) != -1) {
                    bos.write(buffer, 0, size);

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

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