Class 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.
    • 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 long checkLocalHeader​(ZipEntry entry)
      Checks, if the local header of the entry at index i matches the central directory, and returns the offset to the data.
      void close()
      Closes the ZipFile.
      java.util.Enumeration entries()
      Returns an enumeration of all Zip entries in this Zip file.
      protected void finalize()
      Calls the close() 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.
      ZipEntry getEntry​(java.lang.String name)
      Searches for a zip entry in this archive with the given name.
      java.io.InputStream getInputStream​(ZipEntry entry)
      Creates an input stream reading the given zip entry as uncompressed data.
      java.lang.String getName()
      Returns the (path) name of this zip file.
      private void readEntries()
      Read the central directory of a zip file and fill the entries array.
      private int readLeInt​(byte[] b, int off)
      Read an int in little endian byte order from the given byte buffer at the given offset.
      private int readLeInt​(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 int readLeShort​(byte[] b, int off)
      Read an unsigned short in little endian byte order from the given byte buffer at the given offset.
      private int readLeShort​(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.
      int size()
      Returns the number of entries in this zip file.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.IOException
        Opens 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.IOException
        Opens 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.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:
        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.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:
        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.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:
        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.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:
        java.io.IOException - if a i/o error occured.
        ZipException - if the central directory is malformed
      • close

        public void close()
                   throws java.io.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:
        java.io.IOException - if a i/o error occured.
      • finalize

        protected void finalize()
                         throws java.io.IOException
        Calls the close() method when this ZipFile has not yet been explicitly closed.
        Overrides:
        finalize in class java.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.