org.at4j.zip
Class ZipFileParser

java.lang.Object
  extended by org.at4j.zip.ZipFileParser

public class ZipFileParser
extends Object

When it is created, a ZipFile uses a ZipFileParser instance to parse the contents of a Zip file. The parser objects has a few extension points that (advanced) clients may use to teach the parser about new extra field types, new compression methods or new kinds of external file attributes.

Since:
1.0
Author:
Karl Gustafsson
See Also:
ZipFile

Field Summary
protected static long CENTRAL_FILE_HEADER
           
protected static long LOCAL_FILE_HEADER
           
 
Constructor Summary
ZipFileParser()
           
 
Method Summary
protected  void assertIsCentralFileHeader(UnsignedInteger i, RandomAccess ra)
          This method throws a ZipFileParseException if the supplied integer's value is not the central directory header magic number.
 ZipEntryCompressionMethodRegistry getCompressionMethodRegistry()
          Get the compression method registry used by this object.
 ZipExternalFileAttributesParserRegistry getExternalFileAttributesParserRegistry()
          Get the external file attributes parser registry used by this object.
 ZipEntryExtraFieldParserRegistry getExtraFieldParserRegistry()
          Get the extra field parser registry used by this object.
protected  boolean isCentralFileHeader(UnsignedInteger i)
          Does the integer contain the central directory header magic number?
protected  boolean isLocalFileHeader(UnsignedInteger i)
          Does the integer contain the local Zip entry header magic number?
 ZipFileContents parse(ZipEntryCollaborator collaborator, RandomAccess ra, Charset fileNameEncodingCs, Charset textEncodingCs)
          Parse the Zip file.
protected  byte[] readByteArray(RandomAccess ra, int length)
          Read a byte array from the random access.
protected  String readString(RandomAccess ra, int length, Charset cs, boolean utf8Encoding)
          Read a string from the random access.
 void setCompressionMethodRegistry(ZipEntryCompressionMethodRegistry registry)
          Set a custom compression method registry.
 void setExternalFileAttributesParserRegistry(ZipExternalFileAttributesParserRegistry registry)
          Set a custom external file attributes parser registry.
 void setExtraFieldParserRegistry(ZipEntryExtraFieldParserRegistry registry)
          Set a custom Zip entry extra field parser registry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCAL_FILE_HEADER

protected static final long LOCAL_FILE_HEADER
See Also:
Constant Field Values

CENTRAL_FILE_HEADER

protected static final long CENTRAL_FILE_HEADER
See Also:
Constant Field Values
Constructor Detail

ZipFileParser

public ZipFileParser()
Method Detail

getCompressionMethodRegistry

public ZipEntryCompressionMethodRegistry getCompressionMethodRegistry()
Get the compression method registry used by this object.

The object returned is the actual registry used by this object. By changing its configuration, the behavior of this parser is changed.

Returns:
The compression method registry used by this object.
See Also:
setCompressionMethodRegistry(ZipEntryCompressionMethodRegistry)

setCompressionMethodRegistry

public void setCompressionMethodRegistry(ZipEntryCompressionMethodRegistry registry)
Set a custom compression method registry.

If this is not set, a registry with the default configuration for ZipEntryCompressionMethodRegistry is used.

The getCompressionMethodRegistry() method can be used to modify the configuration of the current registry.

Parameters:
registry - The new compression method registry.
See Also:
getCompressionMethodRegistry()

getExternalFileAttributesParserRegistry

public ZipExternalFileAttributesParserRegistry getExternalFileAttributesParserRegistry()
Get the external file attributes parser registry used by this object.

The object returned is the actual registry used by this object. By changing its configuration, the behavior of this parser is changed.

Returns:
The external file attribute parser registry used by this object.
See Also:
setExternalFileAttributesParserRegistry(ZipExternalFileAttributesParserRegistry)

setExternalFileAttributesParserRegistry

public void setExternalFileAttributesParserRegistry(ZipExternalFileAttributesParserRegistry registry)
Set a custom external file attributes parser registry.

If this is not set, a registry with the default configuration for ZipExternalFileAttributesParserRegistry is used.

The getExternalFileAttributesParserRegistry() method can be used to modify the configuration of the current registry.

Parameters:
registry - The new registry.
See Also:
getExternalFileAttributesParserRegistry()

getExtraFieldParserRegistry

public ZipEntryExtraFieldParserRegistry getExtraFieldParserRegistry()
Get the extra field parser registry used by this object.

The object returned is the actual registry used by this object. By changing its configuration, the behavior of this object is changed.

Returns:
The extra field parser registry used by this object.
See Also:
setExtraFieldParserRegistry(ZipEntryExtraFieldParserRegistry)

setExtraFieldParserRegistry

public void setExtraFieldParserRegistry(ZipEntryExtraFieldParserRegistry registry)
Set a custom Zip entry extra field parser registry.

If this is not set, the default configuration for ZipEntryExtraFieldParserRegistry is used.

The getExtraFieldParserRegistry() method can be used to modify the configuration of the current registry.

Parameters:
registry - The new registry.
See Also:
getExtraFieldParserRegistry()

isLocalFileHeader

protected boolean isLocalFileHeader(UnsignedInteger i)
Does the integer contain the local Zip entry header magic number?

Parameters:
i - The integer.
Returns:
true if the integer contains the local Zip entry header magic number.

isCentralFileHeader

protected boolean isCentralFileHeader(UnsignedInteger i)
Does the integer contain the central directory header magic number?

Parameters:
i - The integer.
Returns:
true if the integer contains the central directory header magic number.

assertIsCentralFileHeader

protected void assertIsCentralFileHeader(UnsignedInteger i,
                                         RandomAccess ra)
                                  throws ZipFileParseException
This method throws a ZipFileParseException if the supplied integer's value is not the central directory header magic number.

Parameters:
i - The integer.
ra - The random access that the Zip file is read from.
Throws:
ZipFileParseException - If the supplied integer is not the central directory header magic number.

readString

protected String readString(RandomAccess ra,
                            int length,
                            Charset cs,
                            boolean utf8Encoding)
                     throws UnexpectedEofException
Read a string from the random access.

Parameters:
ra - The random access to read the string from.
length - The length of the string, in bytes.
cs - The charset to use for decoding the string, unless utf8Encoding is set.
utf8Encoding - If this is set, the string is decoded using the UTF-8 charset no matter what the cs argument is set to.
Returns:
The string
Throws:
UnexpectedEofException - If the end of the file is reached before the string data has been fully read.

readByteArray

protected byte[] readByteArray(RandomAccess ra,
                               int length)
                        throws UnexpectedEofException
Read a byte array from the random access.

Parameters:
ra - The random access to read the byte array from.
length - The length of the byte array.
Returns:
A new byte array containing data read from the random access.
Throws:
UnexpectedEofException - If the end of the file is reached before the byte array has been fully read.

parse

public ZipFileContents parse(ZipEntryCollaborator collaborator,
                             RandomAccess ra,
                             Charset fileNameEncodingCs,
                             Charset textEncodingCs)
                      throws ZipFileParseException,
                             WrappedIOException
Parse the Zip file.

Parameters:
collaborator - Collaborator object that is used to access the file that the ZipFile object is created from.
ra - A random access on the Zip file.
fileNameEncodingCs - The charset used to decode file names.
textEncodingCs - The charset used to decode other text entries, such as Zip entry comments.
Returns:
The contents of the Zip file. This is used by the ZipFile object to initialize itself.
Throws:
ZipFileParseException - If the Zip file cannot be parsed for some reason other than an I/O error.
WrappedIOException - On I/O errors.