Package net.sf.jazzlib
Class ZipFile
- java.lang.Object
-
- net.sf.jazzlib.ZipFile
-
- All Implemented Interfaces:
ZipConstants
public class ZipFile extends java.lang.Object implements ZipConstants
This class represents a Zip archive. You can ask for the contained entries, or get an input stream for a file entry. The entry is automatically decompressed. This class is thread safe: You can open input streams for arbitrary entries in different threads.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classZipFile.PartialInputStreamprivate static classZipFile.ZipEntryEnumeration
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedprivate java.util.Map<java.lang.String,ZipEntry>entriesprivate byte[]locBufprivate java.lang.Stringnamestatic intOPEN_DELETEMode flag to delete a zip file after reading.static intOPEN_READMode flag to open a zip file for reading.private java.io.RandomAccessFileraf-
Fields inherited from interface net.sf.jazzlib.ZipConstants
CENATT, CENATX, CENCOM, CENCRC, CENDSK, CENEXT, CENFLG, CENHDR, CENHOW, CENLEN, CENNAM, CENOFF, CENSIG, CENSIZ, CENTIM, CENVEM, CENVER, ENDCOM, ENDDCD, ENDHDR, ENDNRD, ENDOFF, ENDSIG, ENDSIZ, ENDSUB, ENDTOT, EXTCRC, EXTHDR, EXTLEN, EXTSIG, EXTSIZ, LOCCRC, LOCEXT, LOCFLG, LOCHDR, LOCHOW, LOCLEN, LOCNAM, LOCSIG, LOCSIZ, LOCTIM, LOCVER
-
-
Constructor Summary
Constructors Constructor Description ZipFile(java.io.File file)Opens a Zip file reading the given File.ZipFile(java.io.File file, int mode)Opens a Zip file reading the given File in the given mode.ZipFile(java.lang.String name)Opens a Zip file with the given name for reading.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private longcheckLocalHeader(ZipEntry entry)Checks, if the local header of the entry at index i matches the central directory, and returns the offset to the data.voidclose()Closes the ZipFile.java.util.Enumerationentries()Returns an enumeration of all Zip entries in this Zip file.protected voidfinalize()Calls theclose()method when this ZipFile has not yet been explicitly closed.private java.util.Map<java.lang.String,ZipEntry>getEntries()Checks that the ZipFile is still open and reads entries when necessary.ZipEntrygetEntry(java.lang.String name)Searches for a zip entry in this archive with the given name.java.io.InputStreamgetInputStream(ZipEntry entry)Creates an input stream reading the given zip entry as uncompressed data.java.lang.StringgetName()Returns the (path) name of this zip file.private voidreadEntries()Read the central directory of a zip file and fill the entries array.private intreadLeInt(byte[] b, int off)Read an int in little endian byte order from the given byte buffer at the given offset.private intreadLeInt(java.io.DataInput di, byte[] b)Read an int in little endian byte order from the given DataInput stream using the given byte buffer.private intreadLeShort(byte[] b, int off)Read an unsigned short in little endian byte order from the given byte buffer at the given offset.private intreadLeShort(java.io.DataInput di, byte[] b)Read an unsigned short in little endian byte order from the given DataInput stream using the given byte buffer.intsize()Returns the number of entries in this zip file.
-
-
-
Field Detail
-
OPEN_READ
public static final int OPEN_READ
Mode flag to open a zip file for reading.- See Also:
- Constant Field Values
-
OPEN_DELETE
public static final int OPEN_DELETE
Mode flag to delete a zip file after reading.- See Also:
- Constant Field Values
-
name
private final java.lang.String name
-
raf
private final java.io.RandomAccessFile raf
-
entries
private java.util.Map<java.lang.String,ZipEntry> entries
-
closed
private boolean closed
-
locBuf
private final byte[] locBuf
-
-
Constructor Detail
-
ZipFile
public ZipFile(java.lang.String name) throws ZipException, java.io.IOExceptionOpens a Zip file with the given name for reading.- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the file doesn't contain a valid zip archive.
-
ZipFile
public ZipFile(java.io.File file) throws ZipException, java.io.IOExceptionOpens a Zip file reading the given File.- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the file doesn't contain a valid zip archive.
-
ZipFile
public ZipFile(java.io.File file, int mode) throws ZipException, java.io.IOExceptionOpens a Zip file reading the given File in the given mode. If the OPEN_DELETE mode is specified, the zip file will be deleted at some time moment after it is opened. It will be deleted before the zip file is closed or the Virtual Machine exits. The contents of the zip file will be accessible until it is closed. The OPEN_DELETE mode is currently unimplemented in this library- Parameters:
mode- Must be one of OPEN_READ or OPEN_READ | OPEN_DELETE- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the file doesn't contain a valid zip archive.- Since:
- JDK1.3
-
-
Method Detail
-
readLeShort
private final int readLeShort(java.io.DataInput di, byte[] b) throws java.io.IOExceptionRead an unsigned short in little endian byte order from the given DataInput stream using the given byte buffer.- Parameters:
di- DataInput stream to read from.b- the byte buffer to read in (must be at least 2 bytes long).- Returns:
- The value read.
- Throws:
java.io.IOException- if a i/o error occured.java.io.EOFException- if the file ends prematurely
-
readLeInt
private final int readLeInt(java.io.DataInput di, byte[] b) throws java.io.IOExceptionRead an int in little endian byte order from the given DataInput stream using the given byte buffer.- Parameters:
di- DataInput stream to read from.b- the byte buffer to read in (must be at least 4 bytes long).- Returns:
- The value read.
- Throws:
java.io.IOException- if a i/o error occured.java.io.EOFException- if the file ends prematurely
-
readLeShort
private final int readLeShort(byte[] b, int off)Read an unsigned short in little endian byte order from the given byte buffer at the given offset.- Parameters:
b- the byte array to read from.off- the offset to read from.- Returns:
- The value read.
-
readLeInt
private final int readLeInt(byte[] b, int off)Read an int in little endian byte order from the given byte buffer at the given offset.- Parameters:
b- the byte array to read from.off- the offset to read from.- Returns:
- The value read.
-
readEntries
private void readEntries() throws ZipException, java.io.IOExceptionRead the central directory of a zip file and fill the entries array. This is called exactly once when first needed. It is called while holding the lock onraf.- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the central directory is malformed
-
close
public void close() throws java.io.IOExceptionCloses the ZipFile. This also closes all input streams given by this class. After this is called, no further method should be called.- Throws:
java.io.IOException- if a i/o error occured.
-
finalize
protected void finalize() throws java.io.IOExceptionCalls theclose()method when this ZipFile has not yet been explicitly closed.- Overrides:
finalizein classjava.lang.Object- Throws:
java.io.IOException
-
entries
public java.util.Enumeration entries()
Returns an enumeration of all Zip entries in this Zip file.
-
getEntries
private java.util.Map<java.lang.String,ZipEntry> getEntries() throws java.io.IOException
Checks that the ZipFile is still open and reads entries when necessary.- Throws:
java.lang.IllegalStateException- when the ZipFile has already been closed.IOEexception- when the entries could not be read.java.io.IOException
-
getEntry
public ZipEntry getEntry(java.lang.String name)
Searches for a zip entry in this archive with the given name.- Parameters:
the- name. May contain directory components separated by slashes ('/').- Returns:
- the zip entry, or null if no entry with that name exists.
-
checkLocalHeader
private long checkLocalHeader(ZipEntry entry) throws java.io.IOException
Checks, if the local header of the entry at index i matches the central directory, and returns the offset to the data.- Parameters:
entry- to check.- Returns:
- the start offset of the (compressed) data.
- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the local header doesn't match the central directory header
-
getInputStream
public java.io.InputStream getInputStream(ZipEntry entry) throws java.io.IOException
Creates an input stream reading the given zip entry as uncompressed data. Normally zip entry should be an entry returned by getEntry() or entries().- Parameters:
entry- the entry to create an InputStream for.- Returns:
- the input stream.
- Throws:
java.io.IOException- if a i/o error occured.ZipException- if the Zip archive is malformed.
-
getName
public java.lang.String getName()
Returns the (path) name of this zip file.
-
size
public int size()
Returns the number of entries in this zip file.
-
-