E
- the type of the ZIP entries.@NotThreadSafe public abstract class RawZipFile<E extends ZipEntry> extends Object implements Iterable<E>, Closeable
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.
RawZipOutputStream
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 |
RawZipFile(ReadOnlyFile zip,
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.
|
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) . |
InputStream |
getCheckedInputStream(ZipEntry entry)
Equivalent to
getInputStream(entry.getName(), true, true) instead. |
String |
getComment()
Returns the file comment.
|
protected abstract ZipCryptoParameters |
getCryptoParameters()
Returns the parameters for encryption or authentication of entries.
|
E |
getEntry(String name)
Returns the entry for the given name or
null if no entry with
this name exists. |
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 |
getInputStream(ZipEntry entry)
Equivalent to
getInputStream(entry.getName(), null, true) instead. |
InputStream |
getPostambleInputStream()
Returns an
InputStream to read 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 read 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. |
void |
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 RawZipFile(@WillCloseWhenClosed ReadOnlyFile zip, ZipFileParameters<E> param) throws IOException
zip
file in order to provide random access
to its entries.zip
- the ZIP file to be read.param
- the parameters for reading the ZIP file.ZipException
- if the file is not compatible to the ZIP
File Format Specification.IOException
- on any other I/O related issue.recoverLostEntries()
public boolean busy()
true
if and only if this ZIP file is busy reading
one or more entries.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an error occurs closing the file.public String getCharset()
@CreatesObligation @Nullable public final InputStream getCheckedInputStream(String name) throws IOException
getInputStream(name, true, true)
.IOException
@CreatesObligation @Nullable public final InputStream getCheckedInputStream(ZipEntry entry) throws IOException
getInputStream(entry.getName(), true, true)
instead.IOException
@Nullable public String getComment()
@CheckForNull protected abstract ZipCryptoParameters getCryptoParameters()
public E getEntry(String name)
null
if no entry with
this name exists.
Note that the returned entry is shared with this instance.
It is illegal to change its state!name
- the name of the ZIP entry.@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 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 @Nullable public final InputStream getInputStream(ZipEntry entry) throws IOException
getInputStream(entry.getName(), null, true)
instead.IOException
@CreatesObligation public InputStream getPostambleInputStream() throws IOException
InputStream
to read 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 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).
ZipException
- If this ZIP file has been closed.IOException
public long getPostambleLength()
@CreatesObligation public InputStream getPreambleInputStream() throws IOException
InputStream
to read 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 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).
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 void recoverLostEntries() throws 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()
.
ZipException
- if an invalid entry is found.IOException
- if any other I/O error occurs.public int size()
Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.