TrueZIP 6.8.2

de.schlichtherle.util.zip
Class BasicZipFile

java.lang.Object
  extended by de.schlichtherle.util.zip.BasicZipFile
Direct Known Subclasses:
ZipFile, ZipInputArchive

public class BasicZipFile
extends Object

This class is not intended for public use! The methods in this class are unsynchronized and entries/getEntry(java.lang.String) enumerate/return ZipEntry instances which are shared with this class rather than clones of them. This class is used within other parts of the TrueZIP API in order to benefit from the slightly better performance.

Where the constructors of this class accept a charset parameter, this is used to decode comments and entry names in the ZIP file. However, if an entry has bit 11 set in its General Purpose Bit Flag, then this parameter is ignored and "UTF-8" is used for this entry. This is in accordance to Appendix D of PKWARE's ZIP File Format Specification, version 6.3.0 and later.

This class is able to skip a preamble like the one found in self extracting archives.

Note that the entries returned by this class are instances of de.schlichtherle.util.zip.ZipEntry instead of java.util.zip.ZipEntry.

Since:
TrueZIP 6.4
Version:
$Id: BasicZipFile.java cf6a93c1e429 2010/11/07 14:28:47 christian $
Author:
Christian Schlichtherle

Nested Class Summary
private  class BasicZipFile.AccountedInputStream
           
private static class BasicZipFile.CheckedInputStream
           
private  class BasicZipFile.IntervalInputStream
          InputStream that delegates requests to the underlying RandomAccessFile, making sure that only bytes from a certain range can be read.
private static class BasicZipFile.IrregularOffsetMapper
           
private static class BasicZipFile.OffsetMapper
           
private static class BasicZipFile.PooledInflaterInputStream
           
private static class BasicZipFile.RawCheckedInputStream
          A stream which reads and returns deflated data from its input while a CRC-32 checksum is computed over the inflated data and checked in the method close.
 
Field Summary
private  ReadOnlyFile archive
          The actual data source.
private  String charset
          The charset to use for entry names and comments.
private  String comment
          The comment of this ZIP compatible file.
static String DEFAULT_CHARSET
          The default character set used for entry names and comments in ZIP compatible files.
private  Map entries
          Maps entry names to zip entries.
private static int LFH_FILE_NAME_LENGTH_OFF
           
private static long LONG_MSB
           
private  BasicZipFile.OffsetMapper mapper
          Maps offsets specified in the ZIP file to real offsets in the file.
private  int openStreams
          The number of open streams reading from this ZIP compatible file.
private  long postamble
          The number of bytes in the postamble of this ZIP compatible file.
private  long preamble
          The number of bytes in the preamble of this ZIP compatible file.
 
Constructor Summary
  BasicZipFile(File file)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(File file, String charset)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(File file, String charset, boolean preambled, boolean postambled)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(ReadOnlyFile rof)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
private BasicZipFile(ReadOnlyFile rof, File file, String charset, boolean preambled, boolean postambled)
           
  BasicZipFile(ReadOnlyFile rof, String charset)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(ReadOnlyFile rof, String charset, boolean preambled, boolean postambled)
          Opens the given ReadOnlyFile for reading its entries.
  BasicZipFile(String name)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(String name, String charset)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
  BasicZipFile(String name, String charset, boolean preambled, boolean postambled)
          Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.
 
Method Summary
 boolean busy()
          Returns true if and only if some input streams are open to read from this ZIP compatible file.
 void close()
          Closes the file.
protected  ReadOnlyFile createReadOnlyFile(File file)
          Deprecated. This method is unsafe because it's called by some constructors. Use a constructor with a ReadOnlyFile parameter instead.
protected  ZipEntry createZipEntry(String name)
          Deprecated. This method is called from a constructor! It will be replaced by a factory interface in TrueZIP 7.
private  void ensureOpen()
          Ensures that this archive is still open.
 Enumeration entries()
          Returns an enumeration of the ZIP entries in this ZIP file.
private  int findCentralDirectory(ReadOnlyFile rof, boolean preambled, boolean postambled)
          Positions the file pointer at the first Central File Header.
private static int getBufferSize(ZipEntry entry)
           
 String getCharset()
          Returns the charset to use for entry names and comments.
 InputStream getCheckedInputStream(String name)
          Equivalent to getInputStream(name, true, true).
 InputStream getCheckedInputStream(ZipEntry entry)
          Equivalent to getInputStream(entry.getName(), true, true) instead.
 String getComment()
          Returns the comment of this ZIP compatible file or null if no comment exists.
 ZipEntry getEntry(String name)
          Returns the ZipEntry for the given name or null if no entry with that name exists.
 InputStream getInputStream(String name)
          Equivalent to getInputStream(name, false, true).
 InputStream getInputStream(String name, boolean inflate)
          Deprecated.  
protected  InputStream getInputStream(String name, boolean check, boolean inflate)
          Returns an InputStream for reading the inflated or deflated data of the given entry.
 InputStream getInputStream(ZipEntry entry)
          Equivalent to getInputStream(entry.getName(), false, true) instead.
 InputStream getInputStream(ZipEntry entry, boolean inflate)
          Deprecated.  
 InputStream getPostambleInputStream()
          Returns an BasicZipFile.PooledInflaterInputStream to read the postamble of this ZIP compatible file.
 long getPostambleLength()
          Returns the length of the postamble of this ZIP compatible file in bytes.
 InputStream getPreambleInputStream()
          Returns an BasicZipFile.PooledInflaterInputStream to read the preamble of this ZIP compatible file.
 long getPreambleLength()
          Returns the length of the preamble of this ZIP compatible file in bytes.
 long length()
          Returns the file length of this ZIP compatible file in bytes.
private  void mountCentralDirectory(ReadOnlyFile rof, boolean preambled, boolean postambled)
          Reads the central directory of the given file and populates the internal tables with ZipEntry instances.
 boolean offsetsConsiderPreamble()
          Returns true if and only if the offsets in this ZIP file are relative to the start of the file, rather than the first Local File Header.
private  void setComment(String comment)
           
 int size()
          Returns the number of entries in this ZIP compatible file.
private static long skipWithBuffer(InputStream in, long toSkip, byte[] buf)
          This method skips toSkip bytes in the given input stream using the given buffer unless EOF or IOException.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LONG_MSB

private static final long LONG_MSB
See Also:
Constant Field Values

LFH_FILE_NAME_LENGTH_OFF

private static final int LFH_FILE_NAME_LENGTH_OFF
See Also:
Constant Field Values

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
The default character set used for entry names and comments in ZIP compatible files. This is "UTF-8" for compatibility with Sun's JDK implementation. Note that you should use "IBM437" for ordinary ZIP files instead.

See Also:
Constant Field Values

charset

private String charset
The charset to use for entry names and comments.


comment

private String comment
The comment of this ZIP compatible file.


entries

private final Map entries
Maps entry names to zip entries.


archive

private ReadOnlyFile archive
The actual data source.


openStreams

private int openStreams
The number of open streams reading from this ZIP compatible file.


preamble

private long preamble
The number of bytes in the preamble of this ZIP compatible file.


postamble

private long postamble
The number of bytes in the postamble of this ZIP compatible file.


mapper

private BasicZipFile.OffsetMapper mapper
Maps offsets specified in the ZIP file to real offsets in the file.

Constructor Detail

BasicZipFile

public BasicZipFile(String name)
             throws NullPointerException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(name, DEFAULT_CHARSET, true, false)

Throws:
NullPointerException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(String name,
                    String charset)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(name, charset, true, false)

Throws:
NullPointerException
UnsupportedEncodingException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(String name,
                    String charset,
                    boolean preambled,
                    boolean postambled)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Opens the ZIP file identified by the given path name for reading its entries.

Parameters:
name - The path name of the file.
charset - The charset to use for decoding entry names and ZIP file comment.
preambled - If this is true, then the ZIP file may have a preamble. Otherwise, the ZIP file must start with either a Local File Header (LFH) signature or an End Of Central Directory (EOCD) Header, causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX), which usually contain the application code required for extraction in the preamble.
postambled - If this is true, then the ZIP file may have a postamble of arbitrary length. Otherwise, the ZIP file must not have a postamble which exceeds 64KB size, including the End Of Central Directory record (i.e. including the ZIP file comment), causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX) with large postambles.
Throws:
NullPointerException - If name or charset is null.
UnsupportedEncodingException - If charset is not supported by this JVM.
FileNotFoundException - If the file cannot get opened for reading.
ZipException - If the file is not compatible with the ZIP File Format Specification.
IOException - On any other I/O related issue.

BasicZipFile

public BasicZipFile(File file)
             throws NullPointerException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(file, DEFAULT_CHARSET, true, false)

Throws:
NullPointerException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(File file,
                    String charset)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(file, charset, true, false)

Throws:
NullPointerException
UnsupportedEncodingException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(File file,
                    String charset,
                    boolean preambled,
                    boolean postambled)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Opens the given File for reading its entries.

Parameters:
file - The file.
charset - The charset to use for decoding entry names and ZIP file comment.
preambled - If this is true, then the ZIP file may have a preamble. Otherwise, the ZIP file must start with either a Local File Header (LFH) signature or an End Of Central Directory (EOCD) Header, causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX), which usually contain the application code required for extraction in the preamble.
postambled - If this is true, then the ZIP file may have a postamble of arbitrary length. Otherwise, the ZIP file must not have a postamble which exceeds 64KB size, including the End Of Central Directory record (i.e. including the ZIP file comment), causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX) with large postambles.
Throws:
NullPointerException - If file or charset is null.
UnsupportedEncodingException - If charset is not supported by this JVM.
FileNotFoundException - If the file cannot get opened for reading.
ZipException - If the file is not compatible with the ZIP File Format Specification.
IOException - On any other I/O related issue.

BasicZipFile

public BasicZipFile(ReadOnlyFile rof)
             throws NullPointerException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(rof, DEFAULT_CHARSET, true, false)

Throws:
NullPointerException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(ReadOnlyFile rof,
                    String charset)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Deprecated. Use new BasicZipFile(ReadOnlyFile, String, boolean, boolean) instead.

Equivalent to BasicZipFile(rof, charset, true, false)

Throws:
NullPointerException
UnsupportedEncodingException
FileNotFoundException
ZipException
IOException

BasicZipFile

public BasicZipFile(ReadOnlyFile rof,
                    String charset,
                    boolean preambled,
                    boolean postambled)
             throws NullPointerException,
                    UnsupportedEncodingException,
                    FileNotFoundException,
                    ZipException,
                    IOException
Opens the given ReadOnlyFile for reading its entries.

Parameters:
rof - The random access read only file.
charset - The charset to use for decoding entry names and ZIP file comment.
preambled - If this is true, then the ZIP file may have a preamble. Otherwise, the ZIP file must start with either a Local File Header (LFH) signature or an End Of Central Directory (EOCD) Header, causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX), which usually contain the application code required for extraction in the preamble.
postambled - If this is true, then the ZIP file may have a postamble of arbitrary length. Otherwise, the ZIP file must not have a postamble which exceeds 64KB size, including the End Of Central Directory record (i.e. including the ZIP file comment), causing this constructor to fail if the file is actually a false positive ZIP file, i.e. not compatible to the ZIP File Format Specification. This may be useful to read Self Extracting ZIP files (SFX) with large postambles.
Throws:
NullPointerException - If rof or charset is null.
UnsupportedEncodingException - If charset is not supported by this JVM.
FileNotFoundException - If the file cannot get opened for reading.
ZipException - If the file is not compatible with the ZIP File Format Specification.
IOException - On any other I/O related issue.

BasicZipFile

private BasicZipFile(ReadOnlyFile rof,
                     File file,
                     String charset,
                     boolean preambled,
                     boolean postambled)
              throws NullPointerException,
                     UnsupportedEncodingException,
                     FileNotFoundException,
                     ZipException,
                     IOException
Throws:
NullPointerException
UnsupportedEncodingException
FileNotFoundException
ZipException
IOException
Method Detail

createReadOnlyFile

protected ReadOnlyFile createReadOnlyFile(File file)
                                   throws FileNotFoundException,
                                          IOException
Deprecated. This method is unsafe because it's called by some constructors. Use a constructor with a ReadOnlyFile parameter instead.

A factory method called by the constructor to get a read only file to access the contents of the ZIP file. This method is only used if the constructor isn't called with a read only file as its parameter.

Throws:
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.

mountCentralDirectory

private void mountCentralDirectory(ReadOnlyFile rof,
                                   boolean preambled,
                                   boolean postambled)
                            throws ZipException,
                                   IOException
Reads the central directory of the given file and populates the internal tables with ZipEntry instances.

The ZipEntrys will know all data that can be obtained from the central directory alone, but not the data that requires the local file header or additional data to be read.

Throws:
ZipException - If the file is not ZIP compatible.
IOException - On any other I/O related issue.

findCentralDirectory

private int findCentralDirectory(ReadOnlyFile rof,
                                 boolean preambled,
                                 boolean postambled)
                          throws ZipException,
                                 IOException
Positions the file pointer at the first Central File Header. Performs some means to check that this is really a ZIP compatible file.

As a side effect, both mapper and }postamble} will be set.

Throws:
ZipException - If the file is not ZIP compatible.
IOException - On any other I/O related issue.

createZipEntry

protected ZipEntry createZipEntry(String name)
Deprecated. This method is called from a constructor! It will be replaced by a factory interface in TrueZIP 7.

A factory method returning a newly created ZipEntry for the given name.


getComment

public String getComment()
Returns the comment of this ZIP compatible file or null if no comment exists.


setComment

private void setComment(String comment)

busy

public boolean busy()
Returns true if and only if some input streams are open to read from this ZIP compatible file.


getCharset

public String getCharset()
Returns the charset to use for entry names and comments.


entries

public Enumeration entries()
Returns an enumeration of the ZIP entries in this ZIP file. Note that the enumerated entries are shared with this class. It is illegal to change their state!


getEntry

public ZipEntry getEntry(String name)
Returns the ZipEntry for the given name or null if no entry with that name exists. Note that the returned entry is shared with this class. It is illegal to change its state!

Parameters:
name - Name of the ZIP entry.

size

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


length

public long length()
            throws IOException
Returns the file length of this ZIP compatible file in bytes.

Throws:
IOException

getPreambleLength

public long getPreambleLength()
Returns the length of the preamble of this ZIP compatible file in bytes.

Returns:
A positive value or zero to indicate that this ZIP compatible file does not have a preamble.
Since:
TrueZIP 5.1

getPreambleInputStream

public InputStream getPreambleInputStream()
                                   throws IOException
Returns an BasicZipFile.PooledInflaterInputStream to read the preamble of this ZIP compatible file.

Note that the returned stream is a lightweight stream, i.e. there is no external resource such as a ReadOnlyFile allocated for it. Instead, all streams returned by this method share the underlying ReadOnlyFile of this ZipFile. This allows to close this object (and hence the underlying ReadOnlyFile) without cooperation of the returned streams, which is important if the client application wants to work on the underlying file again (e.g. update or delete it).

Throws:
ZipException - If this ZIP file has been closed.
IOException
Since:
TrueZIP 5.1

getPostambleLength

public long getPostambleLength()
Returns the length of the postamble of this ZIP compatible file in bytes.

Returns:
A positive value or zero to indicate that this ZIP compatible file does not have an postamble.
Since:
TrueZIP 5.1

getPostambleInputStream

public InputStream getPostambleInputStream()
                                    throws IOException
Returns an BasicZipFile.PooledInflaterInputStream to read the postamble of this ZIP compatible file.

Note that the returned stream is a lightweight stream, i.e. there is no external resource such as a ReadOnlyFile allocated for it. Instead, all streams returned by this method share the underlying ReadOnlyFile of this ZipFile. This allows to close this object (and hence the underlying ReadOnlyFile) without cooperation of the returned streams, which is important if the client application wants to work on the underlying file again (e.g. update or delete it).

Throws:
ZipException - If this ZIP file has been closed.
IOException
Since:
TrueZIP 5.1

offsetsConsiderPreamble

public boolean offsetsConsiderPreamble()
Returns true if and only if the offsets in this ZIP file are relative to the start of the file, rather than the first Local File Header.

This method is intended for very special purposes only.


getInputStream

public final InputStream getInputStream(String name)
                                 throws IOException
Equivalent to getInputStream(name, false, true).

Throws:
IOException

getInputStream

public final InputStream getInputStream(ZipEntry entry)
                                 throws IOException
Equivalent to getInputStream(entry.getName(), false, true) instead.

Throws:
IOException

getCheckedInputStream

public final InputStream getCheckedInputStream(String name)
                                        throws IOException
Equivalent to getInputStream(name, true, true).

Throws:
IOException

getCheckedInputStream

public final InputStream getCheckedInputStream(ZipEntry entry)
                                        throws IOException
Equivalent to getInputStream(entry.getName(), true, true) instead.

Throws:
IOException

getInputStream

public InputStream getInputStream(String name,
                                  boolean inflate)
                           throws IOException
Deprecated. 

Throws:
IOException

getInputStream

public final InputStream getInputStream(ZipEntry entry,
                                        boolean inflate)
                                 throws IOException
Deprecated. 

Throws:
IOException

getInputStream

protected InputStream getInputStream(String name,
                                     boolean check,
                                     boolean inflate)
                              throws IOException
Returns an InputStream for reading the inflated or deflated data of the given entry.

If the close() method is called on this instance, all input streams returned by this method are closed, too.

Parameters:
name - The name of the entry to get the stream for - may not be null!
check - Whether or not the entry's CRC-32 value is checked. If and only if this parameter is true, two additional checks are performed for the ZIP entry:
  1. All entry headers are checked to have consistent declarations of the CRC-32 value for the inflated entry data.
  2. When calling BasicZipFile.PooledInflaterInputStream.close() on the returned entry stream, the CRC-32 value computed from the inflated entry data is checked against the declared CRC-32 values. This is independent from the inflate parameter.
If any of these checks fail, a CRC32Exception is thrown.

This parameter should be false for most applications, and is the default for the sibling of this class in java.util.zip.ZipFile.

inflate - Whether or not the entry data should be inflated. If false, the entry data is not inflated, even if the entry data is deflated. This parameter should be true for most applications.
Returns:
A stream to read the entry data from or null if the entry does not exist.
Throws:
NullPointerException - If name is null.
CRC32Exception - If the declared CRC-32 values of the inflated entry data are inconsistent across the entry headers.
ZipException - If this file is not compatible to the ZIP File Format Specification.
IOException - If the entry cannot get read from this ZipFile.
Since:
TrueZIP 6.4

getBufferSize

private static int getBufferSize(ZipEntry entry)

ensureOpen

private void ensureOpen()
                 throws ZipException
Ensures that this archive is still open.

Throws:
ZipException

skipWithBuffer

private static long skipWithBuffer(InputStream in,
                                   long toSkip,
                                   byte[] buf)
                            throws IOException
This method skips toSkip bytes in the given input stream using the given buffer unless EOF or IOException.

Throws:
IOException

close

public void close()
           throws IOException
Closes the file. This closes any open input streams reading from this ZIP file.

Throws:
IOException - if an error occurs closing the file.

TrueZIP 6.8.2

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.