Java : DeflaterInputStream (ZIP) with Examples
DeflaterInputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most DeflaterInputStream methods.
Summary
// --------
// 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
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected final Deflater def
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterInputStream
Constructors
DeflaterInputStream (InputStream in)
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)
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)
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 ()
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 ()
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)
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 ()
Please see mark(int limit).
int read ()
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)
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 ()
Please see mark(int limit).
long skip (long n)
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
Methods declared in InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
Please see the link below.