org.apache.commons.compress.archivers.zip
Class ZipArchiveInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.commons.compress.archivers.ArchiveInputStream
          extended by org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
JarArchiveInputStream

public class ZipArchiveInputStream
extends ArchiveInputStream

Implements an input stream that can read Zip archives.

Note that ZipEntry.getSize() may return -1 if the DEFLATE algorithm is used, as the size information is not available from the header.

The ZipFile class is preferred when reading from files.

See Also:
ZipFile

Field Summary
private  boolean allowStoredEntriesWithDataDescriptor
           
private  byte[] buf
           
private  int bytesReadFromStream
           
private  boolean closed
           
private  java.util.zip.CRC32 crc
           
private  ZipArchiveEntry current
           
private  boolean hasDataDescriptor
           
private  boolean hitCentralDirectory
           
private  java.io.InputStream in
           
private  java.util.zip.Inflater inf
           
private  java.io.ByteArrayInputStream lastStoredEntry
           
private  int lengthOfLastRead
           
private static int LFH_LEN
           
private  int offsetInBuffer
           
private  int readBytesOfEntry
           
private static int SHORT
           
private  boolean useUnicodeExtraFields
          Whether to look for and use Unicode extra fields.
private static int WORD
           
private  ZipEncoding zipEncoding
          The zip encoding to use for filenames and the file comment.
 
Constructor Summary
ZipArchiveInputStream(java.io.InputStream inputStream)
           
ZipArchiveInputStream(java.io.InputStream inputStream, java.lang.String encoding, boolean useUnicodeExtraFields)
           
ZipArchiveInputStream(java.io.InputStream inputStream, java.lang.String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor)
           
 
Method Summary
 boolean canReadEntryData(ArchiveEntry ae)
          Whether this class is able to read the given entry.
private static boolean checksig(byte[] signature, byte[] expected)
           
 void close()
           
private  void closeEntry()
          Closes the current ZIP archive entry and positions the underlying stream to the beginning of the next entry.
private  void fill()
           
 ArchiveEntry getNextEntry()
          Returns the next Archive Entry in this Stream.
 ZipArchiveEntry getNextZipEntry()
           
static boolean matches(byte[] signature, int length)
          Checks if the signature matches what is expected for a zip file.
 int read(byte[] buffer, int start, int length)
           
private  void readDataDescriptor()
           
private  void readFully(byte[] b)
           
private  void readStoredEntry()
          Caches a stored entry that uses the data descriptor.
 long skip(long value)
           
private  boolean supportsDataDescriptorFor(ZipArchiveEntry entry)
          Whether this entry requires a data descriptor this library can work with.
 
Methods inherited from class org.apache.commons.compress.archivers.ArchiveInputStream
count, count, getBytesRead, getCount, pushedBackBytes, read
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHORT

private static final int SHORT
See Also:
Constant Field Values

WORD

private static final int WORD
See Also:
Constant Field Values

zipEncoding

private final ZipEncoding zipEncoding
The zip encoding to use for filenames and the file comment.


useUnicodeExtraFields

private final boolean useUnicodeExtraFields
Whether to look for and use Unicode extra fields.


in

private final java.io.InputStream in

inf

private final java.util.zip.Inflater inf

crc

private final java.util.zip.CRC32 crc

buf

private final byte[] buf

current

private ZipArchiveEntry current

closed

private boolean closed

hitCentralDirectory

private boolean hitCentralDirectory

readBytesOfEntry

private int readBytesOfEntry

offsetInBuffer

private int offsetInBuffer

bytesReadFromStream

private int bytesReadFromStream

lengthOfLastRead

private int lengthOfLastRead

hasDataDescriptor

private boolean hasDataDescriptor

lastStoredEntry

private java.io.ByteArrayInputStream lastStoredEntry

allowStoredEntriesWithDataDescriptor

private boolean allowStoredEntriesWithDataDescriptor

LFH_LEN

private static final int LFH_LEN
See Also:
Constant Field Values
Constructor Detail

ZipArchiveInputStream

public ZipArchiveInputStream(java.io.InputStream inputStream)

ZipArchiveInputStream

public ZipArchiveInputStream(java.io.InputStream inputStream,
                             java.lang.String encoding,
                             boolean useUnicodeExtraFields)
Parameters:
encoding - the encoding to use for file names, use null for the platform's default encoding
useUnicodeExtraFields - whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.

ZipArchiveInputStream

public ZipArchiveInputStream(java.io.InputStream inputStream,
                             java.lang.String encoding,
                             boolean useUnicodeExtraFields,
                             boolean allowStoredEntriesWithDataDescriptor)
Parameters:
encoding - the encoding to use for file names, use null for the platform's default encoding
useUnicodeExtraFields - whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.
allowStoredEntriesWithDataDescriptor - whether the stream will try to read STORED entries that use a data descriptor
Since:
Apache Commons Compress 1.1
Method Detail

getNextZipEntry

public ZipArchiveEntry getNextZipEntry()
                                throws java.io.IOException
Throws:
java.io.IOException

getNextEntry

public ArchiveEntry getNextEntry()
                          throws java.io.IOException
Returns the next Archive Entry in this Stream.

Specified by:
getNextEntry in class ArchiveInputStream
Returns:
the next entry, or null if there are no more entries
Throws:
java.io.IOException - if the next entry could not be read

canReadEntryData

public boolean canReadEntryData(ArchiveEntry ae)
Whether this class is able to read the given entry.

May return false if it is set up to use encryption or a compression method that hasn't been implemented yet.

Overrides:
canReadEntryData in class ArchiveInputStream
Since:
Apache Commons Compress 1.1

read

public int read(byte[] buffer,
                int start,
                int length)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long value)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

matches

public static boolean matches(byte[] signature,
                              int length)
Checks if the signature matches what is expected for a zip file. Does not currently handle self-extracting zips which may have arbitrary leading content.

Parameters:
signature - the bytes to check
length - the number of bytes to check
Returns:
true, if this stream is a zip archive stream, false otherwise

checksig

private static boolean checksig(byte[] signature,
                                byte[] expected)

closeEntry

private void closeEntry()
                 throws java.io.IOException
Closes the current ZIP archive entry and positions the underlying stream to the beginning of the next entry. All per-entry variables and data structures are cleared.

If the compressed size of this entry is included in the entry header, then any outstanding bytes are simply skipped from the underlying stream without uncompressing them. This allows an entry to be safely closed even if the compression method is unsupported.

In case we don't know the compressed size of this entry or have already buffered too much data from the underlying stream to support uncompression, then the uncompression process is completed and the end position of the stream is adjusted based on the result of that process.

Throws:
java.io.IOException - if an error occurs

fill

private void fill()
           throws java.io.IOException
Throws:
java.io.IOException

readFully

private void readFully(byte[] b)
                throws java.io.IOException
Throws:
java.io.IOException

readDataDescriptor

private void readDataDescriptor()
                         throws java.io.IOException
Throws:
java.io.IOException

supportsDataDescriptorFor

private boolean supportsDataDescriptorFor(ZipArchiveEntry entry)
Whether this entry requires a data descriptor this library can work with.

Returns:
true if allowStoredEntriesWithDataDescriptor is true, the entry doesn't require any data descriptor or the method is DEFLATED.

readStoredEntry

private void readStoredEntry()
                      throws java.io.IOException
Caches a stored entry that uses the data descriptor.

After calling this method the entry should know its size, the entry's data is cached and the stream is positioned at the next local file or central directory header.

Throws:
java.io.IOException