Class ZipFile
java.lang.Object
java.util.zip.ZipFile
- All Implemented Interfaces:
- Closeable,- AutoCloseable
- Direct Known Subclasses:
- JarFile
This class is used to read entries from a ZIP file.
 
 Unless otherwise noted, passing a null argument to a constructor
 or method in this class will cause a NullPointerException to be
 thrown.
- API Note:
- To release resources used by this ZipFile, theclose()method should be called explicitly or by try-with-resources. Subclasses are responsible for the cleanup of resources acquired by the subclass. Subclasses that overrideObject.finalize()in order to perform cleanup should be modified to use alternative cleanup mechanisms such asCleanerand remove the overridingfinalizemethod.
- Since:
- 1.1
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intCentral directory (CEN) header internal file attributes field offset.static final intCentral directory (CEN) header external file attributes field offset.static final intCentral directory (CEN) header comment length field offset.static final intCentral directory (CEN) header uncompressed file crc-32 value field offset.static final intCentral directory (CEN) header disk number start field offset.static final intCentral directory (CEN) header extra field length field offset.static final intCentral directory (CEN) header encrypt, decrypt flags field offset.static final intCentral directory (CEN) header size in bytes (including signature).static final intCentral directory (CEN) header compression method field offset.static final intCentral directory (CEN) header uncompressed size field offset.static final intCentral directory (CEN) header filename length field offset.static final intCentral directory (CEN) header LOC header offset field offset.static final longCentral directory (CEN) header signature.static final intCentral directory (CEN) header compressed size field offset.static final intCentral directory (CEN) header modification time field offset.static final intCentral directory (CEN) header version made by field offset.static final intCentral directory (CEN) header version needed to extract field offset.static final intEnd of central directory (END) header ZIP file comment length field offset.static final intEnd of central directory (END) header size in bytes (including signature).static final intEnd of central directory (END) header offset for the first CEN header field offset.static final longEnd of central directory (END) header signature.static final intEnd of central directory (END) header central directory size in bytes field offset.static final intEnd of central directory (END) header number of entries on this disk field offset.static final intEnd of central directory (END) header total number of entries field offset.static final intExtra local (EXT) header uncompressed file crc-32 value field offset.static final intExtra local (EXT) header size in bytes (including signature).static final intExtra local (EXT) header uncompressed size field offset.static final longExtra local (EXT) header signature.static final intExtra local (EXT) header compressed size field offset.static final intLocal file (LOC) header uncompressed file crc-32 value field offset.static final intLocal file (LOC) header extra field length field offset.static final intLocal file (LOC) header general purpose bit flag field offset.static final intLocal file (LOC) header size in bytes (including signature).static final intLocal file (LOC) header compression method field offset.static final intLocal file (LOC) header uncompressed size field offset.static final intLocal file (LOC) header filename length field offset.static final longLocal file (LOC) header signature.static final intLocal file (LOC) header compressed size field offset.static final intLocal file (LOC) header modification time field offset.static final intLocal file (LOC) header version needed to extract field offset.static final intMode flag to open a ZIP file and mark it for deletion.static final intMode flag to open a ZIP file for reading.
- 
Constructor SummaryConstructorsConstructorDescriptionOpens a ZIP file for reading given the specified File object.Opens a newZipFileto read from the specifiedFileobject in the specified mode.Opens a newZipFileto read from the specifiedFileobject in the specified mode.Opens a ZIP file for reading given the specified File object.Opens a ZIP file for reading.Opens a ZIP file for reading.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclose()Closes the ZIP file.Enumeration<? extends ZipEntry> entries()Returns an enumeration of the ZIP file entries.Returns the ZIP file comment.Returns the ZIP file entry for the specified name, or null if not found.getInputStream(ZipEntry entry) Returns an input stream for reading the contents of the specified ZIP file entry.getName()Returns the path name of the ZIP file.intsize()Returns the number of entries in the ZIP file.stream()Returns an orderedStreamover the ZIP file entries.toString()Returns a string identifying thisZipFile, for debugging.
- 
Field Details- 
OPEN_READpublic static final int OPEN_READMode flag to open a ZIP file for reading.- See Also:
 
- 
OPEN_DELETEpublic static final int OPEN_DELETEMode flag to open a ZIP file and mark it for deletion. The file will be deleted some time between the moment that it is opened and the moment that it is closed, but its contents will remain accessible via theZipFileobject until either the close method is invoked or the virtual machine exits.- See Also:
 
- 
LOCSIGstatic final long LOCSIGLocal file (LOC) header signature.- See Also:
 
- 
EXTSIGstatic final long EXTSIGExtra local (EXT) header signature.- See Also:
 
- 
CENSIGstatic final long CENSIGCentral directory (CEN) header signature.- See Also:
 
- 
ENDSIGstatic final long ENDSIGEnd of central directory (END) header signature.- See Also:
 
- 
LOCHDRstatic final int LOCHDRLocal file (LOC) header size in bytes (including signature).- See Also:
 
- 
EXTHDRstatic final int EXTHDRExtra local (EXT) header size in bytes (including signature).- See Also:
 
- 
CENHDRstatic final int CENHDRCentral directory (CEN) header size in bytes (including signature).- See Also:
 
- 
ENDHDRstatic final int ENDHDREnd of central directory (END) header size in bytes (including signature).- See Also:
 
- 
LOCVERstatic final int LOCVERLocal file (LOC) header version needed to extract field offset.- See Also:
 
- 
LOCFLGstatic final int LOCFLGLocal file (LOC) header general purpose bit flag field offset.- See Also:
 
- 
LOCHOWstatic final int LOCHOWLocal file (LOC) header compression method field offset.- See Also:
 
- 
LOCTIMstatic final int LOCTIMLocal file (LOC) header modification time field offset.- See Also:
 
- 
LOCCRCstatic final int LOCCRCLocal file (LOC) header uncompressed file crc-32 value field offset.- See Also:
 
- 
LOCSIZstatic final int LOCSIZLocal file (LOC) header compressed size field offset.- See Also:
 
- 
LOCLENstatic final int LOCLENLocal file (LOC) header uncompressed size field offset.- See Also:
 
- 
LOCNAMstatic final int LOCNAMLocal file (LOC) header filename length field offset.- See Also:
 
- 
LOCEXTstatic final int LOCEXTLocal file (LOC) header extra field length field offset.- See Also:
 
- 
EXTCRCstatic final int EXTCRCExtra local (EXT) header uncompressed file crc-32 value field offset.- See Also:
 
- 
EXTSIZstatic final int EXTSIZExtra local (EXT) header compressed size field offset.- See Also:
 
- 
EXTLENstatic final int EXTLENExtra local (EXT) header uncompressed size field offset.- See Also:
 
- 
CENVEMstatic final int CENVEMCentral directory (CEN) header version made by field offset.- See Also:
 
- 
CENVERstatic final int CENVERCentral directory (CEN) header version needed to extract field offset.- See Also:
 
- 
CENFLGstatic final int CENFLGCentral directory (CEN) header encrypt, decrypt flags field offset.- See Also:
 
- 
CENHOWstatic final int CENHOWCentral directory (CEN) header compression method field offset.- See Also:
 
- 
CENTIMstatic final int CENTIMCentral directory (CEN) header modification time field offset.- See Also:
 
- 
CENCRCstatic final int CENCRCCentral directory (CEN) header uncompressed file crc-32 value field offset.- See Also:
 
- 
CENSIZstatic final int CENSIZCentral directory (CEN) header compressed size field offset.- See Also:
 
- 
CENLENstatic final int CENLENCentral directory (CEN) header uncompressed size field offset.- See Also:
 
- 
CENNAMstatic final int CENNAMCentral directory (CEN) header filename length field offset.- See Also:
 
- 
CENEXTstatic final int CENEXTCentral directory (CEN) header extra field length field offset.- See Also:
 
- 
CENCOMstatic final int CENCOMCentral directory (CEN) header comment length field offset.- See Also:
 
- 
CENDSKstatic final int CENDSKCentral directory (CEN) header disk number start field offset.- See Also:
 
- 
CENATTstatic final int CENATTCentral directory (CEN) header internal file attributes field offset.- See Also:
 
- 
CENATXstatic final int CENATXCentral directory (CEN) header external file attributes field offset.- See Also:
 
- 
CENOFFstatic final int CENOFFCentral directory (CEN) header LOC header offset field offset.- See Also:
 
- 
ENDSUBstatic final int ENDSUBEnd of central directory (END) header number of entries on this disk field offset.- See Also:
 
- 
ENDTOTstatic final int ENDTOTEnd of central directory (END) header total number of entries field offset.- See Also:
 
- 
ENDSIZstatic final int ENDSIZEnd of central directory (END) header central directory size in bytes field offset.- See Also:
 
- 
ENDOFFstatic final int ENDOFFEnd of central directory (END) header offset for the first CEN header field offset.- See Also:
 
- 
ENDCOMstatic final int ENDCOMEnd of central directory (END) header ZIP file comment length field offset.- See Also:
 
 
- 
- 
Constructor Details- 
ZipFileOpens a ZIP file for reading.The UTF-8 charsetis used to decode the entry names and comments.- Parameters:
- name- the name of the ZIP file
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
 
- 
ZipFileOpens a newZipFileto read from the specifiedFileobject in the specified mode. The mode argument must be eitherOPEN_READorOPEN_READ | OPEN_DELETE.The UTF-8 charsetis used to decode the entry names and comments- Parameters:
- file- the ZIP file to be opened for reading
- mode- the mode in which the file is to be opened
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
- IllegalArgumentException- if the- modeargument is invalid
- Since:
- 1.3
 
- 
ZipFileOpens a ZIP file for reading given the specified File object.The UTF-8 charsetis used to decode the entry names and comments.- Parameters:
- file- the ZIP file to be opened for reading
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
 
- 
ZipFileOpens a newZipFileto read from the specifiedFileobject in the specified mode. The mode argument must be eitherOPEN_READorOPEN_READ | OPEN_DELETE.- Parameters:
- file- the ZIP file to be opened for reading
- mode- the mode in which the file is to be opened
- charset- 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).
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
- IllegalArgumentException- if the- modeargument is invalid
- Since:
- 1.7
 
- 
ZipFileOpens a ZIP file for reading.- Parameters:
- name- the name of the ZIP file
- charset- 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).
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
- Since:
- 1.7
 
- 
ZipFileOpens a ZIP file for reading given the specified File object.- Parameters:
- file- the ZIP file to be opened for reading
- charset- 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).
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
- Since:
- 1.7
 
 
- 
- 
Method Details- 
getCommentReturns the ZIP file comment. If a comment does not exist or an error is encountered decoding the comment using the charset specified when opening the ZIP file, thennullis returned.- Returns:
- the comment string for the ZIP file, or null if none
- Throws:
- IllegalStateException- if the ZIP file has been closed
- Since:
- 1.7
 
- 
getEntryReturns the ZIP file entry for the specified name, or null if not found.- Parameters:
- name- the name of the entry
- Returns:
- the ZIP file entry, or null if not found
- Throws:
- IllegalStateException- if the ZIP file has been closed
 
- 
getInputStreamReturns an input stream for reading the contents of the specified ZIP file entry.Closing this ZIP file will, in turn, close all input streams that have been returned by invocations of this method. - API Note:
- The InputStreamreturned by this method can wrap anInflaterInputStream, whoseread(byte[], int, int)method can modify any element of the output buffer.
- Parameters:
- entry- the ZIP file entry
- Returns:
- the input stream for reading the contents of the specified ZIP file entry or null if the ZIP file entry does not exist within the ZIP file.
- Throws:
- ZipException- if a ZIP format error has occurred
- IOException- if an I/O error has occurred
- IllegalStateException- if the ZIP file has been closed
 
- 
getNameReturns the path name of the ZIP file.- Returns:
- the path name of the ZIP file
 
- 
toString
- 
entriesReturns an enumeration of the ZIP file entries.- Returns:
- an enumeration of the ZIP file entries
- Throws:
- IllegalStateException- if the ZIP file has been closed
 
- 
streamReturns an orderedStreamover the ZIP file entries. Entries appear in theStreamin the order they appear in the central directory of the ZIP file.- Returns:
- an ordered Streamof entries in this ZIP file
- Throws:
- IllegalStateException- if the ZIP file has been closed
- Since:
- 1.8
 
- 
sizepublic int size()Returns the number of entries in the ZIP file.- Returns:
- the number of entries in the ZIP file
- Throws:
- IllegalStateException- if the ZIP file has been closed
 
- 
closeCloses the ZIP file.Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStreammethod.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Throws:
- IOException- if an I/O error has occurred
 
 
-