Java : ZipEntry with Examples
ZipEntry (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ZipEntry methods.
Summary
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
// --------
// Compression
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
// --------
// Decompression
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
}
Fields
static final int CENATT
System.out.println(ZipEntry.CENATT); // 36
static final int CENATX
System.out.println(ZipEntry.CENATX); // 38
static final int CENCOM
System.out.println(ZipEntry.CENCOM); // 32
static final int CENCRC
System.out.println(ZipEntry.CENCRC); // 16
static final int CENDSK
System.out.println(ZipEntry.CENDSK); // 34
static final int CENEXT
System.out.println(ZipEntry.CENEXT); // 30
static final int CENFLG
System.out.println(ZipEntry.CENFLG); // 8
static final int CENHDR
System.out.println(ZipEntry.CENHDR); // 46
static final int CENHOW
System.out.println(ZipEntry.CENHOW); // 10
static final int CENLEN
System.out.println(ZipEntry.CENLEN); // 24
static final int CENNAM
System.out.println(ZipEntry.CENNAM); // 28
static final int CENOFF
System.out.println(ZipEntry.CENOFF); // 42
static final long CENSIG
System.out.printf("%#x", ZipEntry.CENSIG); // 0x2014b50
static final int CENSIZ
System.out.println(ZipEntry.CENSIZ); // 20
static final int CENTIM
System.out.println(ZipEntry.CENTIM); // 12
static final int CENVEM
System.out.println(ZipEntry.CENVEM); // 4
static final int CENVER
System.out.println(ZipEntry.CENVER); // 6
static final int DEFLATED
System.out.println(ZipEntry.DEFLATED); // 8
static final int ENDCOM
System.out.println(ZipEntry.ENDCOM); // 20
static final int ENDHDR
System.out.println(ZipEntry.ENDHDR); // 22
static final int ENDOFF
System.out.println(ZipEntry.ENDOFF); // 16
static final long ENDSIG
System.out.printf("%#x", ZipEntry.ENDSIG); // 0x6054b50
static final int ENDSIZ
System.out.println(ZipEntry.ENDSIZ); // 12
static final int ENDSUB
System.out.println(ZipEntry.ENDSUB); // 8
static final int ENDTOT
System.out.println(ZipEntry.ENDTOT); // 10
static final int EXTCRC
System.out.println(ZipEntry.EXTCRC); // 4
static final int EXTHDR
System.out.println(ZipEntry.EXTHDR); // 16
static final int EXTLEN
System.out.println(ZipEntry.EXTLEN); // 12
static final long EXTSIG
System.out.printf("%#x", ZipEntry.EXTSIG); // 0x8074b50
static final int EXTSIZ
System.out.println(ZipEntry.EXTSIZ); // 8
static final int LOCCRC
System.out.println(ZipEntry.LOCCRC); // 14
static final int LOCEXT
System.out.println(ZipEntry.LOCEXT); // 28
static final int LOCFLG
System.out.println(ZipEntry.LOCFLG); // 6
static final int LOCHDR
System.out.println(ZipEntry.LOCHDR); // 30
static final int LOCHOW
System.out.println(ZipEntry.LOCHOW); // 8
static final int LOCLEN
System.out.println(ZipEntry.LOCLEN); // 22
static final int LOCNAM
System.out.println(ZipEntry.LOCNAM); // 26
static final long LOCSIG
System.out.printf("%#x", ZipEntry.LOCSIG); // 0x4034b50
static final int LOCSIZ
System.out.println(ZipEntry.LOCSIZ); // 18
static final int LOCTIM
System.out.println(ZipEntry.LOCTIM); // 10
static final int LOCVER
System.out.println(ZipEntry.LOCVER); // 4
static final int STORED
System.out.println(ZipEntry.STORED); // 0
Constructors
ZipEntry (String name)
final var input = "aaaaa".getBytes();
// [97, 97, 97, 97, 97]
System.out.println(Arrays.toString(input));
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
final var entry = new ZipEntry("aaa.txt");
System.out.println(entry.getName()); // aaa.txt
zos.putNextEntry(entry);
zos.write(input);
}
final var compressed = out.toByteArray();
System.out.println(compressed.length); // 133
// [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, -106, -118, 63, 86, 0, 0, 0, 0, ...]
System.out.println(Arrays.toString(compressed));
ZipEntry (ZipEntry e)
final var entry1 = new ZipEntry("aaa.txt");
entry1.setComment("comment!");
final var fileTime = FileTime.from(
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant());
entry1.setCreationTime(fileTime);
System.out.println(entry1.getName()); // aaa.txt
System.out.println(entry1.getComment()); // comment!
System.out.println(entry1.getCreationTime()); // 2100-01-01T00:00:00Z
final var entry2 = new ZipEntry(entry1);
System.out.println(entry2.getName()); // aaa.txt
System.out.println(entry2.getComment()); // comment!
System.out.println(entry2.getCreationTime()); // 2100-01-01T00:00:00Z
Methods
Object clone ()
final var entry = new ZipEntry("aaa.txt");
entry.setComment("comment!");
final var fileTime = FileTime.from(
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant());
entry.setCreationTime(fileTime);
System.out.println(entry.getName()); // aaa.txt
System.out.println(entry.getComment()); // comment!
System.out.println(entry.getCreationTime()); // 2100-01-01T00:00:00Z
if (entry.clone() instanceof ZipEntry cloned) {
System.out.println(cloned.getName()); // aaa.txt
System.out.println(cloned.getComment()); // comment!
System.out.println(cloned.getCreationTime()); // 2100-01-01T00:00:00Z
}
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))) {
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);
}
System.out.println(Files.size(path)); // 246
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("comment : " + entry.getComment());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//comment : BBB!
//-- entry --
//name : ccc.txt
//comment : CCC!
}
long getCompressedSize ()
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 input = "aaaaaaaaaa".getBytes();
System.out.println(input.length); // 10
final var entry = new ZipEntry("aaa.txt");
System.out.println(entry.getSize()); // -1
System.out.println(entry.getCompressedSize()); // -1
System.out.println(entry.getCrc()); // -1
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
System.out.println(entry.getSize()); // 10
System.out.println(entry.getCompressedSize()); // 5
System.out.println(entry.getCrc()); // 1276235248
}
long getCrc ()
Please see getCompressedSize().
FileTime getCreationTime ()
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 fileTime = FileTime.from(
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant());
System.out.println(fileTime); // 2100-01-01T00:00:00Z
final var entry = new ZipEntry("bbb.txt");
entry.setCreationTime(fileTime);
zos.putNextEntry(entry);
}
System.out.println(Files.size(path)); // 202
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("creation time : " + entry.getCreationTime());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//creation time : 2100-01-01T00:00:00Z
}
byte[] getExtra ()
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 byte[] extra = {1, 2, 3, 4, 5};
final var entry = new ZipEntry("bbb.txt");
entry.setExtra(extra);
zos.putNextEntry(entry);
}
System.out.println(Files.size(path)); // 140
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("extra : " + Arrays.toString(entry.getExtra()));
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//extra : [1, 2, 3, 4, 5]
}
FileTime getLastAccessTime ()
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 fileTime = FileTime.from(
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant());
System.out.println(fileTime); // 2100-01-01T00:00:00Z
final var entry = new ZipEntry("bbb.txt");
entry.setLastAccessTime(fileTime);
zos.putNextEntry(entry);
}
System.out.println(Files.size(path)); // 202
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("last access time : " + entry.getLastAccessTime());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//last access time : 2100-01-01T00:00:00Z
}
FileTime getLastModifiedTime ()
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
System.out.println(Instant.now()); // 2023-02-15T09:42:49.865711800Z
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
final var entry1 = new ZipEntry("bbb.txt");
zos.putNextEntry(entry1);
final var fileTime = FileTime.from(
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant());
System.out.println(fileTime); // 2100-01-01T00:00:00Z
final var entry2 = new ZipEntry("ccc.txt");
entry2.setLastModifiedTime(fileTime);
zos.putNextEntry(entry2);
}
System.out.println(Files.size(path)); // 310
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("last modified time : " + entry.getLastModifiedTime());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//last modified time : 2023-02-15T09:42:48Z
//-- entry --
//name : ccc.txt
//last modified time : 2100-01-01T00:00:00Z
}
int getMethod ()
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 input = "aaaaaaaaaa".getBytes();
System.out.println(input.length); // 10
final var crc = new CRC32();
crc.update(input);
System.out.println(crc.getValue()); // 1276235248
final var entry1 = new ZipEntry("bbb.txt");
entry1.setMethod(ZipEntry.STORED);
entry1.setCrc(crc.getValue());
entry1.setSize(input.length);
zos.putNextEntry(entry1);
zos.write(input);
final var entry2 = new ZipEntry("ccc.txt");
entry2.setMethod(ZipEntry.STORED);
entry2.setCrc(crc.getValue());
entry2.setCompressedSize(input.length);
zos.putNextEntry(entry2);
zos.write(input);
}
System.out.println(Files.size(path)); // 222
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("sorted : " + (entry.getMethod() == ZipEntry.STORED));
System.out.println("crc : " + entry.getCrc());
System.out.println("size : " + entry.getSize());
System.out.println("compressed size : " + entry.getCompressedSize());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//sorted : true
//crc : 1276235248
//size : 10
//compressed size : 10
//-- entry --
//name : ccc.txt
//sorted : true
//crc : 1276235248
//size : 10
//compressed size : 10
}
String getName ()
Please see ZipEntry(String name).
long getSize ()
Please see getCompressedSize().
long getTime ()
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
System.out.println(Instant.now()); // 2023-02-15T10:04:06.918957400Z
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
final var entry1 = new ZipEntry("bbb.txt");
zos.putNextEntry(entry1);
final var instant = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
System.out.println(instant); // 2100-01-01T00:00:00Z
final var entry2 = new ZipEntry("ccc.txt");
entry2.setTime(instant.toEpochMilli());
zos.putNextEntry(entry2);
}
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("time : " + Instant.ofEpochMilli(entry.getTime()));
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//time : 2023-02-15T10:04:06Z
//-- entry --
//name : ccc.txt
//time : 2100-01-01T00:00:00Z
}
LocalDateTime getTimeLocal ()
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
System.out.println(LocalDateTime.now()); // 2023-02-15T19:14:25.556582500
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
final var entry1 = new ZipEntry("bbb.txt");
zos.putNextEntry(entry1);
final var time = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(time); // 2100-01-01T00:00
final var entry2 = new ZipEntry("ccc.txt");
entry2.setTimeLocal(time);
zos.putNextEntry(entry2);
}
System.out.println(Files.size(path)); // 238
try (final var zipFile = new ZipFile(path.toFile())) {
zipFile.stream().forEach(entry -> {
System.out.println("-- entry --");
System.out.println("name : " + entry.getName());
System.out.println("time : " + entry.getTimeLocal());
});
// Result
// ↓
//-- entry --
//name : bbb.txt
//time : 2023-02-15T19:14:24
//-- entry --
//name : ccc.txt
//time : 2100-01-01T00:00
}
int hashCode ()
final var entry1 = new ZipEntry("aaa.txt");
System.out.println(entry1.hashCode()); // -1238361277
final var entry2 = new ZipEntry("bbb.txt");
System.out.println(entry2.hashCode()); // -321304924
boolean isDirectory ()
final var path = Path.of("R:", "java-work", "aaa.zip");
System.out.println(path); // R:\java-work\aaa.zip
// --------
// Compression
try (final var zos = new ZipOutputStream(Files.newOutputStream(path))) {
final var entry = new ZipEntry("dir/");
zos.putNextEntry(entry);
System.out.println(entry.isDirectory()); // true
zos.putNextEntry(new ZipEntry("dir/bbb.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
//dir
//aaa.zip
//
//PS R:\java-work> cat .\dir\bbb.txt
//ZZZZZ
// --------
// Decompression
try (final var zis = new ZipInputStream(Files.newInputStream(path))) {
final var entry1 = zis.getNextEntry();
if (entry1 != null) {
System.out.println(entry1.getName()); // dir/
System.out.println(entry1.isDirectory()); // true
}
final var entry2 = zis.getNextEntry();
if (entry2 != null) {
System.out.println(entry2.getName()); // dir/bbb.txt
System.out.println(entry2.isDirectory()); // false
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
}
void setComment (String comment)
Please see getComment().
void setCompressedSize (long csize)
Please see getMethod().
void setCrc (long crc)
Please see getMethod().
ZipEntry setCreationTime (FileTime time)
Please see getCreationTime().
void setExtra (byte[] extra)
Please see getExtra().
ZipEntry setLastAccessTime (FileTime time)
Please see getLastAccessTime().
ZipEntry setLastModifiedTime (FileTime time)
Please see getLastModifiedTime().
void setMethod (int method)
Please see getMethod().
void setSize (long size)
Please see getMethod().
void setTime (long time)
Please see getTime().
void setTimeLocal (LocalDateTime time)
Please see getTimeLocal().
String toString ()
final var entry1 = new ZipEntry("aaa.txt");
final var str1 = entry1.toString();
System.out.println(str1); // aaa.txt
final var entry2 = new ZipEntry("bbb.txt");
final var str2 = entry2.toString();
System.out.println(str2); // bbb.txt