Java : ZipFile - API Examples

ZipFile (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ZipFile methods.


Summary

This class is used to read entries from a zip file.

Class diagram

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 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());
        }
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //uncompressed : YYYYY
    //-- entry --
    //name : ccc.txt
    //uncompressed : ZZZZZ
}

Fields

static final int CENATT

Central directory (CEN) header internal file attributes field offset.

System.out.println(ZipFile.CENATT); // 36

static final int CENATX

Central directory (CEN) header external file attributes field offset.

System.out.println(ZipFile.CENATX); // 38

static final int CENCOM

Central directory (CEN) header comment length field offset.

System.out.println(ZipFile.CENCOM); // 32

static final int CENCRC

Central directory (CEN) header uncompressed file crc-32 value field offset.

System.out.println(ZipFile.CENCRC); // 16

static final int CENDSK

Central directory (CEN) header disk number start field offset.

System.out.println(ZipFile.CENDSK); // 34

static final int CENEXT

Central directory (CEN) header extra field length field offset.

System.out.println(ZipFile.CENEXT); // 30

static final int CENFLG

Central directory (CEN) header encrypt, decrypt flags field offset.

System.out.println(ZipFile.CENFLG); // 8

static final int CENHDR

Central directory (CEN) header size in bytes (including signature).

System.out.println(ZipFile.CENHDR); // 46

static final int CENHOW

Central directory (CEN) header compression method field offset.

System.out.println(ZipFile.CENHOW); // 10

static final int CENLEN

Central directory (CEN) header uncompressed size field offset.

System.out.println(ZipFile.CENLEN); // 24

static final int CENNAM

Central directory (CEN) header filename length field offset.

System.out.println(ZipFile.CENNAM); // 28

static final int CENOFF

Central directory (CEN) header LOC header offset field offset.

System.out.println(ZipFile.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(ZipFile.CENSIZ); // 20

static final int CENTIM

Central directory (CEN) header modification time field offset.

System.out.println(ZipFile.CENTIM); // 12

static final int CENVEM

Central directory (CEN) header version made by field offset.

System.out.println(ZipFile.CENVEM); // 4

static final int CENVER

Central directory (CEN) header version needed to extract field offset.

System.out.println(ZipFile.CENVER); // 6

static final int ENDCOM

End of central directory (END) header zip file comment length field offset.

System.out.println(ZipFile.ENDCOM); // 20

static final int ENDHDR

End of central directory (END) header size in bytes (including signature).

System.out.println(ZipFile.ENDHDR); // 22

static final int ENDOFF

End of central directory (END) header offset for the first CEN header field offset.

System.out.println(ZipFile.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(ZipFile.ENDSIZ); // 12

static final int ENDSUB

End of central directory (END) header number of entries on this disk field offset.

System.out.println(ZipFile.ENDSUB); // 8

static final int ENDTOT

End of central directory (END) header total number of entries field offset.

System.out.println(ZipFile.ENDTOT); // 10

static final int EXTCRC

Extra local (EXT) header uncompressed file crc-32 value field offset.

System.out.println(ZipFile.EXTCRC); // 4

static final int EXTHDR

Extra local (EXT) header size in bytes (including signature).

System.out.println(ZipFile.EXTHDR); // 16

static final int EXTLEN

Extra local (EXT) header uncompressed size field offset.

System.out.println(ZipFile.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(ZipFile.EXTSIZ); // 8

static final int LOCCRC

Local file (LOC) header uncompressed file crc-32 value field offset.

System.out.println(ZipFile.LOCCRC); // 14

static final int LOCEXT

Local file (LOC) header extra field length field offset.

System.out.println(ZipFile.LOCEXT); // 28

static final int LOCFLG

Local file (LOC) header general purpose bit flag field offset.

System.out.println(ZipFile.LOCFLG); // 6

static final int LOCHDR

Local file (LOC) header size in bytes (including signature).

System.out.println(ZipFile.LOCHDR); // 30

static final int LOCHOW

Local file (LOC) header compression method field offset.

System.out.println(ZipFile.LOCHOW); // 8

static final int LOCLEN

Local file (LOC) header uncompressed size field offset.

System.out.println(ZipFile.LOCLEN); // 22

static final int LOCNAM

Local file (LOC) header filename length field offset.

System.out.println(ZipFile.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(ZipFile.LOCSIZ); // 18

static final int LOCTIM

Local file (LOC) header modification time field offset.

System.out.println(ZipFile.LOCTIM); // 10

static final int LOCVER

Local file (LOC) header version needed to extract field offset.

System.out.println(ZipFile.LOCVER); // 4

static final int OPEN_DELETE

Mode flag to open a zip file and mark it for deletion.

System.out.println(ZipFile.OPEN_DELETE); // 4

static final int OPEN_READ

Mode flag to open a zip file for reading.

System.out.println(ZipFile.OPEN_READ); // 1

Constructors

ZipFile (File file)

Opens a ZIP file for reading given the specified File object.

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());
        }
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //uncompressed : YYYYY
}

ZipFile (File file, int mode)

Opens a new ZipFile to read from the specified File object in the specified 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());
        }
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //uncompressed : YYYYY
}

System.out.println(Files.exists(path)); // false

ZipFile (File file, int mode, Charset charset)

Opens a new ZipFile to read from the specified File object in the specified mode.

Please see :

ZipFile (File file, Charset charset)

Opens a ZIP file for reading given the specified File object.

Please see ZipFile(File file) for file parameter.

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());
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //-- entry --
    //name : ○△×.txt
}

ZipFile (String name)

Opens a zip file for reading.

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());
        }
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //uncompressed : YYYYY
}

ZipFile (String name, Charset charset)

Opens a zip file for reading.

Please see :

Methods

void close ()

Closes the ZIP 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"));
}

try (final var zipFile = new ZipFile(path.toFile())) {
    zipFile.stream().forEach(entry -> {
        System.out.println("-- entry --");
        System.out.println("name : " + entry.getName());
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
}

An example without 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());
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
} finally {
    zipFile.close();
}

Enumeration<? extends ZipEntry> entries ()

Returns an enumeration of the ZIP file 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());
    }

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //-- entry --
    //name : ccc.txt
}

String getComment ()

Returns the zip file comment, or null if none.

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)

Returns the zip file entry for the specified name, or null if not found.

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)

Returns an input stream for reading the contents of the specified zip file 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());
        }
    });

    // Result
    // ↓
    //-- entry --
    //name : bbb.txt
    //uncompressed : YYYYY
    //-- entry --
    //name : ccc.txt
    //uncompressed : ZZZZZ
}

String getName ()

Returns the path name of the ZIP file.

Please see getComment().

int size ()

Returns the number of entries in the ZIP 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.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());
    });

    // Result
    // ↓
    //entry size : 3
    //-- entry --
    //name : bbb.txt
    //-- entry --
    //name : ccc.txt
    //-- entry --
    //name : ddd.txt
}

Stream<? extends ZipEntry> stream ()

Returns an ordered Stream over the ZIP file entries.

Please see size().


Related posts

To top of page