Java : GZIPOutputStream (ZIP) with Examples

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


Summary

This class implements a stream filter for writing compressed data in the GZIP file format.

Class diagram

// --------
// Compression
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
    gos.write("aaaaa".getBytes());
    gos.write("ZZZZZ".getBytes());
}

final var compressed = out.toByteArray();

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76, 4,
//  -126, 40, 16, 0, 0, -86, 55, 77, 24, 10, 0, 0, 0]
System.out.println(Arrays.toString(compressed));

// --------
// Decompression
try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
    final var uncompressed = gis.readAllBytes();

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

    // aaaaaZZZZZ
    System.out.println(new String(uncompressed));
}

Fields

protected CRC32 crc

CRC-32 of uncompressed data.

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

Fields declared in DeflaterOutputStream

buf, def

Please see the link below.

Fields declared in FilterOutputStream

out

Please see the link below.

Constructors

GZIPOutputStream (OutputStream out)

Creates a new output stream with a default buffer size.

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 gos = new GZIPOutputStream(out)) {
    gos.write(input);
}

final var compressed = out.toByteArray();

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76,
//  -124, 1, 0, -16, -51, 17, 76, 10, 0, 0, 0]
System.out.println(Arrays.toString(compressed));

GZIPOutputStream (OutputStream out, boolean syncFlush)

Creates a new output stream with a default buffer size and the specified flush mode.

final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(
        new BufferedOutputStream(out), true)) {
    gos.write("aaa".getBytes());

    // []
    System.out.println(Arrays.toString(out.toByteArray()));

    gos.flush();

    // [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 74, 76, 76, 4, 0, 0, 0, -1, -1]
    System.out.println(Arrays.toString(out.toByteArray()));
}

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 74, 76, 76, 4,
//  0, 0, 0, -1, -1, 3, 0, 45, 115, 7, -16, 3, 0, 0, 0]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(
        new BufferedOutputStream(out), false)) {
    gos.write("aaa".getBytes());

    // []
    System.out.println(Arrays.toString(out.toByteArray()));

    gos.flush();

    // [31, -117, 8, 0, 0, 0, 0, 0, 0, -1]
    System.out.println(Arrays.toString(out.toByteArray()));
}

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76, 76, 4,
//  0, 45, 115, 7, -16, 3, 0, 0, 0]
System.out.println(Arrays.toString(out.toByteArray()));

GZIPOutputStream (OutputStream out, int size)

Creates a new output stream with the specified buffer size.

Please see GZIPOutputStream(OutputStream out) for out parameter.

class MyOutputStream extends GZIPOutputStream {
    public MyOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    public MyOutputStream(OutputStream out, int size) throws IOException {
        super(out, size);
    }

    int getSize() {
        return buf.length;
    }
}

try (final var os = new MyOutputStream(new ByteArrayOutputStream())) {
    System.out.println(os.getSize()); // 512
}

try (final var os = new MyOutputStream(new ByteArrayOutputStream(), 1024)) {
    System.out.println(os.getSize()); // 1024
}

GZIPOutputStream (OutputStream out, int size, boolean syncFlush)

Creates a new output stream with the specified buffer size and flush mode.

Please see :

Methods

void finish ()

Finishes writing compressed data to the output stream without closing the underlying stream.

final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
    gos.write("aaaaa".getBytes());
    gos.finish();

    // [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75,
    //  76, 4, 2, 0, -71, -109, -84, -18, 5,
    //  0, 0, 0]
    System.out.println(Arrays.toString(out.toByteArray()));

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

final var compressed = out.toByteArray();

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75,
//  76, 4, 2, 0, -71, -109, -84, -18, 5,
//  0, 0, 0, 1, 2, 3, 4]
System.out.println(Arrays.toString(compressed));

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

Writes array of bytes to the compressed output stream.

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 gos = new GZIPOutputStream(out)) {
        gos.write(input, 0, 5);
    }

    final var compressed = out.toByteArray();

    // [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -29, 18,
    //  -111, -45, 48, 2, 0, 36, 94, -37, -48, 5, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));

    try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
        final var uncompressed = gis.readAllBytes();

        // [10, 20, 30, 40, 50]
        System.out.println(Arrays.toString(uncompressed));
    }
}
{
    final var out = new ByteArrayOutputStream();
    try (final var gos = new GZIPOutputStream(out)) {
        gos.write(input, 1, 3);
    }

    final var compressed = out.toByteArray();

    // [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 19, -111,
    //  -45, 0, 0, -101, 69, -102, 5, 3, 0, 0, 0]
    System.out.println(Arrays.toString(compressed));

    try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
        final var uncompressed = gis.readAllBytes();

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

Methods declared in DeflaterOutputStream

close, 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