Known Direct Subclasses
JarInputStream |
The input stream from which the JAR file to be read may be fetched. |
|
Class Overview
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.
Example
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();
}
Summary
Public Constructors |
|
ZipInputStream(InputStream stream)
Constructs a new ZipInputStream to read zip entries from the given input stream.
|
Public Methods |
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 prepares to read the next entry.
|
ZipEntry
|
getNextEntry()
Returns the next entry from this ZipInputStream or null if
no more entries are present.
|
int
|
read(byte[] buffer, int byteOffset, int byteCount)
Reads up to byteCount uncompressed bytes into the buffer
starting at byteOffset .
|
[Expand]
Inherited Methods |
From class
java.util.zip.InflaterInputStream
|
From class
java.io.FilterInputStream
|
From class
java.io.InputStream
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
|
From interface
java.io.Closeable
abstract
void
|
close()
Closes the object and release any system resources it holds.
|
|
From interface
java.lang.AutoCloseable
abstract
void
|
close()
Closes the object and release any system resources it holds.
|
|
Constants
public
static
final
int
CENATT
Constant Value:
36
(0x00000024)
public
static
final
int
CENATX
Constant Value:
38
(0x00000026)
public
static
final
int
CENCOM
Constant Value:
32
(0x00000020)
public
static
final
int
CENCRC
Constant Value:
16
(0x00000010)
public
static
final
int
CENDSK
Constant Value:
34
(0x00000022)
public
static
final
int
CENEXT
Constant Value:
30
(0x0000001e)
public
static
final
int
CENFLG
Constant Value:
8
(0x00000008)
public
static
final
int
CENHDR
Constant Value:
46
(0x0000002e)
public
static
final
int
CENHOW
Constant Value:
10
(0x0000000a)
public
static
final
int
CENLEN
Constant Value:
24
(0x00000018)
public
static
final
int
CENNAM
Constant Value:
28
(0x0000001c)
public
static
final
int
CENOFF
Constant Value:
42
(0x0000002a)
public
static
final
long
CENSIG
Constant Value:
33639248
(0x0000000002014b50)
public
static
final
int
CENSIZ
Constant Value:
20
(0x00000014)
public
static
final
int
CENTIM
Constant Value:
12
(0x0000000c)
public
static
final
int
CENVEM
Constant Value:
4
(0x00000004)
public
static
final
int
CENVER
Constant Value:
6
(0x00000006)
public
static
final
int
ENDCOM
Constant Value:
20
(0x00000014)
public
static
final
int
ENDHDR
Constant Value:
22
(0x00000016)
public
static
final
int
ENDOFF
Constant Value:
16
(0x00000010)
public
static
final
long
ENDSIG
Constant Value:
101010256
(0x0000000006054b50)
public
static
final
int
ENDSIZ
Constant Value:
12
(0x0000000c)
public
static
final
int
ENDSUB
Constant Value:
8
(0x00000008)
public
static
final
int
ENDTOT
Constant Value:
10
(0x0000000a)
public
static
final
int
EXTCRC
Constant Value:
4
(0x00000004)
public
static
final
int
EXTHDR
Constant Value:
16
(0x00000010)
public
static
final
int
EXTLEN
Constant Value:
12
(0x0000000c)
public
static
final
long
EXTSIG
Constant Value:
134695760
(0x0000000008074b50)
public
static
final
int
EXTSIZ
Constant Value:
8
(0x00000008)
public
static
final
int
LOCCRC
Constant Value:
14
(0x0000000e)
public
static
final
int
LOCEXT
Constant Value:
28
(0x0000001c)
public
static
final
int
LOCFLG
Constant Value:
6
(0x00000006)
public
static
final
int
LOCHDR
Constant Value:
30
(0x0000001e)
public
static
final
int
LOCHOW
Constant Value:
8
(0x00000008)
public
static
final
int
LOCLEN
Constant Value:
22
(0x00000016)
public
static
final
int
LOCNAM
Constant Value:
26
(0x0000001a)
public
static
final
long
LOCSIG
Constant Value:
67324752
(0x0000000004034b50)
public
static
final
int
LOCSIZ
Constant Value:
18
(0x00000012)
public
static
final
int
LOCTIM
Constant Value:
10
(0x0000000a)
public
static
final
int
LOCVER
Constant Value:
4
(0x00000004)
Public Constructors
public
ZipInputStream
(InputStream stream)
Constructs a new ZipInputStream
to read zip entries from the given input stream.
Public Methods
public
int
available
()
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.
Returns
- 0 if no further bytes are available. Otherwise returns 1,
which suggests (but does not guarantee) that additional bytes are
available.
public
void
close
()
Closes this ZipInputStream
.
public
void
closeEntry
()
Closes the current zip entry and prepares to read the next entry.
public
ZipEntry
getNextEntry
()
Returns the next entry from this ZipInputStream
or null
if
no more entries are present.
public
int
read
(byte[] buffer, int byteOffset, int byteCount)
Reads up to byteCount
uncompressed bytes into the buffer
starting at byteOffset
. Returns the number of bytes actually read, or -1.
Protected Methods
protected
ZipEntry
createZipEntry
(String name)
Parameters
name
| the name of the entry. |