java.lang.Object | ||||
↳ | java.io.InputStream | |||
↳ | java.io.FilterInputStream | |||
↳ | java.util.zip.InflaterInputStream | |||
↳ | java.util.zip.ZipInputStream |
Known Direct Subclasses |
Used to read (decompress) the data from zip files.
A zip file (or "archive") is a collection of (possibly) compressed files.
When reading from a ZipInputStream
, you call getNextEntry()
which returns a ZipEntry
of metadata corresponding to the userdata that follows.
When you appear to have hit the end of this stream (which is really just the end of the current
entry's userdata), call getNextEntry
again. When it returns null,
there are no more entries in the input file.
Although InflaterInputStream
can only read compressed zip
entries, this class can read non-compressed entries as well.
Use ZipFile
if you need random access to entries by name, but use this class
if you just want to iterate over all entries.
Using ZipInputStream
is a little more complicated than GZIPInputStream
because zip files are containers that can contain multiple files. This code pulls all the
files out of a zip file, similar to the unzip(1)
utility.
InputStream is = ... ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); try { ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); } String filename = ze.getName(); byte[] bytes = baos.toByteArray(); // do something with 'filename' and 'bytes'... } } finally { zis.close(); }
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | CENATT | ||||||||||
int | CENATX | ||||||||||
int | CENCOM | ||||||||||
int | CENCRC | ||||||||||
int | CENDSK | ||||||||||
int | CENEXT | ||||||||||
int | CENFLG | ||||||||||
int | CENHDR | ||||||||||
int | CENHOW | ||||||||||
int | CENLEN | ||||||||||
int | CENNAM | ||||||||||
int | CENOFF | ||||||||||
long | CENSIG | ||||||||||
int | CENSIZ | ||||||||||
int | CENTIM | ||||||||||
int | CENVEM | ||||||||||
int | CENVER | ||||||||||
int | ENDCOM | ||||||||||
int | ENDHDR | ||||||||||
int | ENDOFF | ||||||||||
long | ENDSIG | ||||||||||
int | ENDSIZ | ||||||||||
int | ENDSUB | ||||||||||
int | ENDTOT | ||||||||||
int | EXTCRC | ||||||||||
int | EXTHDR | ||||||||||
int | EXTLEN | ||||||||||
long | EXTSIG | ||||||||||
int | EXTSIZ | ||||||||||
int | LOCCRC | ||||||||||
int | LOCEXT | ||||||||||
int | LOCFLG | ||||||||||
int | LOCHDR | ||||||||||
int | LOCHOW | ||||||||||
int | LOCLEN | ||||||||||
int | LOCNAM | ||||||||||
long | LOCSIG | ||||||||||
int | LOCSIZ | ||||||||||
int | LOCTIM | ||||||||||
int | LOCVER |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.zip.InflaterInputStream
| |||||||||||
From class
java.io.FilterInputStream
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
ZipInputStream to read zip entries from the given input stream.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns 0 when when this stream has exhausted its input; and 1 otherwise.
| |||||||||||
Closes this
ZipInputStream .
| |||||||||||
Closes the current zip entry and prepares to read the next entry.
| |||||||||||
Returns the next entry from this
ZipInputStream or null if
no more entries are present.
| |||||||||||
Reads up to
byteCount uncompressed bytes into the buffer
starting at byteOffset .
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
creates a
ZipEntry with the given name.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.zip.InflaterInputStream
| |||||||||||
From class
java.io.FilterInputStream
| |||||||||||
From class
java.io.InputStream
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
java.lang.AutoCloseable
|
Constructs a new ZipInputStream
to read zip entries from the given input stream.
UTF-8 is used to decode all strings in the file.
Returns 0 when when this stream has exhausted its input; and 1 otherwise. A result of 1 does not guarantee that further bytes can be returned, with or without blocking.
Although consistent with the RI, this behavior is inconsistent with
available()
, and violates the Liskov
Substitution Principle. This method should not be used.
IOException |
---|
Closes this ZipInputStream
.
IOException | if an IOException occurs.
|
---|
Closes the current zip entry and prepares to read the next entry.
IOException | if an IOException occurs.
|
---|
Returns the next entry from this ZipInputStream
or null
if
no more entries are present.
IOException | if an IOException occurs.
|
---|
Reads up to byteCount
uncompressed bytes into the buffer
starting at byteOffset
. Returns the number of bytes actually read, or -1.
IOException |
---|
creates a ZipEntry
with the given name.
name | the name of the entry. |
---|
ZipEntry
.