Java : DeflaterOutputStream (ZIP) with Examples
DeflaterOutputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most DeflaterOutputStream 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));
Fields
protected byte[] buf
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected Deflater def
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterOutputStream
Constructors
DeflaterOutputStream (OutputStream out)
final var input = "aaaaaaaaaaaaaa".getBytes();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(out)) {
dos.write(input);
}
final var compressed = out.toByteArray();
// [120, -100, 75, 76, 68, 6, 0, 39, -41, 5, 79]
System.out.println(Arrays.toString(compressed));
DeflaterOutputStream (OutputStream out, boolean syncFlush)
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(
new BufferedOutputStream(out), true)) {
dos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
dos.flush();
// [120, -100, 74, 76, 76, 4, 0, 0, 0, -1, -1]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [120, -100, 74, 76, 76, 4, 0, 0, 0, -1, -1, 3, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(
new BufferedOutputStream(out), false)) {
dos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
dos.flush();
// [120, -100]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [120, -100, 75, 76, 76, 4, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
DeflaterOutputStream (OutputStream out, Deflater def)
final var input = "aaaaaaaaaa".getBytes();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(
out, new Deflater(Deflater.BEST_COMPRESSION))) {
dos.write(input);
}
final var compressed = out.toByteArray();
// [120, -38, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(compressed));
DeflaterOutputStream (OutputStream out, Deflater def, boolean syncFlush)
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(new BufferedOutputStream(out),
new Deflater(Deflater.BEST_COMPRESSION), true)) {
dos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
dos.flush();
// [120, -38, 74, 76, 76, 4, 0, 0, 0, -1, -1]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [120, -38, 74, 76, 76, 4, 0, 0, 0, -1, -1, 3, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(new BufferedOutputStream(out),
new Deflater(Deflater.BEST_COMPRESSION), false)) {
dos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
dos.flush();
// [120, -38]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [120, -38, 75, 76, 76, 4, 0, 2, 73, 1, 36]
System.out.println(Arrays.toString(out.toByteArray()));
DeflaterOutputStream (OutputStream out, Deflater def, int size)
Please see DeflaterOutputStream(OutputStream out, Deflater def) for out, def parameters.
class MyOutputStream extends DeflaterOutputStream {
public MyOutputStream(OutputStream out) {
super(out, new Deflater());
}
public MyOutputStream(OutputStream out, int size) {
super(out, new Deflater(), size);
}
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
}
DeflaterOutputStream (OutputStream out, Deflater def, int size, boolean syncFlush)
Please see :
- DeflaterOutputStream(OutputStream out, Deflater def, boolean syncFlush) for out, def, syncFlush parameters.
- DeflaterOutputStream(OutputStream out, Deflater def, int size) for size parameter.
Methods
void close ()
final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data
try (final var dos = new DeflaterOutputStream(Files.newOutputStream(file))) {
dos.write("aaaaaaaaaa".getBytes());
}
final var bytes = Files.readAllBytes(file);
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
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 dos = new DeflaterOutputStream(Files.newOutputStream(file));
try {
dos.write("aaaaaaaaaa".getBytes());
} finally {
dos.close();
}
final var bytes = Files.readAllBytes(file);
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
protected void deflate ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
void finish ()
final var out = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(out)) {
dos.write("aaaaaaaaaa".getBytes());
dos.finish();
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(out.toByteArray()));
// The stream is not closed.
out.write(0);
out.write(1);
out.write(2);
out.write(3);
}
final var compressed = out.toByteArray();
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53, 0, 1, 2, 3]
System.out.println(Arrays.toString(compressed));
void flush ()
Please see DeflaterOutputStream(OutputStream out, boolean syncFlush).
void write (byte[] b, int off, int len)
final byte[] input = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(input)); // [10, 20, 30, 40, 50]
{
final var b1 = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(b1)) {
dos.write(input, 0, 5);
}
final var compressed = b1.toByteArray();
// [120, -100, -29, 18, -111, -45, 48, 2, 0, 1, 99, 0, -105]
System.out.println(Arrays.toString(compressed));
final var b2 = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(b2)) {
ios.write(compressed);
}
final var uncompressed = b2.toByteArray();
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(uncompressed));
}
{
final var b1 = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(b1)) {
dos.write(input, 1, 3);
}
final var compressed = b1.toByteArray();
// [120, -100, 19, -111, -45, 0, 0, 0, -93, 0, 91]
System.out.println(Arrays.toString(compressed));
final var b2 = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(b2)) {
ios.write(compressed);
}
final var uncompressed = b2.toByteArray();
// [20, 30, 40]
System.out.println(Arrays.toString(uncompressed));
}
void write (int b)
final var b1 = new ByteArrayOutputStream();
try (final var dos = new DeflaterOutputStream(b1)) {
dos.write(10);
dos.write(20);
dos.write(30);
}
final var compressed = b1.toByteArray();
// [120, -100, -29, 18, -111, 3, 0, 0, 103, 0, 61]
System.out.println(Arrays.toString(compressed));
final var b2 = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(b2)) {
ios.write(compressed);
}
final var uncompressed = b2.toByteArray();
// [10, 20, 30]
System.out.println(Arrays.toString(uncompressed));
Methods declared in FilterOutputStream
Methods declared in OutputStream
Related posts
- API Examples