Java : ZipInputStream - API使用例
ZipInputStream (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ZIP は Windows でよく使われる圧縮形式です。
ZipInputStream クラスを使うと、ZIP 圧縮形式のデータを解凍して読み込めます。
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
// --------
// 圧縮
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
zos.putNextEntry(new ZipEntry("bbb.txt"));
zos.write("YYYYY".getBytes());
zos.putNextEntry(new ZipEntry("ccc.txt"));
zos.write("ZZZZZ".getBytes());
}
// --- PowerShell ---
//PS R:\java-work> ls -name
//aaa.zip
//
//PS R:\java-work> Expand-Archive .\aaa.zip .
//PS R:\java-work> ls -name
//aaa.zip
//bbb.txt
//ccc.txt
//
//PS R:\java-work> cat .\bbb.txt
//YYYYY
//PS R:\java-work> cat .\ccc.txt
//ZZZZZ
// --------
// 解凍
try (final var zis = new ZipInputStream(Files.newInputStream(path))) {
final var entry1 = zis.getNextEntry();
if (entry1 != null) {
System.out.println(entry1.getName()); // bbb.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [89, 89, 89, 89, 89]
System.out.println(new String(uncompressed)); // YYYYY
}
final var entry2 = zis.getNextEntry();
if (entry2 != null) {
System.out.println(entry2.getName()); // ccc.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [90, 90, 90, 90, 90]
System.out.println(new String(uncompressed)); // ZZZZZ
}
System.out.println(zis.getNextEntry()); // null
}
共通で使うメソッド
本記事のコード例では、利便性のために以下のメソッドを使います。
// 文字列を圧縮します。
public byte[] deflate(String name, String str) throws IOException {
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
zos.putNextEntry(new ZipEntry(name));
zos.write(str.getBytes());
}
return out.toByteArray();
}
フィールド
static final int CENATT
System.out.println(ZipInputStream.CENATT); // 36
static final int CENATX
System.out.println(ZipInputStream.CENATX); // 38
static final int CENCOM
System.out.println(ZipInputStream.CENCOM); // 32
static final int CENCRC
System.out.println(ZipInputStream.CENCRC); // 16
static final int CENDSK
System.out.println(ZipInputStream.CENDSK); // 34
static final int CENEXT
System.out.println(ZipInputStream.CENEXT); // 30
static final int CENFLG
System.out.println(ZipInputStream.CENFLG); // 8
static final int CENHDR
System.out.println(ZipInputStream.CENHDR); // 46
static final int CENHOW
System.out.println(ZipInputStream.CENHOW); // 10
static final int CENLEN
System.out.println(ZipInputStream.CENLEN); // 24
static final int CENNAM
System.out.println(ZipInputStream.CENNAM); // 28
static final int CENOFF
System.out.println(ZipInputStream.CENOFF); // 42
static final long CENSIG
System.out.printf("%#x", ZipInputStream.CENSIG); // 0x2014b50
static final int CENSIZ
System.out.println(ZipInputStream.CENSIZ); // 20
static final int CENTIM
System.out.println(ZipInputStream.CENTIM); // 12
static final int CENVEM
System.out.println(ZipInputStream.CENVEM); // 4
static final int CENVER
System.out.println(ZipInputStream.CENVER); // 6
static final int ENDCOM
System.out.println(ZipInputStream.ENDCOM); // 20
static final int ENDHDR
System.out.println(ZipInputStream.ENDHDR); // 22
static final int ENDOFF
System.out.println(ZipInputStream.ENDOFF); // 16
static final long ENDSIG
System.out.printf("%#x", ZipInputStream.ENDSIG); // 0x6054b50
static final int ENDSIZ
System.out.println(ZipInputStream.ENDSIZ); // 12
static final int ENDSUB
System.out.println(ZipInputStream.ENDSUB); // 8
static final int ENDTOT
System.out.println(ZipInputStream.ENDTOT); // 10
static final int EXTCRC
System.out.println(ZipInputStream.EXTCRC); // 4
static final int EXTHDR
System.out.println(ZipInputStream.EXTHDR); // 16
static final int EXTLEN
System.out.println(ZipInputStream.EXTLEN); // 12
static final long EXTSIG
System.out.printf("%#x", ZipInputStream.EXTSIG); // 0x8074b50
static final int EXTSIZ
System.out.println(ZipInputStream.EXTSIZ); // 8
static final int LOCCRC
System.out.println(ZipInputStream.LOCCRC); // 14
static final int LOCEXT
System.out.println(ZipInputStream.LOCEXT); // 28
static final int LOCFLG
System.out.println(ZipInputStream.LOCFLG); // 6
static final int LOCHDR
System.out.println(ZipInputStream.LOCHDR); // 30
static final int LOCHOW
System.out.println(ZipInputStream.LOCHOW); // 8
static final int LOCLEN
System.out.println(ZipInputStream.LOCLEN); // 22
static final int LOCNAM
System.out.println(ZipInputStream.LOCNAM); // 26
static final long LOCSIG
System.out.printf("%#x", ZipInputStream.LOCSIG); // 0x4034b50
static final int LOCSIZ
System.out.println(ZipInputStream.LOCSIZ); // 18
static final int LOCTIM
System.out.println(ZipInputStream.LOCTIM); // 10
static final int LOCVER
System.out.println(ZipInputStream.LOCVER); // 4
InflaterInputStreamで宣言されたフィールド
buf, inf, len
「Java API 使用例 : InflaterInputStream」をご参照ください。
FilterInputStreamで宣言されたフィールド
in
「Java API 使用例 : FilterInputStream」をご参照ください。
コンストラクタ
ZipInputStream (InputStream in)
final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(entry.getName()); // aaa.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
System.out.println(new String(uncompressed)); // aaaaa
}
}
ZipInputStream (InputStream in, Charset charset)
in パラメータについては ZipInputStream(InputStream in) の使用例をご参照ください。
final var charset = Charset.forName("Shift_JIS");
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out, charset)) {
zos.putNextEntry(new ZipEntry("aaa.txt"));
zos.putNextEntry(new ZipEntry("○△×.txt"));
}
final var compressed = out.toByteArray();
System.out.println(compressed.length); // 244
try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed), charset)) {
final var entry1 = zis.getNextEntry();
if (entry1 != null) {
System.out.println(entry1.getName()); // aaa.txt
}
final var entry2 = zis.getNextEntry();
if (entry2 != null) {
System.out.println(entry2.getName()); // ○△×.txt
}
}
メソッド
int available ()
final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(entry.getName()); // aaa.txt
System.out.println(zis.available()); // 1
System.out.println(zis.read()); // 97
System.out.println(zis.available()); // 1
System.out.println(zis.read()); // 97
System.out.println(zis.available()); // 1
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97]
System.out.println(new String(uncompressed)); // aaa
System.out.println(zis.available()); // 0
}
}
void close ()
可能であれば try-with-resources文 を使うことをおすすめします。
final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip
final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
Files.write(file, bytes);
try (final var zis = new ZipInputStream(Files.newInputStream(file))) {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(entry.getName()); // aaa.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
System.out.println(new String(uncompressed)); // aaaaa
}
}
try-with-resources文を使わない例です。
final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip
final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
Files.write(file, bytes);
final var zis = new ZipInputStream(Files.newInputStream(file));
try {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(entry.getName()); // aaa.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [97, 97, 97, 97, 97]
System.out.println(new String(uncompressed)); // aaaaa
}
} finally {
zis.close();
}
void closeEntry ()
final var bytes = deflate("aaa.txt", "aaaaa");
System.out.println(bytes.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(entry.getName()); // aaa.txt
System.out.println(zis.available()); // 1
System.out.println(zis.read()); // 97
System.out.println(zis.available()); // 1
System.out.println(zis.read()); // 97
System.out.println(zis.available()); // 1
zis.closeEntry();
System.out.println(zis.available()); // 0
System.out.println(zis.read()); // -1
}
}
protected ZipEntry createZipEntry (String name)
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
ZipEntry getNextEntry ()
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
// --------
// 圧縮
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
zos.putNextEntry(new ZipEntry("bbb.txt"));
zos.write("YYYYY".getBytes());
zos.putNextEntry(new ZipEntry("ccc.txt"));
zos.write("ZZZZZ".getBytes());
}
// --------
// 解凍
try (final var zis = new ZipInputStream(Files.newInputStream(path))) {
final var entry1 = zis.getNextEntry();
if (entry1 != null) {
System.out.println(entry1.getName()); // bbb.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [89, 89, 89, 89, 89]
System.out.println(new String(uncompressed)); // YYYYY
}
final var entry2 = zis.getNextEntry();
if (entry2 != null) {
System.out.println(entry2.getName()); // ccc.txt
final var uncompressed = zis.readAllBytes();
System.out.println(Arrays.toString(uncompressed)); // [90, 90, 90, 90, 90]
System.out.println(new String(uncompressed)); // ZZZZZ
}
System.out.println(zis.getNextEntry()); // null
}
int read (byte[] b, int off, int len)
final var str = "abcdef";
System.out.println(Arrays.toString(str.getBytes())); // [97, 98, 99, 100, 101, 102]
final var bytes = deflate("aaa.txt", str);
System.out.println(bytes.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
final var b1 = new byte[5];
System.out.println(zis.read(b1, 0, 3)); // 3
System.out.println(Arrays.toString(b1)); // [97, 98, 99, 0, 0]
final var b2 = new byte[5];
System.out.println(zis.read(b2, 1, 2)); // 2
System.out.println(Arrays.toString(b2)); // [0, 100, 101, 0, 0]
final var b3 = new byte[5];
System.out.println(zis.read(b3, 0, 5)); // 1
System.out.println(Arrays.toString(b3)); // [102, 0, 0, 0, 0]
}
}
long skip (long n)
final var bytes = deflate("aaa.txt", "aaaaaaaaaaaaaa");
System.out.println(bytes.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
final var uncompressed = zis.readAllBytes();
System.out.println(uncompressed.length); // 14
}
}
try (final var zis = new ZipInputStream(new ByteArrayInputStream(bytes))) {
final var entry = zis.getNextEntry();
if (entry != null) {
System.out.println(zis.skip(5)); // 5
final var uncompressed = zis.readAllBytes();
System.out.println(uncompressed.length); // 9
}
}
InflaterInputStreamで宣言されたメソッド
fill, mark, markSupported, read, reset
「Java API 使用例 : InflaterInputStream」をご参照ください。
FilterInputStreamで宣言されたメソッド
read
「Java API 使用例 : FilterInputStream」をご参照ください。
InputStreamで宣言されたメソッド
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
「Java API 使用例 : InputStream」をご参照ください。