Java : DeflaterInputStream (ZIP) with Examples

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


Summary

Implements an input stream filter for compressing data in the "deflate" compression format.

Class diagram

// --------
// Compression
final var buf = "aaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    final var compressed = dis.readAllBytes();

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

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

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

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

Fields

protected final byte[] buf

Input buffer for reading compressed data.

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

protected final 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 FilterInputStream

in

Please see the link below.

Constructors

DeflaterInputStream (InputStream in)

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

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

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

try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    final var compressed = dis.readAllBytes();

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

DeflaterInputStream (InputStream in, Deflater defl)

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

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

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

try (final var dis = new DeflaterInputStream(
        new ByteArrayInputStream(buf), new Deflater(Deflater.BEST_COMPRESSION))) {
    final var compressed = dis.readAllBytes();

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

DeflaterInputStream (InputStream in, Deflater defl, int bufLen)

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

Please see DeflaterInputStream(InputStream in, Deflater defl) for in, defl parameters.

class MyInputStream extends DeflaterInputStream {
    public MyInputStream(InputStream in) {
        super(in, new Deflater());
    }

    public MyInputStream(InputStream in, int bufLen) {
        super(in, new Deflater(), bufLen);
    }

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

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

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

Methods

int available ()

Returns 0 after EOF has been reached, otherwise always return 1.

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

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

try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(dis.available()); // 1

    System.out.println(dis.read()); // 120
    System.out.println(dis.available()); // 1

    System.out.println(dis.read()); // 156
    System.out.println(dis.available()); // 1

    System.out.println(dis.read()); // 75
    System.out.println(dis.available()); // 1

    // [76, 68, 6, 0, 39, -41, 5, 79]
    System.out.println(Arrays.toString(dis.readAllBytes()));

    System.out.println(dis.available()); // 0
}

void close ()

Closes this input stream and its underlying input stream, discarding any pending uncompressed data.

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

Files.writeString(file, "aaaaaaaaaa");

try (final var dis = new DeflaterInputStream(Files.newInputStream(file))) {
    final var compressed = dis.readAllBytes();

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

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

Files.writeString(file, "aaaaaaaaaa");

final var dis = new DeflaterInputStream(Files.newInputStream(file));
try {
    final var compressed = dis.readAllBytes();

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

void mark (int limit)

This operation is not supported.

final var buf = "aaaaaaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    System.out.println("markSupported : " + dis.markSupported());

    dis.mark(0);
    dis.reset();

} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//markSupported : false
//IOException! : mark/reset not supported

boolean markSupported ()

Always returns false because this input stream does not support the mark() and reset() methods.

Please see mark(int limit).

int read ()

Reads a single byte of compressed data from the input stream.

final var buf = "aaaaaaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(dis.read()); // 120
    System.out.println(dis.read()); // 156
    System.out.println(dis.read()); // 75
    System.out.println(dis.read()); // 76
    System.out.println(dis.read()); // 68
    System.out.println(dis.read()); // 6
    System.out.println(dis.read()); // 0
    System.out.println(dis.read()); // 39
    System.out.println(dis.read()); // 215
    System.out.println(dis.read()); // 5
    System.out.println(dis.read()); // 79
    System.out.println(dis.read()); // -1
}

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

Reads compressed data into a byte array.

final var buf = "aaaaaaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    // [120, -100, 75, 76, 68, 6, 0, 39, -41, 5, 79]
    System.out.println(Arrays.toString(dis.readAllBytes()));
}

try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[5];
    System.out.println(dis.read(b1, 0, 3)); // 3
    System.out.println(Arrays.toString(b1)); // [120, -100, 75, 0, 0]

    final var b2 = new byte[5];
    System.out.println(dis.read(b2, 1, 2)); // 2
    System.out.println(Arrays.toString(b2)); // [0, 76, 68, 0, 0]

    final var b3 = new byte[8];
    System.out.println(dis.read(b3, 0, 8)); // 6
    System.out.println(Arrays.toString(b3)); // [6, 0, 39, -41, 5, 79, 0, 0]
}

void reset ()

This operation is not supported.

Please see mark(int limit).

long skip (long n)

Skips over and discards data from the input stream.

final var buf = "aaaaaaaaaaaaaa".getBytes();
try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    // [120, -100, 75, 76, 68, 6, 0, 39, -41, 5, 79]
    System.out.println(Arrays.toString(dis.readAllBytes()));
}

try (final var dis = new DeflaterInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(dis.skip(5)); // 5

    // [6, 0, 39, -41, 5, 79]
    System.out.println(Arrays.toString(dis.readAllBytes()));
}

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