E
- the type of the ZIP entries.@NotThreadSafe @CleanupObligation public abstract class AbstractZipFile<E extends ZipEntry> extends Object implements Closeable, Iterable<E>
ZipEntry
instances.
Warning: This class is not intended for public use - its API may change at will without prior notification!
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 Flags,
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.
AbstractZipOutputStream
Modifier and Type | Field and Description |
---|---|
static Charset |
DEFAULT_CHARSET
The default character set used for entry names and comments in ZIP files.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractZipFile(net.java.truecommons.io.Source source,
ZipFileParameters<E> param)
Reads the given
zip file in order to provide random access
to its entries. |
Modifier and Type | Method and Description |
---|---|
boolean |
busy()
Returns
true if and only if this ZIP file is busy reading
one or more entries. |
void |
close()
Closes the file.
|
E |
entry(String name)
Returns the entry for the given
name or null if no entry
with this name exists in this ZIP file. |
String |
getCharset()
Returns the name of the character set which is effectively used for
decoding entry names and the file comment.
|
InputStream |
getCheckedInputStream(String name)
Equivalent to
getInputStream(name, true, true) . |
String |
getComment()
Returns the file comment.
|
protected abstract ZipCryptoParameters |
getCryptoParameters()
Returns the parameters for encryption or authentication of entries.
|
InputStream |
getInputStream(String name)
Equivalent to
getInputStream(name, null, true) . |
protected InputStream |
getInputStream(String name,
Boolean check,
boolean process)
Returns an
InputStream for reading the contents of the given
entry. |
InputStream |
getPostambleInputStream()
Returns an
InputStream to load the postamble of this ZIP file. |
long |
getPostambleLength()
Returns the size of the postamble of this ZIP file in bytes.
|
InputStream |
getPreambleInputStream()
Returns an
InputStream to load the preamble of this ZIP file. |
long |
getPreambleLength()
Returns the size of the preamble of this ZIP file in bytes.
|
Charset |
getRawCharset()
Returns the character set which is effectively used for
decoding entry names and the file comment.
|
Iterator<E> |
iterator()
Returns an iteration of all entries in this ZIP file.
|
long |
length()
Returns the file length of this ZIP file in bytes.
|
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. |
AbstractZipFile<E> |
recoverLostEntries()
Recovers any lost entries which have been added to the ZIP file after
the (last) End Of Central Directory Record (EOCDR).
|
int |
size()
Returns the number of entries in this ZIP file.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public static final Charset DEFAULT_CHARSET
"UTF-8"
for compatibility with Sun's JDK implementation.
Note that you should use "IBM437" for ordinary ZIP files
instead.@CreatesObligation protected AbstractZipFile(net.java.truecommons.io.Source source, ZipFileParameters<E> param) throws ZipException, EOFException, IOException
zip
file in order to provide random access
to its entries.source
- the source for reading the ZIP file from.param
- the parameters for reading the ZIP file.ZipException
- if the source data is not compatible to the ZIP
File Format Specification.EOFException
- on unexpected end-of-file.IOException
- on any I/O error.recoverLostEntries()
public boolean busy()
true
if and only if this ZIP file is busy reading
one or more entries.@DischargesObligation public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an error occurs closing the file.public E entry(String name)
name
or null
if no entry
with this name exists in this ZIP file.
Note that the returned entry is shared with this instance - it is an
error to change its state!name
- the name of the ZIP entry.name
or null
if no entry
with this name exists in this ZIP file.public String getCharset()
@CreatesObligation @Nullable public final InputStream getCheckedInputStream(String name) throws IOException
getInputStream(name, true, true)
.IOException
@Nullable public String getComment()
@CheckForNull protected abstract ZipCryptoParameters getCryptoParameters()
@CreatesObligation @Nullable public final InputStream getInputStream(String name) throws IOException
getInputStream(name, null, true)
.IOException
@CreatesObligation @Nullable protected InputStream getInputStream(String name, @CheckForNull Boolean check, boolean process) throws ZipException, IOException
InputStream
for reading the contents of the given
entry.
If the close()
method is called on this instance, all input
streams returned by this method are closed, too.
name
- The name of the entry to get the stream for.check
- Whether or not the entry content gets checked/authenticated.
If the parameter process
is false
, then this
parameter is ignored.
Otherwise, if this parameter is null
, then it is set to
the ZipEntry.isEncrypted()
property of the given entry.
Finally, if this parameter is true
,
then the following additional check is performed for the entry:
ZipAuthenticationException
gets thrown from this
method (pre-check).
Crc32Exception
gets thrown when InputStream.close()
is called on the
returned entry stream (post-check).
process
- Whether or not the entry contents should get processed,
e.g. inflated.
This should be set to false
if and only if the
application is going to copy entries from an input ZIP file to
an output ZIP file.null
if the
entry does not exist.ZipAuthenticationException
- If the entry is encrypted and
checking the MAC fails.ZipException
- If this file is not compatible to the ZIP File
Format Specification.IOException
- If the entry cannot get read from this ZipFile.@CreatesObligation public InputStream getPostambleInputStream() throws IOException
InputStream
to load the postamble of this ZIP file.
Note that the returned stream is a lightweight stream,
i.e. there is no external resource such as a SeekableByteChannel
allocated for it. Instead, all streams returned by this method share
the underlying SeekableByteChannel
of this ZipFile
.
This allows to close this object (and hence the underlying
SeekableByteChannel
) 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).
ZipException
- If this ZIP file has been closed.IOException
public long getPostambleLength()
@CreatesObligation public InputStream getPreambleInputStream() throws IOException
InputStream
to load the preamble of this ZIP file.
Note that the returned stream is a lightweight stream,
i.e. there is no external resource such as a SeekableByteChannel
allocated for it. Instead, all streams returned by this method share
the underlying SeekableByteChannel
of this ZipFile
.
This allows to close this object (and hence the underlying
SeekableByteChannel
) 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).
ZipException
- If this ZIP file has been closed.IOException
public long getPreambleLength()
public Charset getRawCharset()
public Iterator<E> iterator()
public long length()
public boolean offsetsConsiderPreamble()
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.
public AbstractZipFile<E> recoverLostEntries() throws ZipException, EOFException, IOException
crypto parameters
.
This method starts parsing entries at the start of the postamble and continues until it hits EOF or any non-entry data. As a side effect, it will not only add any found entries to its internal map, but will also cut the start of the postamble accordingly.
Note that it's very likely that this method terminates with an
exception unless the postamble is empty or contains only valid ZIP
entries.
Therefore it may be a good idea to log or silently ignore any exception
thrown by this method.
If an exception is thrown, you can check and recover the remaining
postamble for post-mortem analysis by calling
getPostambleLength()
and getPostambleInputStream()
.
this
ZipException
- if an invalid entry is found.EOFException
- on unexpected end-of-file.IOException
- on any I/O error.public int size()
Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.