Java : DeflaterOutputStream (ZIP) - API使用例
DeflaterOutputStream (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
Deflate は ZIP でも使われている圧縮形式です。
DeflaterOutputStream クラスを使うと、deflate 圧縮形式でデータを出力できます。
// --------
// 圧縮
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));
// --------
// 解凍
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));
フィールド
protected byte[] buf
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected Deflater def
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
FilterOutputStreamで宣言されたフィールド
out
「Java API 使用例 : FilterOutputStream」をご参照ください。
コンストラクタ
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)
out, def パラメータについては DeflaterOutputStream(OutputStream out, Deflater def) の使用例をご参照ください。
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)
out, def, syncFlush パラメータについては DeflaterOutputStream(OutputStream out, Deflater def, boolean syncFlush) の使用例をご参照ください。
size パラメータについては DeflaterOutputStream(OutputStream out, Deflater def, int size) の使用例をご参照ください。
メソッド
void close ()
可能であれば try-with-resources文 を使うことをおすすめします。
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));
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です。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
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()));
// ストリームはまだ閉じられていません。
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 ()
このメソッドの使用例は、DeflaterOutputStream(OutputStream out, boolean syncFlush) にまとめて記載しました。
そちらのAPI使用例をご参照ください。
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));
FilterOutputStreamで宣言されたメソッド
write
「Java API 使用例 : FilterOutputStream」をご参照ください。
OutputStreamで宣言されたメソッド
nullOutputStream
「Java API 使用例 : OutputStream」をご参照ください。
関連記事
- API 使用例