Java : InflaterOutputStream (ZIP) with Examples
InflaterOutputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most InflaterOutputStream methods.
Summary
// --------
// 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
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected final Inflater inf
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterOutputStream
Constructors
InflaterOutputStream (OutputStream out)
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)
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)
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 ()
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 ()
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 ()
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)
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)
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));