Closeable
, AutoCloseable
JarFile
public class ZipFile extends Object implements Closeable
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
ZipFile
, the close()
method should be called explicitly or by try-with-resources. Subclasses are responsible for the cleanup of resources acquired by the subclass. Subclasses that override Object.finalize()
in order to perform cleanup should be modified to use alternative cleanup mechanisms such as Cleaner
and remove the overriding finalize
method.Modifier and Type | Field | Description |
---|---|---|
static final int |
CENATT |
Central directory (CEN) header internal file attributes field offset. |
static final int |
CENATX |
Central directory (CEN) header external file attributes field offset. |
static final int |
CENCOM |
Central directory (CEN) header comment length field offset. |
static final int |
CENCRC |
Central directory (CEN) header uncompressed file crc-32 value field offset. |
static final int |
CENDSK |
Central directory (CEN) header disk number start field offset. |
static final int |
CENEXT |
Central directory (CEN) header extra field length field offset. |
static final int |
CENFLG |
Central directory (CEN) header encrypt, decrypt flags field offset. |
static final int |
CENHDR |
Central directory (CEN) header size in bytes (including signature). |
static final int |
CENHOW |
Central directory (CEN) header compression method field offset. |
static final int |
CENLEN |
Central directory (CEN) header uncompressed size field offset. |
static final int |
CENNAM |
Central directory (CEN) header filename length field offset. |
static final int |
CENOFF |
Central directory (CEN) header LOC header offset field offset. |
static final long |
CENSIG |
Central directory (CEN) header signature. |
static final int |
CENSIZ |
Central directory (CEN) header compressed size field offset. |
static final int |
CENTIM |
Central directory (CEN) header modification time field offset. |
static final int |
CENVEM |
Central directory (CEN) header version made by field offset. |
static final int |
CENVER |
Central directory (CEN) header version needed to extract field offset. |
static final int |
ENDCOM |
End of central directory (END) header zip file comment length field offset. |
static final int |
ENDHDR |
End of central directory (END) header size in bytes (including signature). |
static final int |
ENDOFF |
End of central directory (END) header offset for the first CEN header field offset. |
static final long |
ENDSIG |
End of central directory (END) header signature. |
static final int |
ENDSIZ |
End of central directory (END) header central directory size in bytes field offset. |
static final int |
ENDSUB |
End of central directory (END) header number of entries on this disk field offset. |
static final int |
ENDTOT |
End of central directory (END) header total number of entries field offset. |
static final int |
EXTCRC |
Extra local (EXT) header uncompressed file crc-32 value field offset. |
static final int |
EXTHDR |
Extra local (EXT) header size in bytes (including signature). |
static final int |
EXTLEN |
Extra local (EXT) header uncompressed size field offset. |
static final long |
EXTSIG |
Extra local (EXT) header signature. |
static final int |
EXTSIZ |
Extra local (EXT) header compressed size field offset. |
static final int |
LOCCRC |
Local file (LOC) header uncompressed file crc-32 value field offset. |
static final int |
LOCEXT |
Local file (LOC) header extra field length field offset. |
static final int |
LOCFLG |
Local file (LOC) header general purpose bit flag field offset. |
static final int |
LOCHDR |
Local file (LOC) header size in bytes (including signature). |
static final int |
LOCHOW |
Local file (LOC) header compression method field offset. |
static final int |
LOCLEN |
Local file (LOC) header uncompressed size field offset. |
static final int |
LOCNAM |
Local file (LOC) header filename length field offset. |
static final long |
LOCSIG |
Local file (LOC) header signature. |
static final int |
LOCSIZ |
Local file (LOC) header compressed size field offset. |
static final int |
LOCTIM |
Local file (LOC) header modification time field offset. |
static final int |
LOCVER |
Local file (LOC) header version needed to extract field offset. |
static final int |
OPEN_DELETE |
Mode flag to open a zip file and mark it for deletion. |
static final int |
OPEN_READ |
Mode flag to open a zip file for reading. |
Constructor | Description |
---|---|
ZipFile |
Opens a ZIP file for reading given the specified File object. |
ZipFile |
Opens a new ZipFile to read from the specified File object in the specified mode. |
ZipFile |
Opens a new ZipFile to read from the specified File object in the specified mode. |
ZipFile |
Opens a ZIP file for reading given the specified File object. |
ZipFile |
Opens a zip file for reading. |
ZipFile |
Opens a zip file for reading. |
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes the ZIP file. |
Enumeration |
entries() |
Returns an enumeration of the ZIP file entries. |
String |
getComment() |
Returns the zip file comment, or null if none. |
ZipEntry |
getEntry |
Returns the zip file entry for the specified name, or null if not found. |
InputStream |
getInputStream |
Returns an input stream for reading the contents of the specified zip file entry. |
String |
getName() |
Returns the path name of the ZIP file. |
int |
size() |
Returns the number of entries in the ZIP file. |
Stream |
stream() |
Returns an ordered Stream over the ZIP file entries. |
public static final int OPEN_READ
public static final int OPEN_DELETE
ZipFile
object until either the close method is invoked or the virtual machine exits.static final long LOCSIG
static final long EXTSIG
static final long CENSIG
static final long ENDSIG
static final int LOCHDR
static final int EXTHDR
static final int CENHDR
static final int ENDHDR
static final int LOCVER
static final int LOCFLG
static final int LOCHOW
static final int LOCTIM
static final int LOCCRC
static final int LOCSIZ
static final int LOCLEN
static final int LOCNAM
static final int LOCEXT
static final int EXTCRC
static final int EXTSIZ
static final int EXTLEN
static final int CENVEM
static final int CENVER
static final int CENFLG
static final int CENHOW
static final int CENTIM
static final int CENCRC
static final int CENSIZ
static final int CENLEN
static final int CENNAM
static final int CENEXT
static final int CENCOM
static final int CENDSK
static final int CENATT
static final int CENATX
static final int CENOFF
static final int ENDSUB
static final int ENDTOT
static final int ENDSIZ
static final int ENDOFF
static final int ENDCOM
public ZipFile(String name) throws IOException
First, if there is a security manager, its checkRead
method is called with the name
argument as its argument to ensure the read is allowed.
The UTF-8 charset
is used to decode the entry names and comments.
name
- the name of the zip fileZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredSecurityException
- if a security manager exists and its checkRead
method doesn't allow read access to the file.public ZipFile(File file, int mode) throws IOException
ZipFile
to read from the specified File
object in the specified mode. The mode argument must be either OPEN_READ
or OPEN_READ | OPEN_DELETE
. First, if there is a security manager, its checkRead
method is called with the name
argument as its argument to ensure the read is allowed.
The UTF-8 charset
is used to decode the entry names and comments
file
- the ZIP file to be opened for readingmode
- the mode in which the file is to be openedZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredSecurityException
- if a security manager exists and its checkRead
method doesn't allow read access to the file, or its checkDelete
method doesn't allow deleting the file when the OPEN_DELETE
flag is set.IllegalArgumentException
- if the mode
argument is invalidpublic ZipFile(File file) throws ZipException, IOException
The UTF-8 charset
is used to decode the entry names and comments.
file
- the ZIP file to be opened for readingZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredpublic ZipFile(File file, int mode, Charset charset) throws IOException
ZipFile
to read from the specified File
object in the specified mode. The mode argument must be either OPEN_READ
or OPEN_READ | OPEN_DELETE
. First, if there is a security manager, its checkRead
method is called with the name
argument as its argument to ensure the read is allowed.
file
- the ZIP file to be opened for readingmode
- the mode in which the file is to be openedcharset
- the charset to be used to decode the ZIP entry name and comment that are not encoded by using UTF-8 encoding (indicated by entry's general purpose flag).ZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredSecurityException
- if a security manager exists and its checkRead
method doesn't allow read access to the file, or its checkDelete
method doesn't allow deleting the file when the OPEN_DELETE
flag is setIllegalArgumentException
- if the mode
argument is invalidpublic ZipFile(String name, Charset charset) throws IOException
First, if there is a security manager, its checkRead
method is called with the name
argument as its argument to ensure the read is allowed.
name
- the name of the zip filecharset
- the charset to be used to decode the ZIP entry name and comment that are not encoded by using UTF-8 encoding (indicated by entry's general purpose flag).ZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredSecurityException
- if a security manager exists and its checkRead
method doesn't allow read access to the filepublic ZipFile(File file, Charset charset) throws IOException
file
- the ZIP file to be opened for readingcharset
- The charset to be used to decode the ZIP entry name and comment (ignored if the language encoding bit of the ZIP entry's general purpose bit flag is set).ZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredpublic String getComment()
IllegalStateException
- if the zip file has been closedpublic ZipEntry getEntry(String name)
name
- the name of the entryIllegalStateException
- if the zip file has been closedpublic InputStream getInputStream(ZipEntry entry) throws IOException
Closing this ZIP file will, in turn, close all input streams that have been returned by invocations of this method.
InputStream
returned by this method can wrap an InflaterInputStream
, whose read(byte[], int, int)
method can modify any element of the output buffer.entry
- the zip file entryZipException
- if a ZIP format error has occurredIOException
- if an I/O error has occurredIllegalStateException
- if the zip file has been closedpublic String getName()
public Enumeration<? extends ZipEntry> entries()
IllegalStateException
- if the zip file has been closedpublic Stream<? extends ZipEntry> stream()
Stream
over the ZIP file entries. Entries appear in the Stream
in the order they appear in the central directory of the ZIP file.Stream
of entries in this ZIP fileIllegalStateException
- if the zip file has been closedpublic int size()
IllegalStateException
- if the zip file has been closedpublic void close() throws IOException
Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream
method.
close
in interface AutoCloseable
close
in interface Closeable
IOException
- if an I/O error has occurred
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/zip/ZipFile.html