Constructor: |
public ZipFile(String name) throws IOException {
this(new File(name), OPEN_READ);
}
Opens a zip file for reading.
First, if there is a security manager, its checkRead
method is called with the name argument as its argument
to ensure the read is allowed.
The UTF-8 charset is used to
decode the entry names and comments. Parameters:
name - the name of the zip file
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
SecurityException - if a security manager exists and its
checkRead method doesn't allow read access to the file.
Also see:
- SecurityManager#checkRead(java.lang.String)
|
public ZipFile(File file) throws ZipException, IOException {
this(file, OPEN_READ);
}
Parameters:
file - the ZIP file to be opened for reading
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
|
public ZipFile(File file,
int mode) throws IOException {
this(file, mode, StandardCharsets.UTF_8);
}
Opens a new ZipFile to read from the specified
File object in the specified mode. The mode argument
must be either OPEN_READ or OPEN_READ | OPEN_DELETE.
First, if there is a security manager, its checkRead
method is called with the name argument as its argument to
ensure the read is allowed.
The UTF-8 charset is used to
decode the entry names and comments Parameters:
file - the ZIP file to be opened for reading
mode - the mode in which the file is to be opened
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
SecurityException - if a security manager exists and
its checkRead method
doesn't allow read access to the file,
or its checkDelete method doesn't allow deleting
the file when the OPEN_DELETE flag is set.
IllegalArgumentException - if the mode argument is invalid
Also see:
- SecurityManager#checkRead(java.lang.String)
- since:
1.3 -
|
public ZipFile(String name,
Charset charset) throws IOException {
this(new File(name), OPEN_READ, charset);
}
Opens a zip file for reading.
First, if there is a security manager, its checkRead
method is called with the name argument as its argument
to ensure the read is allowed. Parameters:
name - the name of the zip file
charset -
the {@linkplain java.nio.charset.Charset charset} to
be used to decode the ZIP entry name and comment that are not
encoded by using UTF-8 encoding (indicated by entry's general
purpose flag).
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
SecurityException -
if a security manager exists and its checkRead
method doesn't allow read access to the file
Also see:
- SecurityManager#checkRead(java.lang.String)
- since:
1.7 -
|
public ZipFile(File file,
Charset charset) throws IOException {
this(file, OPEN_READ, charset);
}
Opens a ZIP file for reading given the specified File object. Parameters:
file - the ZIP file to be opened for reading
charset -
The {@linkplain java.nio.charset.Charset charset} to be
used to decode the ZIP entry name and comment (ignored if
the language
encoding bit of the ZIP entry's general purpose bit
flag is set).
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
- since:
1.7 -
|
public ZipFile(File file,
int mode,
Charset charset) throws IOException {
if (((mode & OPEN_READ) == 0) ||
((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
throw new IllegalArgumentException("Illegal mode: 0x"+
Integer.toHexString(mode));
}
String name = file.getPath();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkRead(name);
if ((mode & OPEN_DELETE) != 0) {
sm.checkDelete(name);
}
}
if (charset == null)
throw new NullPointerException("charset is null");
this.zc = ZipCoder.get(charset);
long t0 = System.nanoTime();
jzfile = open(name, mode, file.lastModified(), usemmap);
sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
sun.misc.PerfCounter.getZipFileCount().increment();
this.name = name;
this.total = getTotal(jzfile);
}
Opens a new ZipFile to read from the specified
File object in the specified mode. The mode argument
must be either OPEN_READ or OPEN_READ | OPEN_DELETE.
First, if there is a security manager, its checkRead
method is called with the name argument as its argument to
ensure the read is allowed. Parameters:
file - the ZIP file to be opened for reading
mode - the mode in which the file is to be opened
charset -
the {@linkplain java.nio.charset.Charset charset} to
be used to decode the ZIP entry name and comment that are not
encoded by using UTF-8 encoding (indicated by entry's general
purpose flag).
Throws:
ZipException - if a ZIP format error has occurred
IOException - if an I/O error has occurred
SecurityException -
if a security manager exists and its checkRead
method doesn't allow read access to the file,or its
checkDelete method doesn't allow deleting the
file when the OPEN_DELETE flag is set
IllegalArgumentException - if the mode argument is invalid
Also see:
- SecurityManager#checkRead(java.lang.String)
- since:
1.7 -
|
Method from java.util.zip.ZipFile Detail: |
public void close() throws IOException {
if (closeRequested)
return;
closeRequested = true;
synchronized (this) {
// Close streams, release their inflaters
synchronized (streams) {
if (false == streams.isEmpty()) {
Map< InputStream, Inflater > copy = new HashMap< >(streams);
streams.clear();
for (Map.Entry< InputStream, Inflater > e : copy.entrySet()) {
e.getKey().close();
Inflater inf = e.getValue();
if (inf != null) {
inf.end();
}
}
}
}
// Release cached inflaters
Inflater inf;
synchronized (inflaterCache) {
while (null != (inf = inflaterCache.poll())) {
inf.end();
}
}
if (jzfile != 0) {
// Close the zip file
long zf = this.jzfile;
jzfile = 0;
close(zf);
}
}
}
Closes the ZIP file.
Closing this ZIP file will close all of the input streams
previously returned by invocations of the
getInputStream method. |
public Enumeration<ZipEntry> entries() {
ensureOpen();
return new Enumeration< ZipEntry >() {
private int i = 0;
public boolean hasMoreElements() {
synchronized (ZipFile.this) {
ensureOpen();
return i < total;
}
}
public ZipEntry nextElement() throws NoSuchElementException {
synchronized (ZipFile.this) {
ensureOpen();
if (i >= total) {
throw new NoSuchElementException();
}
long jzentry = getNextEntry(jzfile, i++);
if (jzentry == 0) {
String message;
if (closeRequested) {
message = "ZipFile concurrently closed";
} else {
message = getZipMessage(ZipFile.this.jzfile);
}
throw new ZipError("jzentry == 0" +
",\n jzfile = " + ZipFile.this.jzfile +
",\n total = " + ZipFile.this.total +
",\n name = " + ZipFile.this.name +
",\n i = " + i +
",\n message = " + message
);
}
ZipEntry ze = getZipEntry(null, jzentry);
freeEntry(jzfile, jzentry);
return ze;
}
}
};
}
Returns an enumeration of the ZIP file entries. |
protected void finalize() throws IOException {
close();
}
Ensures that the system resources held by this ZipFile object are
released when there are no more references to it.
Since the time when GC would invoke this method is undetermined,
it is strongly recommended that applications invoke the close
method as soon they have finished accessing this ZipFile .
This will prevent holding up system resources for an undetermined
length of time. |
public String getComment() {
synchronized (this) {
ensureOpen();
byte[] bcomm = getCommentBytes(jzfile);
if (bcomm == null)
return null;
return zc.toString(bcomm, bcomm.length);
}
}
Returns the zip file comment, or null if none. |
public ZipEntry getEntry(String name) {
if (name == null) {
throw new NullPointerException("name");
}
long jzentry = 0;
synchronized (this) {
ensureOpen();
jzentry = getEntry(jzfile, zc.getBytes(name), true);
if (jzentry != 0) {
ZipEntry ze = getZipEntry(name, jzentry);
freeEntry(jzfile, jzentry);
return ze;
}
}
return null;
}
Returns the zip file entry for the specified name, or null
if not found. |
public InputStream getInputStream(ZipEntry entry) throws IOException {
if (entry == null) {
throw new NullPointerException("entry");
}
long jzentry = 0;
ZipFileInputStream in = null;
synchronized (this) {
ensureOpen();
if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
} else {
jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
}
if (jzentry == 0) {
return null;
}
in = new ZipFileInputStream(jzentry);
switch (getEntryMethod(jzentry)) {
case STORED:
synchronized (streams) {
streams.put(in, null);
}
return in;
case DEFLATED:
// MORE: Compute good size for inflater stream:
long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
if (size > 65536) size = 8192;
if (size < = 0) size = 4096;
Inflater inf = getInflater();
InputStream is =
new ZipFileInflaterInputStream(in, inf, (int)size);
synchronized (streams) {
streams.put(is, inf);
}
return is;
default:
throw new ZipException("invalid compression method");
}
}
}
Returns an input stream for reading the contents of the specified
zip file entry.
Closing this ZIP file will, in turn, close all input
streams that have been returned by invocations of this method. |
public String getName() {
return name;
}
Returns the path name of the ZIP file. |
public int size() {
ensureOpen();
return total;
}
Returns the number of entries in the ZIP file. |