Java : ZipEntry with Examples
ZipEntry (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ZipEntry methods.
Summary
This class is used to represent a ZIP file entry.
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
Central directory (CEN) header internal file attributes field offset.
System.out.println(ZipEntry.CENATT); // 36
static final int CENATX
Central directory (CEN) header external file attributes field offset.
System.out.println(ZipEntry.CENATX); // 38
static final int CENCOM
Central directory (CEN) header comment length field offset.
System.out.println(ZipEntry.CENCOM); // 32
static final int CENCRC
Central directory (CEN) header uncompressed file crc-32 value field offset.
System.out.println(ZipEntry.CENCRC); // 16
static final int CENDSK
Central directory (CEN) header disk number start field offset.
System.out.println(ZipEntry.CENDSK); // 34
static final int CENEXT
Central directory (CEN) header extra field length field offset.
System.out.println(ZipEntry.CENEXT); // 30
static final int CENFLG
Central directory (CEN) header encrypt, decrypt flags field offset.
System.out.println(ZipEntry.CENFLG); // 8
static final int CENHDR
Central directory (CEN) header size in bytes (including signature).
System.out.println(ZipEntry.CENHDR); // 46
static final int CENHOW
Central directory (CEN) header compression method field offset.
System.out.println(ZipEntry.CENHOW); // 10
static final int CENLEN
Central directory (CEN) header uncompressed size field offset.
System.out.println(ZipEntry.CENLEN); // 24
static final int CENNAM
Central directory (CEN) header filename length field offset.
System.out.println(ZipEntry.CENNAM); // 28
static final int CENOFF
Central directory (CEN) header LOC header offset field offset.
System.out.println(ZipEntry.CENOFF); // 42
static final long CENSIG
Central directory (CEN) header signature.
System.out.printf("%#x", ZipEntry.CENSIG); // 0x2014b50
static final int CENSIZ
Central directory (CEN) header compressed size field offset.
System.out.println(ZipEntry.CENSIZ); // 20
static final int CENTIM
Central directory (CEN) header modification time field offset.
System.out.println(ZipEntry.CENTIM); // 12
static final int CENVEM
Central directory (CEN) header version made by field offset.
System.out.println(ZipEntry.CENVEM); // 4
static final int CENVER
Central directory (CEN) header version needed to extract field offset.
System.out.println(ZipEntry.CENVER); // 6
static final int DEFLATED
Compression method for compressed (deflated) entries.
System.out.println(ZipEntry.DEFLATED); // 8
static final int ENDCOM
End of central directory (END) header zip file comment length field offset.
System.out.println(ZipEntry.ENDCOM); // 20
static final int ENDHDR
End of central directory (END) header size in bytes (including signature).
System.out.println(ZipEntry.ENDHDR); // 22
static final int ENDOFF
End of central directory (END) header offset for the first CEN header field offset.
System.out.println(ZipEntry.ENDOFF); // 16
static final long ENDSIG
End of central directory (END) header signature.
System.out.printf("%#x", ZipEntry.ENDSIG); // 0x6054b50
static final int ENDSIZ
End of central directory (END) header central directory size in bytes field offset.
System.out.println(ZipEntry.ENDSIZ); // 12
static final int ENDSUB
End of central directory (END) header number of entries on this disk field offset.
System.out.println(ZipEntry.ENDSUB); // 8
static final int ENDTOT
End of central directory (END) header total number of entries field offset.
System.out.println(ZipEntry.ENDTOT); // 10
static final int EXTCRC
Extra local (EXT) header uncompressed file crc-32 value field offset.
System.out.println(ZipEntry.EXTCRC); // 4
static final int EXTHDR
Extra local (EXT) header size in bytes (including signature).
System.out.println(ZipEntry.EXTHDR); // 16
static final int EXTLEN
Extra local (EXT) header uncompressed size field offset.
System.out.println(ZipEntry.EXTLEN); // 12
static final long EXTSIG
Extra local (EXT) header signature.
System.out.printf("%#x", ZipEntry.EXTSIG); // 0x8074b50
static final int EXTSIZ
Extra local (EXT) header compressed size field offset.
System.out.println(ZipEntry.EXTSIZ); // 8
static final int LOCCRC
Local file (LOC) header uncompressed file crc-32 value field offset.
System.out.println(ZipEntry.LOCCRC); // 14
static final int LOCEXT
Local file (LOC) header extra field length field offset.
System.out.println(ZipEntry.LOCEXT); // 28
static final int LOCFLG
Local file (LOC) header general purpose bit flag field offset.
System.out.println(ZipEntry.LOCFLG); // 6
static final int LOCHDR
Local file (LOC) header size in bytes (including signature).
System.out.println(ZipEntry.LOCHDR); // 30
static final int LOCHOW
Local file (LOC) header compression method field offset.
System.out.println(ZipEntry.LOCHOW); // 8
static final int LOCLEN
Local file (LOC) header uncompressed size field offset.
System.out.println(ZipEntry.LOCLEN); // 22
static final int LOCNAM
Local file (LOC) header filename length field offset.
System.out.println(ZipEntry.LOCNAM); // 26
static final long LOCSIG
Local file (LOC) header signature.
System.out.printf("%#x", ZipEntry.LOCSIG); // 0x4034b50
static final int LOCSIZ
Local file (LOC) header compressed size field offset.
System.out.println(ZipEntry.LOCSIZ); // 18
static final int LOCTIM
Local file (LOC) header modification time field offset.
System.out.println(ZipEntry.LOCTIM); // 10
static final int LOCVER
Local file (LOC) header version needed to extract field offset.
System.out.println(ZipEntry.LOCVER); // 4
static final int STORED
Compression method for uncompressed entries.
System.out.println(ZipEntry.STORED); // 0
Constructors
ZipEntry (String name)
Creates a new zip entry with the specified 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)
Creates a new zip entry with fields taken from the specified zip entry.
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 ()
Returns a copy of this entry.
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 ()
Returns the comment string for the 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))) {
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 ()
Returns the size of the compressed entry data.
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 ()
Returns the CRC-32 checksum of the uncompressed entry data.
Please see getCompressedSize().
FileTime getCreationTime ()
Returns the creation time of the 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))) {
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 ()
Returns the extra field data for the 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))) {
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 ()
Returns the last access time of the 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))) {
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 ()
Returns the last modification time of the entry.
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 ()
Returns the compression method of the 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))) {
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 ()
Returns the name of the entry.
Please see ZipEntry(String name).
long getSize ()
Returns the uncompressed size of the entry data.
Please see getCompressedSize().
long getTime ()
Returns the last modification time of the entry.
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 ()
Returns the last modification time of the entry in local date-time.
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 ()
Returns the hash code value for this entry.
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 ()
Returns true if this is a directory entry.
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)
Sets the optional comment string for the entry.
Please see getComment().
void setCompressedSize (long csize)
Sets the size of the compressed entry data.
Please see getMethod().
void setCrc (long crc)
Sets the CRC-32 checksum of the uncompressed entry data.
Please see getMethod().
ZipEntry setCreationTime (FileTime time)
Sets the creation time of the entry.
Please see getCreationTime().
void setExtra (byte[] extra)
Sets the optional extra field data for the entry.
Please see getExtra().
ZipEntry setLastAccessTime (FileTime time)
Sets the last access time of the entry.
Please see getLastAccessTime().
ZipEntry setLastModifiedTime (FileTime time)
Sets the last modification time of the entry.
Please see getLastModifiedTime().
void setMethod (int method)
Sets the compression method for the entry.
Please see getMethod().
void setSize (long size)
Sets the uncompressed size of the entry data.
Please see getMethod().
void setTime (long time)
Sets the last modification time of the entry.
Please see getTime().
void setTimeLocal (LocalDateTime time)
Sets the last modification time of the entry in local date-time.
Please see getTimeLocal().
String toString ()
Returns a string representation of the ZIP entry.
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