Class ZipFile
- java.lang.Object
-
- java.util.zip.ZipFile
-
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JarFile
public class ZipFile extends Object implements Closeable
This class provides random read access to a zip file. You pay more to read the zip file's central directory up front (from the constructor), but if you're usinggetEntry(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 considerZipInputStream, 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classZipFile.RAFStreamWrap a stream around a RandomAccessFile.static classZipFile.ZipInflaterInputStream
-
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
All Methods Instance Methods Concrete Methods 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 Detail
-
OPEN_READ
public static final int OPEN_READ
Open zip file for reading.- See Also:
- Constant Field Values
-
OPEN_DELETE
public static final int OPEN_DELETE
Delete zip file when closed.- See Also:
- Constant Field Values
-
LOCSIG
public static final long LOCSIG
- See Also:
- Constant Field Values
-
EXTSIG
public static final long EXTSIG
- See Also:
- Constant Field Values
-
CENSIG
public static final long CENSIG
- See Also:
- Constant Field Values
-
ENDSIG
public static final long ENDSIG
- See Also:
- Constant Field Values
-
LOCHDR
public static final int LOCHDR
- See Also:
- Constant Field Values
-
EXTHDR
public static final int EXTHDR
- See Also:
- Constant Field Values
-
CENHDR
public static final int CENHDR
- See Also:
- Constant Field Values
-
ENDHDR
public static final int ENDHDR
- See Also:
- Constant Field Values
-
LOCVER
public static final int LOCVER
- See Also:
- Constant Field Values
-
LOCFLG
public static final int LOCFLG
- See Also:
- Constant Field Values
-
LOCHOW
public static final int LOCHOW
- See Also:
- Constant Field Values
-
LOCTIM
public static final int LOCTIM
- See Also:
- Constant Field Values
-
LOCCRC
public static final int LOCCRC
- See Also:
- Constant Field Values
-
LOCSIZ
public static final int LOCSIZ
- See Also:
- Constant Field Values
-
LOCLEN
public static final int LOCLEN
- See Also:
- Constant Field Values
-
LOCNAM
public static final int LOCNAM
- See Also:
- Constant Field Values
-
LOCEXT
public static final int LOCEXT
- See Also:
- Constant Field Values
-
EXTCRC
public static final int EXTCRC
- See Also:
- Constant Field Values
-
EXTSIZ
public static final int EXTSIZ
- See Also:
- Constant Field Values
-
EXTLEN
public static final int EXTLEN
- See Also:
- Constant Field Values
-
CENVEM
public static final int CENVEM
- See Also:
- Constant Field Values
-
CENVER
public static final int CENVER
- See Also:
- Constant Field Values
-
CENFLG
public static final int CENFLG
- See Also:
- Constant Field Values
-
CENHOW
public static final int CENHOW
- See Also:
- Constant Field Values
-
CENTIM
public static final int CENTIM
- See Also:
- Constant Field Values
-
CENCRC
public static final int CENCRC
- See Also:
- Constant Field Values
-
CENSIZ
public static final int CENSIZ
- See Also:
- Constant Field Values
-
CENLEN
public static final int CENLEN
- See Also:
- Constant Field Values
-
CENNAM
public static final int CENNAM
- See Also:
- Constant Field Values
-
CENEXT
public static final int CENEXT
- See Also:
- Constant Field Values
-
CENCOM
public static final int CENCOM
- See Also:
- Constant Field Values
-
CENDSK
public static final int CENDSK
- See Also:
- Constant Field Values
-
CENATT
public static final int CENATT
- See Also:
- Constant Field Values
-
CENATX
public static final int CENATX
- See Also:
- Constant Field Values
-
CENOFF
public static final int CENOFF
- See Also:
- Constant Field Values
-
ENDSUB
public static final int ENDSUB
- See Also:
- Constant Field Values
-
ENDTOT
public static final int ENDTOT
- See Also:
- Constant Field Values
-
ENDSIZ
public static final int ENDSIZ
- See Also:
- Constant Field Values
-
ENDOFF
public static final int ENDOFF
- See Also:
- Constant Field Values
-
ENDCOM
public static final int ENDCOM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ZipFile
public ZipFile(File file) throws ZipException, IOException
Constructs a newZipFileallowing read access to the contents of the given file.UTF-8 is used to decode all comments and entry names in the file.
- Throws:
ZipException- if a zip error occurs.IOException- if anIOExceptionoccurs.
-
ZipFile
public ZipFile(String name) throws IOException
Constructs a newZipFileallowing read access to the contents of the given file.UTF-8 is used to decode all comments and entry names in the file.
- Throws:
IOException- if an IOException occurs.
-
ZipFile
public ZipFile(File file, int mode) throws IOException
Constructs a newZipFileallowing access to the given file.UTF-8 is used to decode all comments and entry names in the file.
The
modemust be eitherOPEN_READorOPEN_READ|OPEN_DELETE. If theOPEN_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 Detail
-
finalize
protected void finalize() throws IOExceptionDescription 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
public void close() throws IOExceptionCloses 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
public Enumeration<? extends ZipEntry> 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
public String 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
public ZipEntry getEntry(String entryName)
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
public InputStream getInputStream(ZipEntry entry) throws IOException
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
public String 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.
-
-