Java : GZIPInputStream (ZIP) with Examples

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


Summary

This class implements a stream filter for reading 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));
}

Helper method for code examples

Code examples on this page use the helper method below.

// Compress a string.
public byte[] deflate(String str) throws IOException {
    final var out = new ByteArrayOutputStream();
    try (final var gos = new GZIPOutputStream(out)) {
        gos.write(str.getBytes());
    }

    return out.toByteArray();
}

Fields

protected CRC32 crc

CRC-32 for uncompressed data.

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

protected boolean eos

Indicates end of input stream.

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

static final int GZIP_MAGIC

GZIP header magic number.

System.out.printf("%#x", GZIPInputStream.GZIP_MAGIC); // 0x8b1f

Fields declared in InflaterInputStream

buf, inf, len

Please see the link below.

Fields declared in FilterInputStream

in

Please see the link below.

Constructors

GZIPInputStream (InputStream in)

Creates a new input stream with a default buffer size.

The deflate method is here.

final var bytes = deflate("aaaaa");

// [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(bytes));

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

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

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

GZIPInputStream (InputStream in, int size)

Creates a new input stream with the specified buffer size.

The deflate method is here.

Please see GZIPInputStream(InputStream in) for in parameter.

class MyInputStream extends GZIPInputStream {
    public MyInputStream(InputStream in) throws IOException {
        super(in);
    }

    public MyInputStream(InputStream in, int size) throws IOException {
        super(in, size);
    }

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

final var bytes = deflate("");

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

try (final var is = new MyInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.getSize()); // 512
}

try (final var is = new MyInputStream(new ByteArrayInputStream(bytes), 1024)) {
    System.out.println(is.getSize()); // 1024
}

Methods

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.gz");
System.out.println(file); // R:\java-work\aaa.gz

final var bytes = deflate("aaaaa");

// [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(bytes));
Files.write(file, bytes);

try (final var gis = new GZIPInputStream(Files.newInputStream(file))) {
    final var uncompressed = gis.readAllBytes();

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

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

An example without try-with-resources.

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

final var bytes = deflate("aaaaa");

// [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(bytes));
Files.write(file, bytes);

final var gis = new GZIPInputStream(Files.newInputStream(file));
try {
    final var uncompressed = gis.readAllBytes();

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

    // aaaaa
    System.out.println(new String(uncompressed));
} finally {
    gis.close();
}

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

Reads uncompressed data 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(str);

// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76, 74, 78,
//  73, 77, 3, 0, -17, 57, -114, 75, 6, 0, 0, 0]
System.out.println(Arrays.toString(bytes));

try (final var gis = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
    final var b1 = new byte[5];
    System.out.println(gis.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(gis.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(gis.read(b3, 0, 5)); // 1
    System.out.println(Arrays.toString(b3)); // [102, 0, 0, 0, 0]
}

Methods declared in InflaterInputStream

available, fill, mark, markSupported, read, reset, skip

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