Java : ZipFile - API使用例
ZipFile (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ZipFile クラスを使うと、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 zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
try (final var is = zipFile.getInputStream(entry)) {
final var uncompressed = is.readAllBytes();
System.out.println("uncompressed : " + new String(uncompressed));
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//uncompressed : YYYYY
//-- entry --
//name : ccc.txt
//uncompressed : ZZZZZ
}
フィールド
static final int CENATT
System.out.println(ZipFile.CENATT); // 36
static final int CENATX
System.out.println(ZipFile.CENATX); // 38
static final int CENCOM
System.out.println(ZipFile.CENCOM); // 32
static final int CENCRC
System.out.println(ZipFile.CENCRC); // 16
static final int CENDSK
System.out.println(ZipFile.CENDSK); // 34
static final int CENEXT
System.out.println(ZipFile.CENEXT); // 30
static final int CENFLG
System.out.println(ZipFile.CENFLG); // 8
static final int CENHDR
System.out.println(ZipFile.CENHDR); // 46
static final int CENHOW
System.out.println(ZipFile.CENHOW); // 10
static final int CENLEN
System.out.println(ZipFile.CENLEN); // 24
static final int CENNAM
System.out.println(ZipFile.CENNAM); // 28
static final int CENOFF
System.out.println(ZipFile.CENOFF); // 42
static final long CENSIG
System.out.printf("%#x", ZipEntry.CENSIG); // 0x2014b50
static final int CENSIZ
System.out.println(ZipFile.CENSIZ); // 20
static final int CENTIM
System.out.println(ZipFile.CENTIM); // 12
static final int CENVEM
System.out.println(ZipFile.CENVEM); // 4
static final int CENVER
System.out.println(ZipFile.CENVER); // 6
static final int ENDCOM
System.out.println(ZipFile.ENDCOM); // 20
static final int ENDHDR
System.out.println(ZipFile.ENDHDR); // 22
static final int ENDOFF
System.out.println(ZipFile.ENDOFF); // 16
static final long ENDSIG
System.out.printf("%#x", ZipEntry.ENDSIG); // 0x6054b50
static final int ENDSIZ
System.out.println(ZipFile.ENDSIZ); // 12
static final int ENDSUB
System.out.println(ZipFile.ENDSUB); // 8
static final int ENDTOT
System.out.println(ZipFile.ENDTOT); // 10
static final int EXTCRC
System.out.println(ZipFile.EXTCRC); // 4
static final int EXTHDR
System.out.println(ZipFile.EXTHDR); // 16
static final int EXTLEN
System.out.println(ZipFile.EXTLEN); // 12
static final long EXTSIG
System.out.printf("%#x", ZipEntry.EXTSIG); // 0x8074b50
static final int EXTSIZ
System.out.println(ZipFile.EXTSIZ); // 8
static final int LOCCRC
System.out.println(ZipFile.LOCCRC); // 14
static final int LOCEXT
System.out.println(ZipFile.LOCEXT); // 28
static final int LOCFLG
System.out.println(ZipFile.LOCFLG); // 6
static final int LOCHDR
System.out.println(ZipFile.LOCHDR); // 30
static final int LOCHOW
System.out.println(ZipFile.LOCHOW); // 8
static final int LOCLEN
System.out.println(ZipFile.LOCLEN); // 22
static final int LOCNAM
System.out.println(ZipFile.LOCNAM); // 26
static final long LOCSIG
System.out.printf("%#x", ZipEntry.LOCSIG); // 0x4034b50
static final int LOCSIZ
System.out.println(ZipFile.LOCSIZ); // 18
static final int LOCTIM
System.out.println(ZipFile.LOCTIM); // 10
static final int LOCVER
System.out.println(ZipFile.LOCVER); // 4
static final int OPEN_DELETE
System.out.println(ZipFile.OPEN_DELETE); // 4
static final int OPEN_READ
System.out.println(ZipFile.OPEN_READ); // 1
コンストラクタ
ZipFile (File file)
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());
}
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
try (final var is = zipFile.getInputStream(entry)) {
final var uncompressed = is.readAllBytes();
System.out.println("uncompressed : " + new String(uncompressed));
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//uncompressed : YYYYY
}
ZipFile (File file, int mode)
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());
}
System.out.println(Files.exists(path)); // true
System.out.println(Files.size(path)); // 133
final int mode = ZipFile.OPEN_READ | ZipFile.OPEN_DELETE;
try (final var zipFile = new ZipFile(path.toFile(), mode)) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
try (final var is = zipFile.getInputStream(entry)) {
final var uncompressed = is.readAllBytes();
System.out.println("uncompressed : " + new String(uncompressed));
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//uncompressed : YYYYY
}
System.out.println(Files.exists(path)); // false
ZipFile (File file, int mode, Charset charset)
file, mode パラメータについては ZipFile(File file, int mode) の使用例をご参照ください。
charset パラメータについては ZipFile(File file, Charset charset) の使用例をご参照ください。
ZipFile (File file, Charset charset)
file パラメータについては ZipFile(File file) の使用例をご参照ください。
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
final var charset = Charset.forName("Shift_JIS");
try (final var zos = new ZipOutputStream(Files.newOutputStream(path), charset)) {
zos.putNextEntry(new ZipEntry("bbb.txt"));
zos.putNextEntry(new ZipEntry("○△×.txt"));
}
try (final var zipFile = new ZipFile(path.toFile(), charset)) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//-- entry --
//name : ○△×.txt
}
ZipFile (String name)
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());
}
final var name = path.toString();
System.out.println(name); // R:\java-work\aaa.zip
try (final var zipFile = new ZipFile(name)) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
try (final var is = zipFile.getInputStream(entry)) {
final var uncompressed = is.readAllBytes();
System.out.println("uncompressed : " + new String(uncompressed));
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//uncompressed : YYYYY
}
ZipFile (String name, Charset charset)
name パラメータについては ZipFile(String name) の使用例をご参照ください。
charset パラメータについては ZipFile(File file, Charset charset) の使用例をご参照ください。
メソッド
void close ()
可能であれば try-with-resources文 を使うことをおすすめします。
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"));
}
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
}
try-with-resources文を使わない例です。
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"));
}
final var zipFile = new ZipFile(path.toFile());
try {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
} finally {
zipFile.close();
}
Enumeration<? extends ZipEntry> entries ()
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.putNextEntry(new ZipEntry("ccc.txt"));
}
try (final var zipFile = new ZipFile(path.toFile())) {
final var entries = zipFile.entries();
while (entries.hasMoreElements()) {
System.out.println("-- entry --");
final var entry = entries.nextElement();
System.out.println("name : " + entry.getName());
}
// 結果
// ↓
//-- entry --
//name : bbb.txt
//-- entry --
//name : ccc.txt
}
String getComment ()
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.setComment("comment!");
}
try (final var zipFile = new ZipFile(path.toFile())) {
System.out.println(zipFile.getName()); // R:\java-work\aaa.zip
System.out.println(zipFile.getComment()); // comment!
}
ZipEntry getEntry (String name)
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))) {
final var entry1 = new ZipEntry("bbb.txt");
entry1.setComment("BBB!");
zos.putNextEntry(entry1);
final var entry2 = new ZipEntry("ccc.txt");
entry2.setComment("CCC!");
zos.putNextEntry(entry2);
}
try (final var zipFile = new ZipFile(path.toFile())) {
final var entry1 = zipFile.getEntry("bbb.txt");
if (entry1 != null) {
System.out.println(entry1.getName()); // bbb.txt
System.out.println(entry1.getComment()); // BBB!
}
final var entry2 = zipFile.getEntry("ccc.txt");
if (entry2 != null) {
System.out.println(entry2.getName()); // ccc.txt
System.out.println(entry2.getComment()); // CCC!
}
}
InputStream getInputStream (ZipEntry entry)
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 zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
try (final var is = zipFile.getInputStream(entry)) {
final var uncompressed = is.readAllBytes();
System.out.println("uncompressed : " + new String(uncompressed));
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
// 結果
// ↓
//-- entry --
//name : bbb.txt
//uncompressed : YYYYY
//-- entry --
//name : ccc.txt
//uncompressed : ZZZZZ
}
String getName ()
このメソッドの使用例は、getComment() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
int size ()
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.putNextEntry(new ZipEntry("ccc.txt"));
zos.putNextEntry(new ZipEntry("ddd.txt"));
}
try (final var zipFile = new ZipFile(path.toFile())) {
System.out.println("entry size : " + zipFile.size());
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
});
// 結果
// ↓
//entry size : 3
//-- entry --
//name : bbb.txt
//-- entry --
//name : ccc.txt
//-- entry --
//name : ddd.txt
}
Stream<? extends ZipEntry> stream ()
このメソッドの使用例は、size() にまとめて記載しました。
そちらのAPI使用例をご参照ください。