Class ZipFile

java.lang.Object
net.sf.jazzlib.ZipFile
All Implemented Interfaces:
ZipConstants

public class ZipFile extends 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.
  • Field Details

    • OPEN_READ

      public static final int OPEN_READ
      Mode flag to open a zip file for reading.
      See Also:
    • OPEN_DELETE

      public static final int OPEN_DELETE
      Mode flag to delete a zip file after reading.
      See Also:
    • name

      private final String name
    • raf

      private final RandomAccessFile raf
    • entries

      private Map<String,ZipEntry> entries
    • closed

      private boolean closed
    • locBuf

      private final byte[] locBuf
  • Constructor Details

    • ZipFile

      public ZipFile(String name) throws ZipException, IOException
      Opens a Zip file with the given name for reading.
      Throws:
      IOException - if a i/o error occured.
      ZipException - if the file doesn't contain a valid zip archive.
    • ZipFile

      public ZipFile(File file) throws ZipException, IOException
      Opens a Zip file reading the given File.
      Throws:
      IOException - if a i/o error occured.
      ZipException - if the file doesn't contain a valid zip archive.
    • ZipFile

      public ZipFile(File file, int mode) throws ZipException, IOException
      Opens 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:
      IOException - if a i/o error occured.
      ZipException - if the file doesn't contain a valid zip archive.
      Since:
      JDK1.3
  • Method Details

    • readLeShort

      private final int readLeShort(DataInput di, byte[] b) throws IOException
      Read 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:
      IOException - if a i/o error occured.
      EOFException - if the file ends prematurely
    • readLeInt

      private final int readLeInt(DataInput di, byte[] b) throws IOException
      Read 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:
      IOException - if a i/o error occured.
      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, IOException
      Read 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 on raf.
      Throws:
      IOException - if a i/o error occured.
      ZipException - if the central directory is malformed
    • close

      public void close() throws IOException
      Closes the ZipFile. This also closes all input streams given by this class. After this is called, no further method should be called.
      Throws:
      IOException - if a i/o error occured.
    • finalize

      protected void finalize() throws IOException
      Calls the close() method when this ZipFile has not yet been explicitly closed.
      Overrides:
      finalize in class Object
      Throws:
      IOException
    • entries

      public Enumeration entries()
      Returns an enumeration of all Zip entries in this Zip file.
    • getEntries

      private Map<String,ZipEntry> getEntries() throws IOException
      Checks that the ZipFile is still open and reads entries when necessary.
    • getEntry

      public ZipEntry getEntry(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 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:
      IOException - if a i/o error occured.
      ZipException - if the local header doesn't match the central directory header
    • getInputStream

      public InputStream getInputStream(ZipEntry entry) throws 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:
      IOException - if a i/o error occured.
      ZipException - if the Zip archive is malformed.
    • getName

      public String getName()
      Returns the (path) name of this zip file.
    • size

      public int size()
      Returns the number of entries in this zip file.