public class ZipInputStream extends InflaterInputStream
FilterInputStream
that
decompresses data from an InputStream
containing a ZIP archive.
A ZIP archive is a collection of (possibly) compressed files.
When reading from a ZipInputStream
, you retrieve the
entry's metadata with getNextEntry
before you can read the userdata.
Although InflaterInputStream
can only read compressed ZIP archive
entries, this class can read non-compressed entries as well.
Use ZipFile
if you can access the archive as a file directly,
especially if you want random access to entries, rather than needing to
iterate over all entries.
Using ZipInputStream
is a little more complicated than GZIPInputStream
because ZIP archives are containers that can contain multiple files. This code pulls all the
files out of a ZIP archive, 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(); }
Modifier and Type | Field and Description |
---|---|
static int |
CENATT |
static int |
CENATX |
static int |
CENCOM |
static int |
CENCRC |
static int |
CENDSK |
static int |
CENEXT |
static int |
CENFLG |
static int |
CENHDR |
static int |
CENHOW |
static int |
CENLEN |
static int |
CENNAM |
static int |
CENOFF |
static long |
CENSIG |
static int |
CENSIZ |
static int |
CENTIM |
static int |
CENVEM |
static int |
CENVER |
static int |
ENDCOM |
static int |
ENDHDR |
static int |
ENDOFF |
static long |
ENDSIG |
static int |
ENDSIZ |
static int |
ENDSUB |
static int |
ENDTOT |
static int |
EXTCRC |
static int |
EXTHDR |
static int |
EXTLEN |
static long |
EXTSIG |
static int |
EXTSIZ |
static int |
LOCCRC |
static int |
LOCEXT |
static int |
LOCFLG |
static int |
LOCHDR |
static int |
LOCHOW |
static int |
LOCLEN |
static int |
LOCNAM |
static long |
LOCSIG |
static int |
LOCSIZ |
static int |
LOCTIM |
static int |
LOCVER |
buf, inf, len
in
Constructor and Description |
---|
ZipInputStream(InputStream stream)
Constructs a new
ZipInputStream from the specified input stream. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns 0 when when this stream has exhausted its input; and 1 otherwise.
|
void |
close()
Closes this
ZipInputStream . |
void |
closeEntry()
Closes the current ZIP entry and positions to read the next entry.
|
protected ZipEntry |
createZipEntry(String name)
creates a
ZipEntry with the given name. |
ZipEntry |
getNextEntry()
Reads the next entry from this
ZipInputStream or null if
no more entries are present. |
int |
read(byte[] buffer,
int offset,
int byteCount)
Reads up to the specified number of uncompressed bytes into the buffer
starting at the offset.
|
fill, mark, markSupported, read, reset, skip
read
public static final long LOCSIG
public static final long EXTSIG
public static final long CENSIG
public static final long ENDSIG
public static final int LOCHDR
public static final int EXTHDR
public static final int CENHDR
public static final int ENDHDR
public static final int LOCVER
public static final int LOCFLG
public static final int LOCHOW
public static final int LOCTIM
public static final int LOCCRC
public static final int LOCSIZ
public static final int LOCLEN
public static final int LOCNAM
public static final int LOCEXT
public static final int EXTCRC
public static final int EXTSIZ
public static final int EXTLEN
public static final int CENVEM
public static final int CENVER
public static final int CENFLG
public static final int CENHOW
public static final int CENTIM
public static final int CENCRC
public static final int CENSIZ
public static final int CENLEN
public static final int CENNAM
public static final int CENEXT
public static final int CENCOM
public static final int CENDSK
public static final int CENATT
public static final int CENATX
public static final int CENOFF
public static final int ENDSUB
public static final int ENDTOT
public static final int ENDSIZ
public static final int ENDOFF
public static final int ENDCOM
public ZipInputStream(InputStream stream)
ZipInputStream
from the specified input stream.stream
- the input stream to representing a ZIP archive.public void close() throws IOException
ZipInputStream
.close
in interface Closeable
close
in interface AutoCloseable
close
in class InflaterInputStream
IOException
- if an IOException
occurs.public void closeEntry() throws IOException
IOException
- if an IOException
occurs.public ZipEntry getNextEntry() throws IOException
ZipInputStream
or null
if
no more entries are present.ZipEntry
contained in the input stream.IOException
- if an IOException
occurs.ZipEntry
public int read(byte[] buffer, int offset, int byteCount) throws IOException
read
in class InflaterInputStream
buffer
- the byte array in which to store the bytes read.offset
- the initial position in buffer
to store the bytes
read from this stream.byteCount
- the maximum number of bytes to store in buffer
.IOException
- if this stream is closed or another I/O error occurs.public int available() throws IOException
InflaterInputStream
Although consistent with the RI, this behavior is inconsistent with
InputStream.available()
, and violates the Liskov
Substitution Principle. This method should not be used.
available
in class InflaterInputStream
IOException
- if this stream is closed or an error occurs