Java : Inflater (ZIP) with Examples
Inflater (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Inflater methods.
Summary
Please see also : InflaterInputStream, InflaterOutputStream
// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
deflater.setInput(input);
deflater.finish();
compressedLength = deflater.deflate(compressed);
// 11
System.out.println(compressedLength);
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
// --------
// Decompression
final var inflater = new Inflater();
try {
inflater.setInput(compressed, 0, compressedLength);
final var decompressed = new byte[10];
final var decompressedLength = inflater.inflate(decompressed);
// 5
System.out.println(decompressedLength);
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(decompressed));
// aaaaa
System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
inflater.end();
}
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
System.out.printf("length=%d : %s%n", length, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : aaaXX
//length=5 : XbbbY
//length=2 : YY
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();
}
Constructors
Inflater ()
final var input = deflate("aaaaa");
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26]
System.out.println(Arrays.toString(input));
final var inflater = new Inflater();
try {
inflater.setInput(input);
final var output = new byte[10];
final var length = inflater.inflate(output);
// 5
System.out.println(length);
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output));
// aaaaa
System.out.println(new String(output, 0, length));
} finally {
inflater.end();
}
Inflater (boolean nowrap)
final var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
final var compressed = new byte[10];
int compressLength;
try {
final var input = "aaaXXX".getBytes();
// [97, 97, 97, 88, 88, 88]
System.out.println(Arrays.toString(input));
deflater.setInput(input);
deflater.finish();
compressLength = deflater.deflate(compressed);
// 8
System.out.println(compressLength);
// [75, 76, 76, -116, -120, -120, 0, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
final var inflater = new Inflater(true);
try {
inflater.setInput(compressed, 0, compressLength);
final var uncompressed = new byte[10];
final var uncompressedLength = inflater.inflate(uncompressed);
// 6
System.out.println(uncompressedLength);
// [97, 97, 97, 88, 88, 88, 0, 0, 0, 0]
System.out.println(Arrays.toString(uncompressed));
// aaaXXX
System.out.println(new String(uncompressed, 0, uncompressedLength));
} finally {
inflater.end();
}
Methods
void end ()
Please see inflate(byte[] output).
boolean finished ()
Please see inflate(byte[] output).
int getAdler ()
final var input = deflate("aaaaa");
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26]
System.out.println(Arrays.toString(input));
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println(inflater.getAdler()); // 1
final var output = new byte[10];
System.out.println(inflater.inflate(output)); // 5
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output));
System.out.println(inflater.getAdler()); // 95683046
} finally {
inflater.end();
}
long getBytesRead ()
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
final var total = inflater.getBytesRead();
System.out.printf("length=%d : total=%2d : %s%n",
length, total, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : total= 9 : aaaXX
//length=5 : total=14 : XbbbY
//length=2 : total=20 : YY
long getBytesWritten ()
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
final var total = inflater.getBytesWritten();
System.out.printf("length=%d : total=%2d : %s%n",
length, total, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : total= 5 : aaaXX
//length=5 : total=10 : XbbbY
//length=2 : total=12 : YY
int getRemaining ()
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
final var total = inflater.getRemaining();
System.out.printf("length=%d : total=%2d : %s%n",
length, total, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : total=11 : aaaXX
//length=5 : total= 6 : XbbbY
//length=2 : total= 0 : YY
int getTotalIn ()
Please see also : getBytesRead()
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
final var total = inflater.getTotalIn();
System.out.printf("length=%d : total=%2d : %s%n",
length, total, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : total= 9 : aaaXX
//length=5 : total=14 : XbbbY
//length=2 : total=20 : YY
int getTotalOut ()
Please see also : getBytesWritten()
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
final var total = inflater.getTotalOut();
System.out.printf("length=%d : total=%2d : %s%n",
length, total, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : total= 5 : aaaXX
//length=5 : total=10 : XbbbY
//length=2 : total=12 : YY
int inflate (byte[] output)
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
System.out.println("-- inflate --");
while (!inflater.finished()) {
final var output = new byte[5];
final var length = inflater.inflate(output);
System.out.printf("length=%d : %s%n", length, new String(output, 0, length));
}
} finally {
inflater.end();
}
// Result
// ↓
//-- inflate --
//length=5 : aaaXX
//length=5 : XbbbY
//length=2 : YY
int inflate (byte[] output, int off, int len)
final var input = deflate("aaaaa");
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26]
System.out.println(Arrays.toString(input));
// 11
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
final var output1 = new byte[10];
System.out.println(inflater.inflate(output1)); // 5
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output1));
inflater.reset();
inflater.setInput(input);
final var output2 = new byte[10];
System.out.println(inflater.inflate(output2, 2, 7)); // 5
// [0, 0, 97, 97, 97, 97, 97, 0, 0, 0]
System.out.println(Arrays.toString(output2));
} finally {
inflater.end();
}
int inflate (ByteBuffer output)
final var input = deflate("aaaaa");
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26]
System.out.println(Arrays.toString(input));
// 11
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
final var output = ByteBuffer.allocate(10);
final var length = inflater.inflate(output);
System.out.println(length); // 5
System.out.println(output); // java.nio.HeapByteBuffer[pos=5 lim=10 cap=10]
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output.array()));
// aaaaa
System.out.println(new String(output.array(), 0, length));
} finally {
inflater.end();
}
boolean needsDictionary ()
Please see setDictionary(byte[] dictionary).
boolean needsInput ()
final var input = deflate("aaaaXXXX");
// [120, -100, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
System.out.println(Arrays.toString(input));
// 14
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input);
final var output1 = new byte[5];
final var length1 = inflater.inflate(output1);
System.out.println(length1); // 5
System.out.println(new String(output1, 0, length1)); // aaaaX
System.out.println(inflater.needsInput()); // false
final var output2 = new byte[5];
final var length2 = inflater.inflate(output2);
System.out.println(length2); // 3
System.out.println(new String(output2, 0, length2)); // XXX
System.out.println(inflater.needsInput()); // true
} finally {
inflater.end();
}
void reset ()
Please see inflate(byte[] output, int off, int len).
void setDictionary (byte[] dictionary)
final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();
// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
deflater.setDictionary(dictionary);
deflater.setInput(input);
deflater.finish();
compressedLength = deflater.deflate(compressed);
System.out.println(compressedLength); // 14
// [120, -69, 7, -42, 2, 50, 75, -60, 32, 1, 62, -12, 6, -108, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
// --------
// Decompression
final var inflater = new Inflater();
try {
inflater.setInput(compressed, 0, compressedLength);
final var decompressed = new byte[20];
System.out.println(inflater.inflate(decompressed)); // 0
System.out.println(inflater.needsDictionary()); // true
inflater.setDictionary(dictionary);
final int decompressedLength = inflater.inflate(decompressed);
System.out.println(decompressedLength); // 18
// [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
System.out.println(Arrays.toString(decompressed));
// abcXYZabcXYZabcXYZ
System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
inflater.end();
}
void setDictionary (byte[] dictionary, int off, int len)
final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();
// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[20];
int compressedLength;
try {
deflater.setDictionary(dictionary, 1, 4);
deflater.setInput(input);
deflater.finish();
compressedLength = deflater.deflate(compressed);
System.out.println(compressedLength); // 18
// [120, -69, 3, -66, 1, 119, 75, 76, 2, 18, 81, -56, 36, 0, 62, -12, 6, -108, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
// --------
// Decompression
final var inflater = new Inflater();
try {
inflater.setInput(compressed, 0, compressedLength);
final var decompressed = new byte[20];
System.out.println(inflater.inflate(decompressed)); // 0
System.out.println(inflater.needsDictionary()); // true
inflater.setDictionary(dictionary, 1, 4);
final int decompressedLength = inflater.inflate(decompressed);
System.out.println(decompressedLength); // 18
// [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
System.out.println(Arrays.toString(decompressed));
// abcXYZabcXYZabcXYZ
System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
inflater.end();
}
void setDictionary (ByteBuffer dictionary)
final var input = "abcXYZabcXYZabcXYZ".getBytes();
final var dictionary = "abcXYZ".getBytes();
// --------
// Compression
final var deflater = new Deflater();
final var compressed = new byte[15];
int compressedLength;
try {
deflater.setDictionary(ByteBuffer.wrap(dictionary));
deflater.setInput(input);
deflater.finish();
compressedLength = deflater.deflate(compressed);
System.out.println(compressedLength); // 14
// [120, -69, 7, -42, 2, 50, 75, -60, 32, 1, 62, -12, 6, -108, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
// --------
// Decompression
final var inflater = new Inflater();
try {
inflater.setInput(compressed, 0, compressedLength);
final var decompressed = new byte[20];
System.out.println(inflater.inflate(decompressed)); // 0
System.out.println(inflater.needsDictionary()); // true
inflater.setDictionary(ByteBuffer.wrap(dictionary));
final int decompressedLength = inflater.inflate(decompressed);
System.out.println(decompressedLength); // 18
// [97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 97, 98, 99, 88, 89, 90, 0, 0]
System.out.println(Arrays.toString(decompressed));
// abcXYZabcXYZabcXYZ
System.out.println(new String(decompressed, 0, decompressedLength));
} finally {
inflater.end();
}
void setInput (byte[] input)
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(Arrays.copyOf(input, 10));
final var output1 = new byte[10];
final var length1 = inflater.inflate(output1);
// 7
System.out.println(length1);
// [97, 97, 97, 88, 88, 88, 98, 0, 0, 0]
System.out.println(Arrays.toString(output1));
// aaaXXXb
System.out.println(new String(output1, 0, length1));
inflater.setInput(Arrays.copyOfRange(input, 10, 20));
final var output2 = new byte[10];
final var length2 = inflater.inflate(output2);
// 5
System.out.println(length2);
// [98, 98, 89, 89, 89, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
// bbYYY
System.out.println(new String(output2, 0, length2));
} finally {
inflater.end();
}
void setInput (byte[] input, int off, int len)
final var input = deflate("aaaXXXbbbYYY");
// [120, -100, 75, 76, 76, -116, -120, -120, 72, 74, 74,
// -118, -116, -116, 4, 0, 28, -95, 4, 93]
System.out.println(Arrays.toString(input));
// 20
System.out.println(input.length);
final var inflater = new Inflater();
try {
inflater.setInput(input, 0, 12);
final var output1 = new byte[10];
final var length1 = inflater.inflate(output1);
// 9
System.out.println(length1);
// [97, 97, 97, 88, 88, 88, 98, 98, 98, 0]
System.out.println(Arrays.toString(output1));
// aaaXXXbbb
System.out.println(new String(output1, 0, length1));
inflater.setInput(input, 12, 8);
final var output2 = new byte[10];
final var length2 = inflater.inflate(output2);
// 3
System.out.println(length2);
// [89, 89, 89, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
// YYY
System.out.println(new String(output2, 0, length2));
} finally {
inflater.end();
}
void setInput (ByteBuffer input)
final var input = ByteBuffer.wrap(deflate("aaaaa"));
System.out.println(input); // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26]
System.out.println(Arrays.toString(input.array()));
final var inflater = new Inflater();
try {
inflater.setInput(input);
final var output = new byte[10];
final var length = inflater.inflate(output);
// 5
System.out.println(length);
// [97, 97, 97, 97, 97, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output));
// aaaaa
System.out.println(new String(output, 0, length));
} finally {
inflater.end();
}