Java : Deflater (ZIP) with Examples
Deflater (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Deflater methods.
Summary
Please see also : DeflaterInputStream, DeflaterOutputStream
// --------
// 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 deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
System.out.printf("length=%2d : %s%n", length, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
Fields
static final int BEST_COMPRESSION
Please see Deflater(int level).
static final int BEST_SPEED
Please see Deflater(int level).
static final int DEFAULT_COMPRESSION
Please see Deflater(int level).
static final int DEFAULT_STRATEGY
Please see setStrategy(int strategy).
static final int DEFLATED
Related Fields : ZipEntry.DEFLATED, ZipEntry.STORED
System.out.println(Deflater.DEFLATED); // 8
static final int FILTERED
Please see setStrategy(int strategy).
static final int FULL_FLUSH
Please see deflate(byte[] output, int off, int len, int flush).
static final int HUFFMAN_ONLY
Please see setStrategy(int strategy).
static final int NO_COMPRESSION
Please see Deflater(int level).
static final int NO_FLUSH
Please see deflate(byte[] output, int off, int len, int flush).
static final int SYNC_FLUSH
Please see deflate(byte[] output, int off, int len, int flush).
Constructors
Deflater ()
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
deflater.setInput(input);
deflater.finish();
final var compressed = new byte[15];
final var length = deflater.deflate(compressed);
// 11
System.out.println(length);
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
Deflater (int level)
byte[] deflate(byte[] input, int level) {
final var deflater = new Deflater(level);
try {
deflater.setInput(input);
deflater.finish();
final var compressed = new byte[100];
final var length = deflater.deflate(compressed);
return Arrays.copyOf(compressed, length);
} finally {
deflater.end();
}
}
final var input = "aaaaXXXX".getBytes();
// [97, 97, 97, 97, 88, 88, 88, 88]
System.out.println(Arrays.toString(input));
{
final var compressed = deflate(input, Deflater.DEFAULT_COMPRESSION);
// [120, -100, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
System.out.println(Arrays.toString(compressed));
}
{
final var compressed = deflate(input, Deflater.NO_COMPRESSION);
// [120, 1, 1, 8, 0, -9, -1, 97, 97, 97, 97, 88, 88, 88, 88, 13, 82, 2, -27]
System.out.println(Arrays.toString(compressed));
}
{
final var compressed = deflate(input, Deflater.BEST_SPEED);
// [120, 1, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
System.out.println(Arrays.toString(compressed));
}
{
final var compressed = deflate(input, Deflater.BEST_COMPRESSION);
// [120, -38, 75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27]
System.out.println(Arrays.toString(compressed));
}
Deflater (int level, boolean nowrap)
Please see Deflater(int level) for level parameter.
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
int deflate (byte[] output)
final var deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
System.out.printf("length=%2d : %s%n", length, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
int deflate (byte[] output, int off, int len)
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
deflater.setInput(input);
deflater.finish();
final var output1 = new byte[20];
System.out.println(deflater.deflate(output1)); // 11
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output1));
deflater.reset();
deflater.setInput(input);
deflater.finish();
final var output2 = new byte[20];
System.out.println(deflater.deflate(output2, 3, 15)); // 11
// [0, 0, 0, 120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
} finally {
deflater.end();
}
int deflate (byte[] output, int off, int len, int flush)
Please see deflate(byte[] output, int off, int len) for off, len parameters.
final var deflater = new Deflater();
try {
deflater.setInput("aaaaa".getBytes());
final var output1 = new byte[15];
final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.NO_FLUSH);
System.out.println(length1); // 2
// [120, -100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output1));
deflater.finish();
final var output2 = new byte[15];
final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.NO_FLUSH);
System.out.println(length2); // 9
// [75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
} finally {
deflater.end();
}
final var deflater = new Deflater();
try {
deflater.setInput("aaaaa".getBytes());
final var output1 = new byte[15];
final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.SYNC_FLUSH);
System.out.println(length1); // 12
// [120, -100, 74, 76, 4, 2, 0, 0, 0, 0, -1, -1, 0, 0, 0]
System.out.println(Arrays.toString(output1));
deflater.finish();
final var output2 = new byte[15];
final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.SYNC_FLUSH);
System.out.println(length2); // 6
// [3, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
} finally {
deflater.end();
}
final var deflater = new Deflater();
try {
deflater.setInput("aaaaa".getBytes());
final var output1 = new byte[15];
final var length1 = deflater.deflate(output1, 0, output1.length, Deflater.FULL_FLUSH);
System.out.println(length1); // 12
// [120, -100, 74, 76, 4, 2, 0, 0, 0, 0, -1, -1, 0, 0, 0]
System.out.println(Arrays.toString(output1));
deflater.finish();
final var output2 = new byte[15];
final var length2 = deflater.deflate(output2, 0, output2.length, Deflater.FULL_FLUSH);
System.out.println(length2); // 6
// [3, 0, 5, -76, 1, -26, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(output2));
} finally {
deflater.end();
}
int deflate (ByteBuffer output)
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
deflater.setInput(input);
deflater.finish();
final var output = ByteBuffer.allocate(15);
final var length = deflater.deflate(output);
System.out.println(length); // 11
System.out.println(output); // java.nio.HeapByteBuffer[pos=11 lim=15 cap=15]
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(output.array()));
} finally {
deflater.end();
}
int deflate (ByteBuffer output, int flush)
Please see :
- deflate(ByteBuffer output) for output parameter.
- deflate(byte[] output, int off, int len, int flush) for flush parameter.
void end ()
Please see deflate(byte[] output).
void finish ()
Please see deflate(byte[] output).
boolean finished ()
Please see deflate(byte[] output).
int getAdler ()
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
deflater.setInput(input);
deflater.finish();
System.out.println(deflater.getAdler()); // 1
final var output = new byte[15];
System.out.println(deflater.deflate(output)); // 11
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(output));
System.out.println(deflater.getAdler()); // 95683046
} finally {
deflater.end();
}
long getBytesRead ()
final var deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
final var total = deflater.getBytesRead();
System.out.printf("length=%2d : total=%2d : %s%n",
length, total, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : total=24 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=24 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
long getBytesWritten ()
final var deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
final var total = deflater.getBytesWritten();
System.out.printf("length=%2d : total=%2d : %s%n",
length, total, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : total=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=20 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
int getTotalIn ()
Please see also : getBytesRead()
final var deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
final var total = deflater.getTotalIn();
System.out.printf("length=%2d : total=%2d : %s%n",
length, total, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : total=24 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=24 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
int getTotalOut ()
Please see also : getBytesWritten()
final var deflater = new Deflater();
try {
final var input = "aaaaXXXXbbbbYYYYccccZZZZ".getBytes();
deflater.setInput(input);
deflater.finish();
while (!deflater.finished()) {
final var compressed = new byte[10];
final var length = deflater.deflate(compressed);
final var total = deflater.getTotalOut();
System.out.printf("length=%2d : total=%2d : %s%n",
length, total, Arrays.toString(compressed));
}
} finally {
deflater.end();
}
// Result
// ↓
//length=10 : total=10 : [120, -100, 75, 76, 76, 76, -116, 0, -126, 36]
//length=10 : total=20 : [32, -120, 4, -126, 100, 32, -120, 2, 2, 0]
//length= 4 : total=24 : [110, 2, 8, -59, 0, 0, 0, 0, 0, 0]
boolean needsInput ()
Please see setStrategy(int strategy).
void reset ()
Please see setStrategy(int strategy).
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 deflater = new Deflater();
try {
deflater.setInput("aaaa".getBytes());
final var output1 = new byte[5];
System.out.println(deflater.deflate(output1)); // 2
System.out.println(Arrays.toString(output1)); // [120, -100, 0, 0, 0]
deflater.setInput("XXXX".getBytes());
final var output2 = new byte[5];
System.out.println(deflater.deflate(output2)); // 0
System.out.println(Arrays.toString(output2)); // [0, 0, 0, 0, 0]
deflater.finish();
final var output3 = new byte[15];
System.out.println(deflater.deflate(output3)); // 12
// [75, 76, 76, 76, -116, 0, 2, 0, 13, 82, 2, -27, 0, 0, 0]
System.out.println(Arrays.toString(output3));
} finally {
deflater.end();
}
void setInput (byte[] input, int off, int len)
final var deflater = new Deflater();
try {
final var input = "abcdXYZ".getBytes();
// [97, 98, 99, 100, 88, 89, 90]
System.out.println(Arrays.toString(input));
deflater.setInput(input, 2, 4);
deflater.finish();
final var compressed = new byte[15];
System.out.println(deflater.deflate(compressed)); // 12
// [120, -100, 75, 78, -119, -120, 4, 0, 3, -59, 1, 121, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
// Decompression
try (final var ios = new InflaterInputStream(new ByteArrayInputStream(compressed))) {
final var decompressed = ios.readAllBytes();
System.out.println(Arrays.toString(decompressed)); // [99, 100, 88, 89]
System.out.println(new String(decompressed)); // cdXY
}
} finally {
deflater.end();
}
void setInput (ByteBuffer input)
final var deflater = new Deflater();
try {
final var input = ByteBuffer.wrap("aaaaa".getBytes());
System.out.println(input); // java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(input.array())); // [97, 97, 97, 97, 97]
deflater.setInput(input);
deflater.finish();
final var compressed = new byte[15];
System.out.println(deflater.deflate(compressed)); // 11
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
void setLevel (int level)
Please see also : Deflater(int level)
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
deflater.setLevel(Deflater.NO_COMPRESSION);
deflater.setInput(input);
deflater.finish();
final var compressed = new byte[20];
System.out.println(deflater.deflate(compressed)); // 0
System.out.println(deflater.needsInput()); // false
System.out.println(deflater.deflate(compressed)); // 16
System.out.println(deflater.needsInput()); // true
// [120, 1, 1, 5, 0, -6, -1, 97, 97, 97, 97, 97, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
} finally {
deflater.end();
}
void setStrategy (int strategy)
final var deflater = new Deflater();
try {
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
deflater.setStrategy(Deflater.FILTERED);
deflater.setInput(input);
deflater.finish();
final var filtered = new byte[15];
System.out.println(deflater.deflate(filtered)); // 0
System.out.println(deflater.needsInput()); // false
System.out.println(deflater.deflate(filtered)); // 13
System.out.println(deflater.needsInput()); // true
// [120, -100, 75, 76, 76, 76, 76, 4, 0, 5, -76, 1, -26, 0, 0]
System.out.println(Arrays.toString(filtered));
deflater.reset();
deflater.setStrategy(Deflater.HUFFMAN_ONLY);
deflater.setInput(input);
deflater.finish();
final var huffman = new byte[15];
System.out.println(deflater.deflate(huffman)); // 0
System.out.println(deflater.needsInput()); // false
System.out.println(deflater.deflate(huffman)); // 13
System.out.println(deflater.needsInput()); // true
// [120, 1, 75, 76, 76, 76, 76, 4, 0, 5, -76, 1, -26, 0, 0]
System.out.println(Arrays.toString(huffman));
deflater.reset();
deflater.setStrategy(Deflater.DEFAULT_STRATEGY);
deflater.setInput(input);
deflater.finish();
final var def = new byte[15];
System.out.println(deflater.deflate(def)); // 0
System.out.println(deflater.needsInput()); // false
System.out.println(deflater.deflate(def)); // 11
System.out.println(deflater.needsInput()); // true
// [120, -100, 75, 76, 4, 2, 0, 5, -76, 1, -26, 0, 0, 0, 0]
System.out.println(Arrays.toString(def));
} finally {
deflater.end();
}