Java : ZipOutputStream with Examples

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


Summary

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

Class diagram

// --------
// 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
}

Fields

static final int CENATT

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

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

static final int CENATX

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

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

static final int CENCOM

Central directory (CEN) header comment length field offset.

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

static final int CENCRC

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

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

static final int CENDSK

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

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

static final int CENEXT

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

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

static final int CENFLG

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

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

static final int CENHDR

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

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

static final int CENHOW

Central directory (CEN) header compression method field offset.

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

static final int CENLEN

Central directory (CEN) header uncompressed size field offset.

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

static final int CENNAM

Central directory (CEN) header filename length field offset.

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

static final int CENOFF

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

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

static final long CENSIG

Central directory (CEN) header signature.

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

static final int CENSIZ

Central directory (CEN) header compressed size field offset.

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

static final int CENTIM

Central directory (CEN) header modification time field offset.

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

static final int CENVEM

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

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

static final int CENVER

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

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

static final int DEFLATED

Compression method for compressed (DEFLATED) entries.

System.out.println(ZipOutputStream.DEFLATED); // 8

static final int ENDCOM

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

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

static final int ENDHDR

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

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

static final int ENDOFF

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

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

static final long ENDSIG

End of central directory (END) header signature.

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

static final int ENDSIZ

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

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

static final int ENDSUB

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

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

static final int ENDTOT

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

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

static final int EXTCRC

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

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

static final int EXTHDR

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

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

static final int EXTLEN

Extra local (EXT) header uncompressed size field offset.

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

static final long EXTSIG

Extra local (EXT) header signature.

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

static final int EXTSIZ

Extra local (EXT) header compressed size field offset.

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

static final int LOCCRC

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

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

static final int LOCEXT

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

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

static final int LOCFLG

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

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

static final int LOCHDR

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

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

static final int LOCHOW

Local file (LOC) header compression method field offset.

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

static final int LOCLEN

Local file (LOC) header uncompressed size field offset.

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

static final int LOCNAM

Local file (LOC) header filename length field offset.

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

static final long LOCSIG

Local file (LOC) header signature.

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

static final int LOCSIZ

Local file (LOC) header compressed size field offset.

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

static final int LOCTIM

Local file (LOC) header modification time field offset.

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

static final int LOCVER

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

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

static final int STORED

Compression method for uncompressed (STORED) entries.

System.out.println(ZipOutputStream.STORED); // 0

Fields declared in DeflaterOutputStream

buf, def

Please see the link below.

Fields declared in FilterOutputStream

out

Please see the link below.

Constructors

ZipOutputStream (OutputStream out)

Creates a new ZIP output stream.

final var input = "aaaaa".getBytes();

// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
    zos.putNextEntry(new ZipEntry("aaa.txt"));
    zos.write(input);
}

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

// [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, -106, -118, 63, 86, 0, 0, 0, 0, ...]
System.out.println(Arrays.toString(compressed));

ZipOutputStream (OutputStream out, Charset charset)

Creates a new ZIP output stream.

Please see ZipOutputStream(OutputStream out) for out 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

void close ()

Closes the ZIP output stream as well as the stream being filtered.

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

try (final var zos = new ZipOutputStream(Files.newOutputStream(file))) {
    zos.putNextEntry(new ZipEntry("xxx.txt"));
    zos.write("aaaaa".getBytes());
}

final var bytes = Files.readAllBytes(file);
System.out.println(bytes.length); // 133

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 zos = new ZipOutputStream(Files.newOutputStream(file));
try {
    zos.putNextEntry(new ZipEntry("xxx.txt"));
    zos.write("aaaaa".getBytes());
} finally {
    zos.close();
}

final var bytes = Files.readAllBytes(file);
System.out.println(bytes.length); // 133

void closeEntry ()

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

final var input = "aaaaaaaaaa".getBytes();

// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
    final var entry = new ZipEntry("aaa.txt");
    zos.putNextEntry(entry);
    zos.write(input);

    System.out.println(entry.getSize()); // -1
    System.out.println(entry.getCompressedSize()); // -1
    System.out.println(out.toByteArray().length); // 37

    zos.closeEntry();

    System.out.println(entry.getSize()); // 10
    System.out.println(entry.getCompressedSize()); // 5
    System.out.println(out.toByteArray().length); // 58
}

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

void finish ()

Finishes writing the contents of the ZIP output stream without closing the underlying stream.

final var input = "aaaaa".getBytes();

// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
    zos.putNextEntry(new ZipEntry("aaa.txt"));
    zos.write(input);
    zos.finish();

    // [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 59, -102, 63, 86, 0, 0, 0, ...]
    System.out.println(Arrays.toString(out.toByteArray()));

    // The stream is not closed.
    out.write(1);
    out.write(2);
    out.write(3);
}

final var compressed = out.toByteArray();

// [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 59, -102, 63, 86, 0, 0, 0, ..., 1, 2, 3]
System.out.println(Arrays.toString(compressed));

void putNextEntry (ZipEntry e)

Begins writing a new ZIP file entry and positions the stream to the start 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
}

void setComment (String comment)

Sets the ZIP file comment.

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

try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
    zos.setComment("Comment!");

    zos.putNextEntry(new ZipEntry("bbb.txt"));
    zos.write("YYYYY".getBytes());
}

try (final var zipFile = new ZipFile(path.toFile())) {
    System.out.println(zipFile.getComment()); // Comment!
}

void setLevel (int level)

Sets the compression level for subsequent entries which are DEFLATED.

final var input = "aaaaaaaaaa".getBytes();
System.out.println(input.length); // 10

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
    final var entry1 = new ZipEntry("aaa.txt");
    zos.putNextEntry(entry1);
    zos.setLevel(Deflater.BEST_COMPRESSION);
    zos.write(input);
    zos.closeEntry();

    System.out.println(entry1.getCompressedSize()); // 5

    final var entry2 = new ZipEntry("bbb.txt");
    zos.putNextEntry(entry2);
    zos.setLevel(Deflater.NO_COMPRESSION);
    zos.write(input);
    zos.closeEntry();

    System.out.println(entry2.getCompressedSize()); // 15
}

System.out.println(out.toByteArray().length); // 254

void setMethod (int method)

Sets the default compression method for subsequent entries.

final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
    zos.setMethod(ZipEntry.STORED);

    final var input = "aaaaaaaaaa".getBytes();
    System.out.println(input.length); // 10

    final var crc = new CRC32();
    crc.update(input);

    final var entry = new ZipEntry("aaa.txt");
    entry.setCrc(crc.getValue());
    entry.setSize(input.length);

    zos.putNextEntry(entry);
    zos.write(input);
}

System.out.println(out.toByteArray().length); // 122

void write (byte[] b, int off, int len)

Writes an array of bytes to the current ZIP entry data.

final byte[] input = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(input)); // [10, 20, 30, 40, 50]

{
    final var out = new ByteArrayOutputStream();
    try (final var zos = new ZipOutputStream(out)) {
        zos.putNextEntry(new ZipEntry("aaa.txt"));
        zos.write(input, 0, 5);
    }

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

    try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed))) {
        final var entry = zis.getNextEntry();
        if (entry != null) {
            final var uncompressed = zis.readAllBytes();

            // [10, 20, 30, 40, 50]
            System.out.println(Arrays.toString(uncompressed));
        }
    }
}
{
    final var out = new ByteArrayOutputStream();
    try (final var zos = new ZipOutputStream(out)) {
        zos.putNextEntry(new ZipEntry("aaa.txt"));
        zos.write(input, 1, 3);
    }

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

    try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed))) {
        final var entry = zis.getNextEntry();
        if (entry != null) {
            final var uncompressed = zis.readAllBytes();

            // [20, 30, 40]
            System.out.println(Arrays.toString(uncompressed));
        }
    }
}

Methods declared in DeflaterOutputStream

deflate, flush, write

Please see the link below.

Methods declared in FilterOutputStream

write

Please see the link below.

Methods declared in OutputStream

nullOutputStream

Please see the link below.


Related posts

To top of page