Java : InflaterOutputStream (ZIP) with Examples

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


Summary

Implements an output stream filter for uncompressing data stored in the "deflate" compression format.

Class diagram

// --------
// Compression
final var b1 = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(b1)) {
    dos.write("aaaaaaaaaa".getBytes());
    dos.write("ZZZZZZZZZZ".getBytes());
}

final var compressed = b1.toByteArray();

// [120, -100, 75, 76, -124, -127, 40, 56, 0, 0, 78, 37, 7, 79]
System.out.println(Arrays.toString(compressed));

// --------
// Decompression
final var b2 = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(b2)) {
    ios.write(compressed);
}

final var uncompressed = b2.toByteArray();

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

// aaaaaaaaaaZZZZZZZZZZ
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 dos = new DeflaterOutputStream(out)) {
        dos.write(str.getBytes());
    }

    return out.toByteArray();
}

Fields

protected final byte[] buf

Output buffer for writing uncompressed data.

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

protected final Inflater inf

Decompressor for this stream.

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

Fields declared in FilterOutputStream

out

Please see the link below.

Constructors

InflaterOutputStream (OutputStream out)

Creates a new output stream with a default decompressor and buffer size.

The deflate method is here.

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
    ios.write(bytes);
}

final var uncompressed = out.toByteArray();

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

// aaaaaaaaaa
System.out.println(new String(uncompressed));

InflaterOutputStream (OutputStream out, Inflater infl)

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

The deflate method is here.

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out, new Inflater())) {
    ios.write(bytes);
}

final var uncompressed = out.toByteArray();

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

// aaaaaaaaaa
System.out.println(new String(uncompressed));

InflaterOutputStream (OutputStream out, Inflater infl, int bufLen)

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

Please see InflaterOutputStream(OutputStream out, Inflater infl) for out, infl parameters.

class MyOutputStream extends InflaterOutputStream {
    public MyOutputStream(OutputStream out) {
        super(out, new Inflater());
    }

    public MyOutputStream(OutputStream out, int bufLen) {
        super(out, new Inflater(), bufLen);
    }

    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
}

Methods

void close ()

Writes any remaining uncompressed data to the output stream and closes the underlying output stream.

The deflate method is here.

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

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

try (final var ios = new InflaterOutputStream(Files.newOutputStream(file))) {
    ios.write(bytes);
}

System.out.println(Files.readString(file)); // aaaaaaaaaa

An example without try-with-resources.

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

final var bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var ios = new InflaterOutputStream(Files.newOutputStream(file));
try {
    ios.write(bytes);
} finally {
    ios.close();
}

System.out.println(Files.readString(file)); // aaaaaaaaaa

void finish ()

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

The deflate method is here.

final byte[] bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
    ios.write(bytes);
    ios.finish();

    // The stream is not closed.
    out.write("XYZ".getBytes());
}

final var uncompressed = out.toByteArray();

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

// aaaaaaaaaaXYZ
System.out.println(new String(uncompressed));

void flush ()

Flushes this output stream, forcing any pending buffered output bytes to be written.

The deflate method is here.

final byte[] bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var div1 = Arrays.copyOfRange(bytes, 0, 5);
System.out.println(Arrays.toString(div1)); // [120, -100, 75, 76, -124]

final var div2 = Arrays.copyOfRange(bytes, 5, bytes.length);
System.out.println(Arrays.toString(div2)); // [1, 0, 20, -31, 3, -53]

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(new BufferedOutputStream(out))) {

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

    ios.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [97, 97]

    ios.write(div2);
}

final var uncompressed = out.toByteArray();

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

// aaaaaaaaaa
System.out.println(new String(uncompressed));

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

Writes an array of bytes to the uncompressed output stream.

The deflate method is here.

final byte[] bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
    ios.write(bytes, 0, 5);

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

    ios.write(bytes, 5, 6);

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

// aaaaaaaaaa
System.out.println(out);

void write (int b)

Writes a byte to the uncompressed output stream.

The deflate method is here.

final byte[] bytes = deflate("aaaaaaaaaa");

// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));

final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
    for (final var b : bytes) {
        ios.write(b);
    }
}

final var uncompressed = out.toByteArray();

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

// aaaaaaaaaa
System.out.println(new String(uncompressed));

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