Java : ZipInputStream with Examples

ZipInputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ZipInputStream methods.


Summary

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.

Class diagram

final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip

// --------
// Compression
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
    zos.putNextEntry(new ZipEntry("bbb.txt"));
    zos.write("YYYYY".getBytes());

    zos.putNextEntry(new ZipEntry("ccc.txt"));
    zos.write("ZZZZZ".getBytes());
}

// --- PowerShell ---
//PS R:\java-work> ls -name
//aaa.zip
//
//PS R:\java-work> Expand-Archive .\aaa.zip .
//PS R:\java-work> ls -name
//aaa.zip
//bbb.txt
//ccc.txt
//
//PS R:\java-work> cat .\bbb.txt
//YYYYY
//PS R:\java-work> cat .\ccc.txt
//ZZZZZ

// --------
// Decompression
try (final var zis = new ZipInputStream(Files.newInputStream(path))) {
    final var entry1 = zis.getNextEntry();
    if (entry1 != null) {
        System.out.println(entry1.getName()); // bbb.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [89, 89, 89, 89, 89]
        System.out.println(new String(uncompressed)); // YYYYY
    }

    final var entry2 = zis.getNextEntry();
    if (entry2 != null) {
        System.out.println(entry2.getName()); // ccc.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [90, 90, 90, 90, 90]
        System.out.println(new String(uncompressed)); // ZZZZZ
    }

    System.out.println(zis.getNextEntry()); // null
}

Helper method for code examples

Code examples on this page use the helper method below.

// Compress a string.
public byte[] deflate(String name, String str) throws IOException {
    final var out = new ByteArrayOutputStream();
    try (final var zos = new ZipOutputStream(out)) {
        zos.putNextEntry(new ZipEntry(name));
        zos.write(str.getBytes());
    }

    return out.toByteArray();
}

Fields

static final int CENATT

Central directory (CEN) header internal file attributes field offset.

System.out.println(ZipInputStream.CENATT); // 36

static final int CENATX

Central directory (CEN) header external file attributes field offset.

System.out.println(ZipInputStream.CENATX); // 38

static final int CENCOM

Central directory (CEN) header comment length field offset.

System.out.println(ZipInputStream.CENCOM); // 32

static final int CENCRC

Central directory (CEN) header uncompressed file crc-32 value field offset.

System.out.println(ZipInputStream.CENCRC); // 16

static final int CENDSK

Central directory (CEN) header disk number start field offset.

System.out.println(ZipInputStream.CENDSK); // 34

static final int CENEXT

Central directory (CEN) header extra field length field offset.

System.out.println(ZipInputStream.CENEXT); // 30

static final int CENFLG

Central directory (CEN) header encrypt, decrypt flags field offset.

System.out.println(ZipInputStream.CENFLG); // 8

static final int CENHDR

Central directory (CEN) header size in bytes (including signature).

System.out.println(ZipInputStream.CENHDR); // 46

static final int CENHOW

Central directory (CEN) header compression method field offset.

System.out.println(ZipInputStream.CENHOW); // 10

static final int CENLEN

Central directory (CEN) header uncompressed size field offset.

System.out.println(ZipInputStream.CENLEN); // 24

static final int CENNAM

Central directory (CEN) header filename length field offset.

System.out.println(ZipInputStream.CENNAM); // 28

static final int CENOFF

Central directory (CEN) header LOC header offset field offset.

System.out.println(ZipInputStream.CENOFF); // 42

static final long CENSIG

Central directory (CEN) header signature.

System.out.printf("%#x", ZipInputStream.CENSIG); // 0x2014b50

static final int CENSIZ

Central directory (CEN) header compressed size field offset.

System.out.println(ZipInputStream.CENSIZ); // 20

static final int CENTIM

Central directory (CEN) header modification time field offset.

System.out.println(ZipInputStream.CENTIM); // 12

static final int CENVEM

Central directory (CEN) header version made by field offset.

System.out.println(ZipInputStream.CENVEM); // 4

static final int CENVER

Central directory (CEN) header version needed to extract field offset.

System.out.println(ZipInputStream.CENVER); // 6

static final int ENDCOM

End of central directory (END) header zip file comment length field offset.

System.out.println(ZipInputStream.ENDCOM); // 20

static final int ENDHDR

End of central directory (END) header size in bytes (including signature).

System.out.println(ZipInputStream.ENDHDR); // 22

static final int ENDOFF

End of central directory (END) header offset for the first CEN header field offset.

System.out.println(ZipInputStream.ENDOFF); // 16

static final long ENDSIG

End of central directory (END) header signature.

System.out.printf("%#x", ZipInputStream.ENDSIG); // 0x6054b50

static final int ENDSIZ

End of central directory (END) header central directory size in bytes field offset.

System.out.println(ZipInputStream.ENDSIZ); // 12

static final int ENDSUB

End of central directory (END) header number of entries on this disk field offset.

System.out.println(ZipInputStream.ENDSUB); // 8

static final int ENDTOT

End of central directory (END) header total number of entries field offset.

System.out.println(ZipInputStream.ENDTOT); // 10

static final int EXTCRC

Extra local (EXT) header uncompressed file crc-32 value field offset.

System.out.println(ZipInputStream.EXTCRC); // 4

static final int EXTHDR

Extra local (EXT) header size in bytes (including signature).

System.out.println(ZipInputStream.EXTHDR); // 16

static final int EXTLEN

Extra local (EXT) header uncompressed size field offset.

System.out.println(ZipInputStream.EXTLEN); // 12

static final long EXTSIG

Extra local (EXT) header signature.

System.out.printf("%#x", ZipInputStream.EXTSIG); // 0x8074b50

static final int EXTSIZ

Extra local (EXT) header compressed size field offset.

System.out.println(ZipInputStream.EXTSIZ); // 8

static final int LOCCRC

Local file (LOC) header uncompressed file crc-32 value field offset.

System.out.println(ZipInputStream.LOCCRC); // 14

static final int LOCEXT

Local file (LOC) header extra field length field offset.

System.out.println(ZipInputStream.LOCEXT); // 28

static final int LOCFLG

Local file (LOC) header general purpose bit flag field offset.

System.out.println(ZipInputStream.LOCFLG); // 6

static final int LOCHDR

Local file (LOC) header size in bytes (including signature).

System.out.println(ZipInputStream.LOCHDR); // 30

static final int LOCHOW

Local file (LOC) header compression method field offset.

System.out.println(ZipInputStream.LOCHOW); // 8

static final int LOCLEN

Local file (LOC) header uncompressed size field offset.

System.out.println(ZipInputStream.LOCLEN); // 22

static final int LOCNAM

Local file (LOC) header filename length field offset.

System.out.println(ZipInputStream.LOCNAM); // 26

static final long LOCSIG

Local file (LOC) header signature.

System.out.printf("%#x", ZipInputStream.LOCSIG); // 0x4034b50

static final int LOCSIZ

Local file (LOC) header compressed size field offset.

System.out.println(ZipInputStream.LOCSIZ); // 18

static final int LOCTIM

Local file (LOC) header modification time field offset.

System.out.println(ZipInputStream.LOCTIM); // 10

static final int LOCVER

Local file (LOC) header version needed to extract field offset.

System.out.println(ZipInputStream.LOCVER); // 4

Fields declared in InflaterInputStream

buf, inf, len

Please see the link below.

Fields declared in FilterInputStream

in

Please see the link below.

Constructors

ZipInputStream (InputStream in)

Creates a new ZIP input stream.

The deflate method is here.

final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(entry.getName()); // aaa.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
        System.out.println(new String(uncompressed)); // aaaaa
    }
}

ZipInputStream (InputStream in, Charset charset)

Creates a new ZIP input stream.

Please see ZipInputStream(InputStream in) for in parameter.

final var charset = Charset.forName("Shift_JIS");

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out, charset)) {
    zos.putNextEntry(new ZipEntry("aaa.txt"));
    zos.putNextEntry(new ZipEntry("○△×.txt"));
}

final var compressed = out.toByteArray();
System.out.println(compressed.length); // 244

try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed), charset)) {
    final var entry1 = zis.getNextEntry();
    if (entry1 != null) {
        System.out.println(entry1.getName()); // aaa.txt
    }

    final var entry2 = zis.getNextEntry();
    if (entry2 != null) {
        System.out.println(entry2.getName()); // ○△×.txt
    }
}

Methods

int available ()

Returns 0 after EOF has reached for the current entry data, otherwise always return 1.

The deflate method is here.

final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(entry.getName()); // aaa.txt

        System.out.println(zis.available()); // 1

        System.out.println(zis.read()); // 97
        System.out.println(zis.available()); // 1

        System.out.println(zis.read()); // 97
        System.out.println(zis.available()); // 1

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97]
        System.out.println(new String(uncompressed)); // aaa

        System.out.println(zis.available()); // 0
    }
}

void close ()

Closes this input stream and releases any system resources associated with the stream.

The deflate method is here.

final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip

final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
Files.write(file, bytes);

try (final var zis = new ZipInputStream(Files.newInputStream(file))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(entry.getName()); // aaa.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
        System.out.println(new String(uncompressed)); // aaaaa
    }
}

An example without try-with-resources.

final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip

final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
Files.write(file, bytes);

final var zis = new ZipInputStream(Files.newInputStream(file));
try {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(entry.getName()); // aaa.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
        System.out.println(new String(uncompressed)); // aaaaa
    }
} finally {
    zis.close();
}

void closeEntry ()

Closes the current ZIP entry and positions the stream for reading the next entry.

The deflate method is here.

final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(entry.getName()); // aaa.txt

        System.out.println(zis.available()); // 1
        System.out.println(zis.read()); // 97

        System.out.println(zis.available()); // 1
        System.out.println(zis.read()); // 97

        System.out.println(zis.available()); // 1
        zis.closeEntry();

        System.out.println(zis.available()); // 0
        System.out.println(zis.read()); // -1
    }
}

protected ZipEntry createZipEntry (String name)

Creates a new ZipEntry object for the specified entry name.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

ZipEntry getNextEntry ()

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip

// --------
// Compression
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
    zos.putNextEntry(new ZipEntry("bbb.txt"));
    zos.write("YYYYY".getBytes());

    zos.putNextEntry(new ZipEntry("ccc.txt"));
    zos.write("ZZZZZ".getBytes());
}

// --------
// Decompression
try (final var zis = new ZipInputStream(Files.newInputStream(path))) {
    final var entry1 = zis.getNextEntry();
    if (entry1 != null) {
        System.out.println(entry1.getName()); // bbb.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [89, 89, 89, 89, 89]
        System.out.println(new String(uncompressed)); // YYYYY
    }

    final var entry2 = zis.getNextEntry();
    if (entry2 != null) {
        System.out.println(entry2.getName()); // ccc.txt

        final var uncompressed = zis.readAllBytes();
        System.out.println(Arrays.toString(uncompressed)); // [90, 90, 90, 90, 90]
        System.out.println(new String(uncompressed)); // ZZZZZ
    }

    System.out.println(zis.getNextEntry()); // null
}

int read (byte[] b, int off, int len)

Reads from the current ZIP entry into an array of bytes.

The deflate method is here.

final var str = "abcdef";
System.out.println(Arrays.toString(str.getBytes())); // [97, 98, 99, 100, 101, 102]

final var bytes = deflate("aaa.txt", str);
System.out.println(bytes.length); // 133

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        final var b1 = new byte[5];
        System.out.println(zis.read(b1, 0, 3)); // 3
        System.out.println(Arrays.toString(b1)); // [97, 98, 99, 0, 0]

        final var b2 = new byte[5];
        System.out.println(zis.read(b2, 1, 2)); // 2
        System.out.println(Arrays.toString(b2)); // [0, 100, 101, 0, 0]

        final var b3 = new byte[5];
        System.out.println(zis.read(b3, 0, 5)); // 1
        System.out.println(Arrays.toString(b3)); // [102, 0, 0, 0, 0]
    }
}

long skip (long n)

Skips specified number of bytes in the current ZIP entry.

The deflate method is here.

final var bytes = deflate("aaa.txt", "aaaaaaaaaaaaaa");
System.out.println(bytes.length); // 133

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        final var uncompressed = zis.readAllBytes();
        System.out.println(uncompressed.length); // 14
    }
}

try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
    final var entry = zis.getNextEntry();
    if (entry != null) {
        System.out.println(zis.skip(5)); // 5

        final var uncompressed = zis.readAllBytes();
        System.out.println(uncompressed.length); // 9
    }
}

Methods declared in InflaterInputStream

fill, mark, markSupported, read, reset

Please see the link below.

Methods declared in FilterInputStream

read

Please see the link below.

Methods declared in InputStream

nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo

Please see the link below.


Related posts

To top of page