Java : ZipOutputStream with Examples
ZipOutputStream (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ZipOutputStream methods.
Summary
// --------
// 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(ZipOutputStream.CENATT); // 36
static final int CENATX
System.out.println(ZipOutputStream.CENATX); // 38
static final int CENCOM
System.out.println(ZipOutputStream.CENCOM); // 32
static final int CENCRC
System.out.println(ZipOutputStream.CENCRC); // 16
static final int CENDSK
System.out.println(ZipOutputStream.CENDSK); // 34
static final int CENEXT
System.out.println(ZipOutputStream.CENEXT); // 30
static final int CENFLG
System.out.println(ZipOutputStream.CENFLG); // 8
static final int CENHDR
System.out.println(ZipOutputStream.CENHDR); // 46
static final int CENHOW
System.out.println(ZipOutputStream.CENHOW); // 10
static final int CENLEN
System.out.println(ZipOutputStream.CENLEN); // 24
static final int CENNAM
System.out.println(ZipOutputStream.CENNAM); // 28
static final int CENOFF
System.out.println(ZipOutputStream.CENOFF); // 42
static final long CENSIG
System.out.printf("%#x", ZipOutputStream.CENSIG); // 0x2014b50
static final int CENSIZ
System.out.println(ZipOutputStream.CENSIZ); // 20
static final int CENTIM
System.out.println(ZipOutputStream.CENTIM); // 12
static final int CENVEM
System.out.println(ZipOutputStream.CENVEM); // 4
static final int CENVER
System.out.println(ZipOutputStream.CENVER); // 6
static final int DEFLATED
System.out.println(ZipOutputStream.DEFLATED); // 8
static final int ENDCOM
System.out.println(ZipOutputStream.ENDCOM); // 20
static final int ENDHDR
System.out.println(ZipOutputStream.ENDHDR); // 22
static final int ENDOFF
System.out.println(ZipOutputStream.ENDOFF); // 16
static final long ENDSIG
System.out.printf("%#x", ZipOutputStream.ENDSIG); // 0x6054b50
static final int ENDSIZ
System.out.println(ZipOutputStream.ENDSIZ); // 12
static final int ENDSUB
System.out.println(ZipOutputStream.ENDSUB); // 8
static final int ENDTOT
System.out.println(ZipOutputStream.ENDTOT); // 10
static final int EXTCRC
System.out.println(ZipOutputStream.EXTCRC); // 4
static final int EXTHDR
System.out.println(ZipOutputStream.EXTHDR); // 16
static final int EXTLEN
System.out.println(ZipOutputStream.EXTLEN); // 12
static final long EXTSIG
System.out.printf("%#x", ZipOutputStream.EXTSIG); // 0x8074b50
static final int EXTSIZ
System.out.println(ZipOutputStream.EXTSIZ); // 8
static final int LOCCRC
System.out.println(ZipOutputStream.LOCCRC); // 14
static final int LOCEXT
System.out.println(ZipOutputStream.LOCEXT); // 28
static final int LOCFLG
System.out.println(ZipOutputStream.LOCFLG); // 6
static final int LOCHDR
System.out.println(ZipOutputStream.LOCHDR); // 30
static final int LOCHOW
System.out.println(ZipOutputStream.LOCHOW); // 8
static final int LOCLEN
System.out.println(ZipOutputStream.LOCLEN); // 22
static final int LOCNAM
System.out.println(ZipOutputStream.LOCNAM); // 26
static final long LOCSIG
System.out.printf("%#x", ZipOutputStream.LOCSIG); // 0x4034b50
static final int LOCSIZ
System.out.println(ZipOutputStream.LOCSIZ); // 18
static final int LOCTIM
System.out.println(ZipOutputStream.LOCTIM); // 10
static final int LOCVER
System.out.println(ZipOutputStream.LOCVER); // 4
static final int STORED
System.out.println(ZipOutputStream.STORED); // 0
Fields declared in DeflaterOutputStream
Fields declared in FilterOutputStream
Constructors
ZipOutputStream (OutputStream out)
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)) {
zos.putNextEntry(new ZipEntry("aaa.txt"));
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));
ZipOutputStream (OutputStream out, Charset charset)
Please see ZipOutputStream(OutputStream out) for out parameter.
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
}
}
Methods
void close ()
final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip
try (final var zos = new ZipOutputStream(Files.newOutputStream(file))) {
zos.putNextEntry(new ZipEntry("xxx.txt"));
zos.write("aaaaa".getBytes());
}
final var bytes = Files.readAllBytes(file);
System.out.println(bytes.length); // 133
An example without try-with-resources.
final var file = Path.of("R:", "java-work", "aaa.zip");
System.out.println(file); // R:\java-work\aaa.zip
final var zos = new ZipOutputStream(Files.newOutputStream(file));
try {
zos.putNextEntry(new ZipEntry("xxx.txt"));
zos.write("aaaaa".getBytes());
} finally {
zos.close();
}
final var bytes = Files.readAllBytes(file);
System.out.println(bytes.length); // 133
void closeEntry ()
final var input = "aaaaaaaaaa".getBytes();
// [97, 97, 97, 97, 97, 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");
zos.putNextEntry(entry);
zos.write(input);
System.out.println(entry.getSize()); // -1
System.out.println(entry.getCompressedSize()); // -1
System.out.println(out.toByteArray().length); // 37
zos.closeEntry();
System.out.println(entry.getSize()); // 10
System.out.println(entry.getCompressedSize()); // 5
System.out.println(out.toByteArray().length); // 58
}
final var compressed = out.toByteArray();
System.out.println(compressed.length); // 133
void finish ()
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)) {
zos.putNextEntry(new ZipEntry("aaa.txt"));
zos.write(input);
zos.finish();
// [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 59, -102, 63, 86, 0, 0, 0, ...]
System.out.println(Arrays.toString(out.toByteArray()));
// The stream is not closed.
out.write(1);
out.write(2);
out.write(3);
}
final var compressed = out.toByteArray();
// [80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 59, -102, 63, 86, 0, 0, 0, ..., 1, 2, 3]
System.out.println(Arrays.toString(compressed));
void putNextEntry (ZipEntry e)
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());
}
// --------
// 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
}
void setComment (String comment)
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!");
zos.putNextEntry(new ZipEntry("bbb.txt"));
zos.write("YYYYY".getBytes());
}
try (final var zipFile = new ZipFile(path.toFile())) {
System.out.println(zipFile.getComment()); // Comment!
}
void setLevel (int level)
final var input = "aaaaaaaaaa".getBytes();
System.out.println(input.length); // 10
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
final var entry1 = new ZipEntry("aaa.txt");
zos.putNextEntry(entry1);
zos.setLevel(Deflater.BEST_COMPRESSION);
zos.write(input);
zos.closeEntry();
System.out.println(entry1.getCompressedSize()); // 5
final var entry2 = new ZipEntry("bbb.txt");
zos.putNextEntry(entry2);
zos.setLevel(Deflater.NO_COMPRESSION);
zos.write(input);
zos.closeEntry();
System.out.println(entry2.getCompressedSize()); // 15
}
System.out.println(out.toByteArray().length); // 254
void setMethod (int method)
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
zos.setMethod(ZipEntry.STORED);
final var input = "aaaaaaaaaa".getBytes();
System.out.println(input.length); // 10
final var crc = new CRC32();
crc.update(input);
final var entry = new ZipEntry("aaa.txt");
entry.setCrc(crc.getValue());
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
}
System.out.println(out.toByteArray().length); // 122
void write (byte[] b, int off, int len)
final byte[] input = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(input)); // [10, 20, 30, 40, 50]
{
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
zos.putNextEntry(new ZipEntry("aaa.txt"));
zos.write(input, 0, 5);
}
final var compressed = out.toByteArray();
System.out.println(compressed.length); // 135
try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed))) {
final var entry = zis.getNextEntry();
if (entry != null) {
final var uncompressed = zis.readAllBytes();
// [10, 20, 30, 40, 50]
System.out.println(Arrays.toString(uncompressed));
}
}
}
{
final var out = new ByteArrayOutputStream();
try (final var zos = new ZipOutputStream(out)) {
zos.putNextEntry(new ZipEntry("aaa.txt"));
zos.write(input, 1, 3);
}
final var compressed = out.toByteArray();
System.out.println(compressed.length); // 133
try (final var zis = new ZipInputStream(new ByteArrayInputStream(compressed))) {
final var entry = zis.getNextEntry();
if (entry != null) {
final var uncompressed = zis.readAllBytes();
// [20, 30, 40]
System.out.println(Arrays.toString(uncompressed));
}
}
}