Java : DeflaterOutputStream (ZIP) with Examples

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


Summary

This class implements an output stream filter for compressing data in the "deflate" compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.

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));

Fields

protected byte[] buf

Output buffer for writing compressed data.

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

protected Deflater def

Compressor 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

DeflaterOutputStream (OutputStream out)

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

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

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

final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(out)) {
    dos.write(input);
}

final var compressed = out.toByteArray();

// [120, -100, 75, 76, 68, 6, 0, 39, -41, 5, 79]
System.out.println(Arrays.toString(compressed));

DeflaterOutputStream (OutputStream out, boolean syncFlush)

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

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

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

    dos.flush();

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

// [120, -100, 74, 76, 76, 4, 0, 0, 0, -1, -1, 3, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(
        new BufferedOutputStream(out), false)) {
    dos.write("aaa".getBytes());

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

    dos.flush();

    // [120, -100]
    System.out.println(Arrays.toString(out.toByteArray()));
}

// [120, -100, 75, 76, 76, 4, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));

DeflaterOutputStream (OutputStream out, Deflater def)

Creates a new output stream with the specified compressor and 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 dos = new DeflaterOutputStream(
        out, new Deflater(Deflater.BEST_COMPRESSION))) {
    dos.write(input);
}

final var compressed = out.toByteArray();

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

DeflaterOutputStream (OutputStream out, Deflater def, boolean syncFlush)

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

final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(new BufferedOutputStream(out),
        new Deflater(Deflater.BEST_COMPRESSION), true)) {
    dos.write("aaa".getBytes());

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

    dos.flush();

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

// [120, -38, 74, 76, 76, 4, 0, 0, 0, -1, -1, 3, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(new BufferedOutputStream(out),
        new Deflater(Deflater.BEST_COMPRESSION), false)) {
    dos.write("aaa".getBytes());

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

    dos.flush();

    // [120, -38]
    System.out.println(Arrays.toString(out.toByteArray()));
}

// [120, -38, 75, 76, 76, 4, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));

DeflaterOutputStream (OutputStream out, Deflater def, int size)

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

Please see DeflaterOutputStream(OutputStream out, Deflater def) for out, def parameters.

class MyOutputStream extends DeflaterOutputStream {
    public MyOutputStream(OutputStream out) {
        super(out, new Deflater());
    }

    public MyOutputStream(OutputStream out, int size) {
        super(out, new Deflater(), 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
}

DeflaterOutputStream (OutputStream out, Deflater def, int size, boolean syncFlush)

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

Please see :

Methods

void close ()

Writes remaining compressed data to the output stream and closes the underlying stream.

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

try (final var dos = new DeflaterOutputStream(Files.newOutputStream(file))) {
    dos.write("aaaaaaaaaa".getBytes());
}

final var bytes = Files.readAllBytes(file);

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

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 dos = new DeflaterOutputStream(Files.newOutputStream(file));
try {
    dos.write("aaaaaaaaaa".getBytes());
} finally {
    dos.close();
}

final var bytes = Files.readAllBytes(file);

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

protected void deflate ()

Writes next block of compressed data to the output stream.

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

void finish ()

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

final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(out)) {
    dos.write("aaaaaaaaaa".getBytes());
    dos.finish();

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

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

final var compressed = out.toByteArray();

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

void flush ()

Flushes the compressed output stream.

Please see DeflaterOutputStream(OutputStream out, boolean syncFlush).

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

Writes an 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 b1 = new ByteArrayOutputStream();
    try (final var dos = new DeflaterOutputStream(b1)) {
        dos.write(input, 0, 5);
    }

    final var compressed = b1.toByteArray();

    // [120, -100, -29, 18, -111, -45, 48, 2, 0, 1, 99, 0, -105]
    System.out.println(Arrays.toString(compressed));

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

    final var uncompressed = b2.toByteArray();

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

    final var compressed = b1.toByteArray();

    // [120, -100, 19, -111, -45, 0, 0, 0, -93, 0, 91]
    System.out.println(Arrays.toString(compressed));

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

    final var uncompressed = b2.toByteArray();

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

void write (int b)

Writes a byte to the compressed output stream.

final var b1 = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(b1)) {
    dos.write(10);
    dos.write(20);
    dos.write(30);
}

final var compressed = b1.toByteArray();

// [120, -100, -29, 18, -111, 3, 0, 0, 103, 0, 61]
System.out.println(Arrays.toString(compressed));

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

final var uncompressed = b2.toByteArray();

// [10, 20, 30]
System.out.println(Arrays.toString(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