Java ZipInputStream tutorial

Java ZipInputStream tutorial shows how to read ZIP files in Java with ZipInputStream.

Java ZipInputStream

ZipInputStream is a Java class that implements an input stream filter for reading files in the ZIP file format. It has support for both compressed and uncompressed entries.

ZIP

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. Java Archive (JAR) is built on the ZIP format.

ZipInputStream constructors

ZipInputStream has the following constructors:

ZipInputStream(InputStream in)
ZipInputStream(InputStream in, Charset charset)

ZipInputStream getNextEntry

The ZipInputStream's getNextEntry() reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Java read ZIP example

The following example reads the contents of a ZIP file.

JavaReadZip.java
package com.zetcode;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class JavaReadZip {

    private final static Long MILLS_IN_DAY = 86400000L;
    
    public static void main(String[] args) throws IOException {

        String fileName = "src/resources/myfile.zip";

        FileInputStream fis = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(fis);

        try (ZipInputStream zis = new ZipInputStream(bis)) {
            ZipEntry ze;

            while ((ze = zis.getNextEntry()) != null) {

                System.out.format("File: %s Size: %d Last Modified %s %n", 
                        ze.getName(), ze.getSize(), 
                        LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));
            }
        }
    }
}

The example reads the given ZIP file with ZipInputStream and prints its contents to the terminal. We print the file names, their size, and the last modification time.

String fileName = "src/resources/myfile.zip";

The ZIP file is located int src/resources/ directory.

FileInputStream fis = new FileInputStream(fileName);

We create a FileInputStream from the file. FileInputStream is used for reading streams of raw bytes.

BufferedInputStream bis = new BufferedInputStream(fis);

For better performance, we pass the FileInputStream into the BufferedInputStream.

try (ZipInputStream zis = new ZipInputStream(bis)) {

A ZipInputStream is created from the buffered FileInputStream. The try-with-resources closes the stream when it is not needed anymore.

while ((ze = zis.getNextEntry()) != null) {

In a while loop, we go through the entries of the ZIP file with getNextEntry() method. It returns null if there are no more entries.

System.out.format("File: %s Size: %d Last Modified %s %n", 
        ze.getName(), ze.getSize(), 
        LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));

The getName() returns the name of the entry, the getSize() returns the uncompressed size of the entry, and the getTime() returns the last modification time of the entry.

File: maven.pdf Size: 6430817 Last Modified 2017-02-23 
File: mavenbyexample.pdf Size: 1363061 Last Modified 2017-02-15 
File: modal_verbs.jpg Size: 31353 Last Modified 2017-03-04 
File: sid.jpg Size: 57708 Last Modified 2017-06-05 
File: spring-boot-reference.pdf Size: 1946586 Last Modified 2017-06-05 

This is a sample output.

Java decompress ZIP example

In the next example, we decompress a ZIP file in Java.

JavaUnzip.java
package com.zetcode;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class JavaUnzip {

    public static void main(String args[]) throws Exception {

        byte[] buffer = new byte[2048];

        Path outDir = Paths.get("src/resources/output/");

        String zipFileName = "src/resources/myfile.zip";

        FileInputStream fis = new FileInputStream(zipFileName);
        BufferedInputStream bis = new BufferedInputStream(fis);

        try (ZipInputStream stream = new ZipInputStream(bis)) {

            ZipEntry entry;
            while ((entry = stream.getNextEntry()) != null) {

                Path filePath = outDir.resolve(entry.getName());

                FileOutputStream fos = new FileOutputStream(filePath.toFile());
                try (BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) {

                    int len;
                    while ((len  = stream.read(buffer)) > 0) {
                        bos.write(buffer, 0, len);
                    }
                }
            }
        }
    }
}

The example uses ZipInputStream to read the contents of the given ZIP file and FileOutputStream and BufferedOutputStream to write the contents into a directory.

Path outDir = Paths.get("src/resources/output/");

This is the directory where we extract the contents of the ZIP file.

while ((entry = stream.getNextEntry()) != null) {

In the first while loop, we go through the entries of the ZIP file.

while ((len  = stream.read(buffer)) > 0) {
    bos.write(buffer, 0, len);
}

In the second while loop, we read the entries and write them to the output stream.

In this tutorial, we have presented the Java InputStream class. We have created two examples to read and to decompress a ZIP file. You might also be interested in the related tutorials: Reading text files in Java, Java HashMap tutorial, Java ArrayList tutorial, Java static keyword, HashMap iteration in Java, Java 8 forEach tutorial, Reading and writing ICO images in Java, Java tutorial, Displaying image in Java.