Java : GZIPOutputStream (ZIP) with Examples
GZIPOutputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most GZIPOutputStream methods.
Summary
// --------
// Compression
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
gos.write("aaaaa".getBytes());
gos.write("ZZZZZ".getBytes());
}
final var compressed = out.toByteArray();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76, 4,
// -126, 40, 16, 0, 0, -86, 55, 77, 24, 10, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
// --------
// Decompression
try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
final var uncompressed = gis.readAllBytes();
// [97, 97, 97, 97, 97, 90, 90, 90, 90, 90]
System.out.println(Arrays.toString(uncompressed));
// aaaaaZZZZZ
System.out.println(new String(uncompressed));
}
Fields
protected CRC32 crc
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in DeflaterOutputStream
Fields declared in FilterOutputStream
Constructors
GZIPOutputStream (OutputStream out)
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 gos = new GZIPOutputStream(out)) {
gos.write(input);
}
final var compressed = out.toByteArray();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76,
// -124, 1, 0, -16, -51, 17, 76, 10, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
GZIPOutputStream (OutputStream out, boolean syncFlush)
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(
new BufferedOutputStream(out), true)) {
gos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
gos.flush();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 74, 76, 76, 4, 0, 0, 0, -1, -1]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 74, 76, 76, 4,
// 0, 0, 0, -1, -1, 3, 0, 45, 115, 7, -16, 3, 0, 0, 0]
System.out.println(Arrays.toString(out.toByteArray()));
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(
new BufferedOutputStream(out), false)) {
gos.write("aaa".getBytes());
// []
System.out.println(Arrays.toString(out.toByteArray()));
gos.flush();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1]
System.out.println(Arrays.toString(out.toByteArray()));
}
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75, 76, 76, 4,
// 0, 45, 115, 7, -16, 3, 0, 0, 0]
System.out.println(Arrays.toString(out.toByteArray()));
GZIPOutputStream (OutputStream out, int size)
Please see GZIPOutputStream(OutputStream out) for out parameter.
class MyOutputStream extends GZIPOutputStream {
public MyOutputStream(OutputStream out) throws IOException {
super(out);
}
public MyOutputStream(OutputStream out, int size) throws IOException {
super(out, 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
}
GZIPOutputStream (OutputStream out, int size, boolean syncFlush)
Please see :
- GZIPOutputStream(OutputStream out, boolean syncFlush) for out, syncFlush parameters.
- GZIPOutputStream(OutputStream out, int size) for size parameter.
Methods
void finish ()
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
gos.write("aaaaa".getBytes());
gos.finish();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75,
// 76, 4, 2, 0, -71, -109, -84, -18, 5,
// 0, 0, 0]
System.out.println(Arrays.toString(out.toByteArray()));
// The stream is not closed.
out.write(1);
out.write(2);
out.write(3);
out.write(4);
}
final var compressed = out.toByteArray();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 75,
// 76, 4, 2, 0, -71, -109, -84, -18, 5,
// 0, 0, 0, 1, 2, 3, 4]
System.out.println(Arrays.toString(compressed));
void write (byte[] buf, 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 out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
gos.write(input, 0, 5);
}
final var compressed = out.toByteArray();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -29, 18,
// -111, -45, 48, 2, 0, 36, 94, -37, -48, 5, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
final var uncompressed = gis.readAllBytes();
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(uncompressed));
}
}
{
final var out = new ByteArrayOutputStream();
try (final var gos = new GZIPOutputStream(out)) {
gos.write(input, 1, 3);
}
final var compressed = out.toByteArray();
// [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 19, -111,
// -45, 0, 0, -101, 69, -102, 5, 3, 0, 0, 0]
System.out.println(Arrays.toString(compressed));
try (final var gis = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
final var uncompressed = gis.readAllBytes();
// [20, 30, 40]
System.out.println(Arrays.toString(uncompressed));
}
}