Java : InflaterOutputStream (ZIP) - API使用例
InflaterOutputStream (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
Deflate は ZIP でも使われている圧縮形式です。
InflaterOutputStream クラスを使うと、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));
共通で使うメソッド
本記事のコード例では、利便性のために以下のメソッドを使います。
// 文字列を圧縮します。
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();
}
フィールド
protected final byte[] buf
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected final Inflater inf
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
FilterOutputStreamで宣言されたフィールド
out
「Java API 使用例 : FilterOutputStream」をご参照ください。
コンストラクタ
InflaterOutputStream (OutputStream out)
final var bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
ios.write(bytes);
}
final var uncompressed = out.toByteArray();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(uncompressed));
// aaaaaaaaaa
System.out.println(new String(uncompressed));
InflaterOutputStream (OutputStream out, Inflater infl)
final var bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out, new Inflater())) {
ios.write(bytes);
}
final var uncompressed = out.toByteArray();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(uncompressed));
// aaaaaaaaaa
System.out.println(new String(uncompressed));
InflaterOutputStream (OutputStream out, Inflater infl, int bufLen)
out, infl パラメータについては InflaterOutputStream(OutputStream out, Inflater infl) の使用例をご参照ください。
class MyOutputStream extends InflaterOutputStream {
public MyOutputStream(OutputStream out) {
super(out, new Inflater());
}
public MyOutputStream(OutputStream out, int bufLen) {
super(out, new Inflater(), bufLen);
}
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
}
メソッド
void close ()
可能であれば try-with-resources文 を使うことをおすすめします。
final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data
final var bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
try (final var ios = new InflaterOutputStream(Files.newOutputStream(file))) {
ios.write(bytes);
}
System.out.println(Files.readString(file)); // aaaaaaaaaa
try-with-resources文を使わない例です。
final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data
final var bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var ios = new InflaterOutputStream(Files.newOutputStream(file));
try {
ios.write(bytes);
} finally {
ios.close();
}
System.out.println(Files.readString(file)); // aaaaaaaaaa
void finish ()
final byte[] bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
ios.write(bytes);
ios.finish();
// ストリームはまだ閉じられていません。
out.write("XYZ".getBytes());
}
final var uncompressed = out.toByteArray();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 88, 89, 90]
System.out.println(Arrays.toString(uncompressed));
// aaaaaaaaaaXYZ
System.out.println(new String(uncompressed));
void flush ()
final byte[] bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var div1 = Arrays.copyOfRange(bytes, 0, 5);
System.out.println(Arrays.toString(div1)); // [120, -100, 75, 76, -124]
final var div2 = Arrays.copyOfRange(bytes, 5, bytes.length);
System.out.println(Arrays.toString(div2)); // [1, 0, 20, -31, 3, -53]
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(new BufferedOutputStream(out))) {
ios.write(div1);
System.out.println(Arrays.toString(out.toByteArray())); // []
ios.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [97, 97]
ios.write(div2);
}
final var uncompressed = out.toByteArray();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(uncompressed));
// aaaaaaaaaa
System.out.println(new String(uncompressed));
void write (byte[] b, int off, int len)
final byte[] bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
ios.write(bytes, 0, 5);
// [97, 97]
System.out.println(Arrays.toString(out.toByteArray()));
ios.write(bytes, 5, 6);
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(out.toByteArray()));
}
// aaaaaaaaaa
System.out.println(out);
void write (int b)
final byte[] bytes = deflate("aaaaaaaaaa");
// [120, -100, 75, 76, -124, 1, 0, 20, -31, 3, -53]
System.out.println(Arrays.toString(bytes));
final var out = new ByteArrayOutputStream();
try (final var ios = new InflaterOutputStream(out)) {
for (final var b : bytes) {
ios.write(b);
}
}
final var uncompressed = out.toByteArray();
// [97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
System.out.println(Arrays.toString(uncompressed));
// aaaaaaaaaa
System.out.println(new String(uncompressed));
FilterOutputStreamで宣言されたメソッド
write
「Java API 使用例 : FilterOutputStream」をご参照ください。
OutputStreamで宣言されたメソッド
nullOutputStream
「Java API 使用例 : OutputStream」をご参照ください。