Class ZipFile
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JarFile
public class ZipFile extends Object implements Closeable
getEntry(java.lang.String) to look up multiple files by name, you get the benefit of this index.
If you only want to iterate through all the files (using entries(), you should
consider ZipInputStream, which provides stream-like read access to a zip file and
has a lower up-front cost because you don't pay to build an in-memory index.
If you want to create a zip file, use ZipOutputStream. There is no API for updating
an existing zip file.
-
Field Summary
Fields Modifier and Type Field Description static intCENATTstatic intCENATXstatic intCENCOMstatic intCENCRCstatic intCENDSKstatic intCENEXTstatic intCENFLGstatic intCENHDRstatic intCENHOWstatic intCENLENstatic intCENNAMstatic intCENOFFstatic longCENSIGstatic intCENSIZstatic intCENTIMstatic intCENVEMstatic intCENVERstatic intENDCOMstatic intENDHDRstatic intENDOFFstatic longENDSIGstatic intENDSIZstatic intENDSUBstatic intENDTOTstatic intEXTCRCstatic intEXTHDRstatic intEXTLENstatic longEXTSIGstatic intEXTSIZstatic intLOCCRCstatic intLOCEXTstatic intLOCFLGstatic intLOCHDRstatic intLOCHOWstatic intLOCLENstatic intLOCNAMstatic longLOCSIGstatic intLOCSIZstatic intLOCTIMstatic intLOCVERstatic intOPEN_DELETEDelete zip file when closed.static intOPEN_READOpen zip file for reading. -
Constructor Summary
Constructors Constructor Description ZipFile(File file)Constructs a newZipFileallowing read access to the contents of the given file.ZipFile(File file, int mode)Constructs a newZipFileallowing access to the given file.ZipFile(String name)Constructs a newZipFileallowing read access to the contents of the given file. -
Method Summary
Modifier and Type Method Description voidclose()Closes this zip file.Enumeration<? extends ZipEntry>entries()Returns an enumeration of the entries.protected voidfinalize()Invoked when the garbage collector has detected that this instance is no longer reachable.StringgetComment()Returns this file's comment, or null if it doesn't have one.ZipEntrygetEntry(String entryName)Returns the zip entry with the given name, or null if there is no such entry.InputStreamgetInputStream(ZipEntry entry)Returns an input stream on the data of the specifiedZipEntry.StringgetName()Gets the file name of thisZipFile.intsize()Returns the number ofZipEntriesin thisZipFile.
-
Field Details
-
OPEN_READ
public static final int OPEN_READOpen zip file for reading.- See Also:
- Constant Field Values
-
OPEN_DELETE
public static final int OPEN_DELETEDelete zip file when closed.- See Also:
- Constant Field Values
-
LOCSIG
static final long LOCSIG- See Also:
- Constant Field Values
-
EXTSIG
static final long EXTSIG- See Also:
- Constant Field Values
-
CENSIG
static final long CENSIG- See Also:
- Constant Field Values
-
ENDSIG
static final long ENDSIG- See Also:
- Constant Field Values
-
LOCHDR
static final int LOCHDR- See Also:
- Constant Field Values
-
EXTHDR
static final int EXTHDR- See Also:
- Constant Field Values
-
CENHDR
static final int CENHDR- See Also:
- Constant Field Values
-
ENDHDR
static final int ENDHDR- See Also:
- Constant Field Values
-
LOCVER
static final int LOCVER- See Also:
- Constant Field Values
-
LOCFLG
static final int LOCFLG- See Also:
- Constant Field Values
-
LOCHOW
static final int LOCHOW- See Also:
- Constant Field Values
-
LOCTIM
static final int LOCTIM- See Also:
- Constant Field Values
-
LOCCRC
static final int LOCCRC- See Also:
- Constant Field Values
-
LOCSIZ
static final int LOCSIZ- See Also:
- Constant Field Values
-
LOCLEN
static final int LOCLEN- See Also:
- Constant Field Values
-
LOCNAM
static final int LOCNAM- See Also:
- Constant Field Values
-
LOCEXT
static final int LOCEXT- See Also:
- Constant Field Values
-
EXTCRC
static final int EXTCRC- See Also:
- Constant Field Values
-
EXTSIZ
static final int EXTSIZ- See Also:
- Constant Field Values
-
EXTLEN
static final int EXTLEN- See Also:
- Constant Field Values
-
CENVEM
static final int CENVEM- See Also:
- Constant Field Values
-
CENVER
static final int CENVER- See Also:
- Constant Field Values
-
CENFLG
static final int CENFLG- See Also:
- Constant Field Values
-
CENHOW
static final int CENHOW- See Also:
- Constant Field Values
-
CENTIM
static final int CENTIM- See Also:
- Constant Field Values
-
CENCRC
static final int CENCRC- See Also:
- Constant Field Values
-
CENSIZ
static final int CENSIZ- See Also:
- Constant Field Values
-
CENLEN
static final int CENLEN- See Also:
- Constant Field Values
-
CENNAM
static final int CENNAM- See Also:
- Constant Field Values
-
CENEXT
static final int CENEXT- See Also:
- Constant Field Values
-
CENCOM
static final int CENCOM- See Also:
- Constant Field Values
-
CENDSK
static final int CENDSK- See Also:
- Constant Field Values
-
CENATT
static final int CENATT- See Also:
- Constant Field Values
-
CENATX
static final int CENATX- See Also:
- Constant Field Values
-
CENOFF
static final int CENOFF- See Also:
- Constant Field Values
-
ENDSUB
static final int ENDSUB- See Also:
- Constant Field Values
-
ENDTOT
static final int ENDTOT- See Also:
- Constant Field Values
-
ENDSIZ
static final int ENDSIZ- See Also:
- Constant Field Values
-
ENDOFF
static final int ENDOFF- See Also:
- Constant Field Values
-
ENDCOM
static final int ENDCOM- See Also:
- Constant Field Values
-
-
Constructor Details
-
ZipFile
Constructs a newZipFileallowing read access to the contents of the given file.- Throws:
ZipException- if a zip error occurs.IOException- if anIOExceptionoccurs.
-
ZipFile
Constructs a newZipFileallowing read access to the contents of the given file.- Throws:
IOException- if an IOException occurs.
-
ZipFile
Constructs a newZipFileallowing access to the given file. Themodemust be eitherOPEN_READorOPEN_READ|OPEN_DELETE.If the
OPEN_DELETEflag is supplied, the file will be deleted at or before the time that theZipFileis closed (the contents will remain accessible until thisZipFileis closed); it also callsFile.deleteOnExit.- Throws:
IOException- if anIOExceptionoccurs.
-
-
Method Details
-
finalize
Description copied from class:ObjectInvoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.Note that objects that override
finalizeare significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicitclosemethod (and implementCloseable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like aBigIntegerwhere typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.If you must use finalizers, consider at least providing your own
ReferenceQueueand having your own thread process that queue.Unlike constructors, finalizers are not automatically chained. You are responsible for calling
super.finalize()yourself.Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
- Overrides:
finalizein classObject- Throws:
IOException
-
close
Closes this zip file. This method is idempotent. This method may cause I/O if the zip file needs to be deleted.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an IOException occurs.
-
entries
Returns an enumeration of the entries. The entries are listed in the order in which they appear in the zip file.If you only need to iterate over the entries in a zip file, and don't need random-access entry lookup by name, you should probably use
ZipInputStreaminstead, to avoid paying to construct the in-memory index.- Throws:
IllegalStateException- if this zip file has been closed.
-
getComment
Returns this file's comment, or null if it doesn't have one. SeeZipOutputStream.setComment(java.lang.String).- Throws:
IllegalStateException- if this zip file has been closed.- Since:
- 1.7
-
getEntry
Returns the zip entry with the given name, or null if there is no such entry.- Throws:
IllegalStateException- if this zip file has been closed.
-
getInputStream
Returns an input stream on the data of the specifiedZipEntry.- Parameters:
entry- the ZipEntry.- Returns:
- an input stream of the data contained in the
ZipEntry. - Throws:
IOException- if anIOExceptionoccurs.IllegalStateException- if this zip file has been closed.
-
getName
Gets the file name of thisZipFile.- Returns:
- the file name of this
ZipFile.
-
size
public int size()Returns the number ofZipEntriesin thisZipFile.- Returns:
- the number of entries in this file.
- Throws:
IllegalStateException- if this zip file has been closed.
-