0N/A/*
3909N/A * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.zip;
0N/A
1332N/Aimport java.io.Closeable;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.EOFException;
0N/Aimport java.io.File;
1107N/Aimport java.nio.charset.Charset;
4213N/Aimport java.nio.charset.StandardCharsets;
4023N/Aimport java.util.ArrayDeque;
4023N/Aimport java.util.Deque;
0N/Aimport java.util.Enumeration;
4023N/Aimport java.util.HashMap;
4023N/Aimport java.util.Map;
0N/Aimport java.util.NoSuchElementException;
4023N/Aimport java.util.WeakHashMap;
2227N/Aimport java.security.AccessController;
2227N/Aimport sun.security.action.GetPropertyAction;
1107N/Aimport static java.util.zip.ZipConstants64.*;
0N/A
0N/A/**
0N/A * This class is used to read entries from a zip file.
0N/A *
0N/A * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
0N/A * or method in this class will cause a {@link NullPointerException} to be
0N/A * thrown.
0N/A *
0N/A * @author David Connelly
0N/A */
0N/Apublic
1332N/Aclass ZipFile implements ZipConstants, Closeable {
6329N/A private long jzfile; // address of jzfile data
6329N/A private final String name; // zip file name
6329N/A private final int total; // total number of entries
6329N/A private final boolean locsig; // if zip file starts with LOCSIG (usually true)
4023N/A private volatile boolean closeRequested = false;
0N/A
0N/A private static final int STORED = ZipEntry.STORED;
0N/A private static final int DEFLATED = ZipEntry.DEFLATED;
0N/A
0N/A /**
0N/A * Mode flag to open a zip file for reading.
0N/A */
0N/A public static final int OPEN_READ = 0x1;
0N/A
0N/A /**
0N/A * Mode flag to open a zip file and mark it for deletion. The file will be
0N/A * deleted some time between the moment that it is opened and the moment
0N/A * that it is closed, but its contents will remain accessible via the
0N/A * <tt>ZipFile</tt> object until either the close method is invoked or the
0N/A * virtual machine exits.
0N/A */
0N/A public static final int OPEN_DELETE = 0x4;
0N/A
0N/A static {
0N/A /* Zip library is loaded from System.initializeSystemClass */
0N/A initIDs();
0N/A }
0N/A
0N/A private static native void initIDs();
0N/A
2227N/A private static final boolean usemmap;
2227N/A
2227N/A static {
2227N/A // A system prpperty to disable mmap use to avoid vm crash when
2227N/A // in-use zip file is accidently overwritten by others.
2916N/A String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping");
2227N/A usemmap = (prop == null ||
2227N/A !(prop.length() == 0 || prop.equalsIgnoreCase("true")));
2227N/A }
2227N/A
0N/A /**
0N/A * Opens a zip file for reading.
0N/A *
1107N/A * <p>First, if there is a security manager, its <code>checkRead</code>
1107N/A * method is called with the <code>name</code> argument as its argument
1107N/A * to ensure the read is allowed.
1107N/A *
1107N/A * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
1107N/A * decode the entry names and comments.
0N/A *
0N/A * @param name the name of the zip file
0N/A * @throws ZipException if a ZIP format error has occurred
0N/A * @throws IOException if an I/O error has occurred
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method doesn't allow read access to the file.
1107N/A *
0N/A * @see SecurityManager#checkRead(java.lang.String)
0N/A */
0N/A public ZipFile(String name) throws IOException {
0N/A this(new File(name), OPEN_READ);
0N/A }
0N/A
0N/A /**
0N/A * Opens a new <code>ZipFile</code> to read from the specified
0N/A * <code>File</code> object in the specified mode. The mode argument
0N/A * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkRead</code>
0N/A * method is called with the <code>name</code> argument as its argument to
0N/A * ensure the read is allowed.
0N/A *
1107N/A * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
1107N/A * decode the entry names and comments
1107N/A *
0N/A * @param file the ZIP file to be opened for reading
0N/A * @param mode the mode in which the file is to be opened
0N/A * @throws ZipException if a ZIP format error has occurred
0N/A * @throws IOException if an I/O error has occurred
0N/A * @throws SecurityException if a security manager exists and
0N/A * its <code>checkRead</code> method
0N/A * doesn't allow read access to the file,
0N/A * or its <code>checkDelete</code> method doesn't allow deleting
0N/A * the file when the <tt>OPEN_DELETE</tt> flag is set.
0N/A * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
0N/A * @see SecurityManager#checkRead(java.lang.String)
0N/A * @since 1.3
0N/A */
0N/A public ZipFile(File file, int mode) throws IOException {
4213N/A this(file, mode, StandardCharsets.UTF_8);
1107N/A }
1107N/A
1107N/A /**
1107N/A * Opens a ZIP file for reading given the specified File object.
1107N/A *
1107N/A * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
1107N/A * decode the entry names and comments.
1107N/A *
1107N/A * @param file the ZIP file to be opened for reading
1107N/A * @throws ZipException if a ZIP format error has occurred
1107N/A * @throws IOException if an I/O error has occurred
1107N/A */
1107N/A public ZipFile(File file) throws ZipException, IOException {
1107N/A this(file, OPEN_READ);
1107N/A }
1107N/A
1107N/A private ZipCoder zc;
1107N/A
1107N/A /**
1107N/A * Opens a new <code>ZipFile</code> to read from the specified
1107N/A * <code>File</code> object in the specified mode. The mode argument
1107N/A * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
1107N/A *
1107N/A * <p>First, if there is a security manager, its <code>checkRead</code>
1107N/A * method is called with the <code>name</code> argument as its argument to
1107N/A * ensure the read is allowed.
1107N/A *
1107N/A * @param file the ZIP file to be opened for reading
1107N/A * @param mode the mode in which the file is to be opened
1107N/A * @param charset
1190N/A * the {@linkplain java.nio.charset.Charset charset} to
1107N/A * be used to decode the ZIP entry name and comment that are not
1107N/A * encoded by using UTF-8 encoding (indicated by entry's general
1107N/A * purpose flag).
1107N/A *
1107N/A * @throws ZipException if a ZIP format error has occurred
1107N/A * @throws IOException if an I/O error has occurred
1107N/A *
1107N/A * @throws SecurityException
1107N/A * if a security manager exists and its <code>checkRead</code>
1107N/A * method doesn't allow read access to the file,or its
1107N/A * <code>checkDelete</code> method doesn't allow deleting the
1107N/A * file when the <tt>OPEN_DELETE</tt> flag is set
1107N/A *
1107N/A * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
1107N/A *
1107N/A * @see SecurityManager#checkRead(java.lang.String)
1107N/A *
1107N/A * @since 1.7
1107N/A */
1107N/A public ZipFile(File file, int mode, Charset charset) throws IOException
1107N/A {
0N/A if (((mode & OPEN_READ) == 0) ||
0N/A ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
0N/A throw new IllegalArgumentException("Illegal mode: 0x"+
0N/A Integer.toHexString(mode));
0N/A }
0N/A String name = file.getPath();
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkRead(name);
0N/A if ((mode & OPEN_DELETE) != 0) {
0N/A sm.checkDelete(name);
0N/A }
0N/A }
1107N/A if (charset == null)
1107N/A throw new NullPointerException("charset is null");
1107N/A this.zc = ZipCoder.get(charset);
1660N/A long t0 = System.nanoTime();
2227N/A jzfile = open(name, mode, file.lastModified(), usemmap);
1660N/A sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
1660N/A sun.misc.PerfCounter.getZipFileCount().increment();
0N/A this.name = name;
0N/A this.total = getTotal(jzfile);
6329N/A this.locsig = startsWithLOC(jzfile);
0N/A }
0N/A
1107N/A /**
1107N/A * Opens a zip file for reading.
1107N/A *
1107N/A * <p>First, if there is a security manager, its <code>checkRead</code>
1107N/A * method is called with the <code>name</code> argument as its argument
1107N/A * to ensure the read is allowed.
1107N/A *
1107N/A * @param name the name of the zip file
1107N/A * @param charset
1190N/A * the {@linkplain java.nio.charset.Charset charset} to
1107N/A * be used to decode the ZIP entry name and comment that are not
1107N/A * encoded by using UTF-8 encoding (indicated by entry's general
1107N/A * purpose flag).
1107N/A *
1107N/A * @throws ZipException if a ZIP format error has occurred
1107N/A * @throws IOException if an I/O error has occurred
1107N/A * @throws SecurityException
1107N/A * if a security manager exists and its <code>checkRead</code>
1107N/A * method doesn't allow read access to the file
1107N/A *
1107N/A * @see SecurityManager#checkRead(java.lang.String)
1107N/A *
1107N/A * @since 1.7
1107N/A */
1107N/A public ZipFile(String name, Charset charset) throws IOException
1107N/A {
1107N/A this(new File(name), OPEN_READ, charset);
1107N/A }
0N/A
0N/A /**
0N/A * Opens a ZIP file for reading given the specified File object.
0N/A * @param file the ZIP file to be opened for reading
1107N/A * @param charset
1190N/A * The {@linkplain java.nio.charset.Charset charset} to be
1107N/A * used to decode the ZIP entry name and comment (ignored if
1107N/A * the <a href="package-summary.html#lang_encoding"> language
1107N/A * encoding bit</a> of the ZIP entry's general purpose bit
1107N/A * flag is set).
1107N/A *
1107N/A * @throws ZipException if a ZIP format error has occurred
0N/A * @throws IOException if an I/O error has occurred
1107N/A *
1107N/A * @since 1.7
0N/A */
1107N/A public ZipFile(File file, Charset charset) throws IOException
1107N/A {
1107N/A this(file, OPEN_READ, charset);
0N/A }
0N/A
0N/A /**
1332N/A * Returns the zip file comment, or null if none.
1332N/A *
1332N/A * @return the comment string for the zip file, or null if none
1332N/A *
1332N/A * @throws IllegalStateException if the zip file has been closed
1332N/A *
1332N/A * Since 1.7
1332N/A */
1332N/A public String getComment() {
1332N/A synchronized (this) {
1332N/A ensureOpen();
1332N/A byte[] bcomm = getCommentBytes(jzfile);
1332N/A if (bcomm == null)
1332N/A return null;
1332N/A return zc.toString(bcomm, bcomm.length);
1332N/A }
1332N/A }
1332N/A
1332N/A /**
0N/A * Returns the zip file entry for the specified name, or null
0N/A * if not found.
0N/A *
0N/A * @param name the name of the entry
0N/A * @return the zip file entry, or null if not found
0N/A * @throws IllegalStateException if the zip file has been closed
0N/A */
0N/A public ZipEntry getEntry(String name) {
0N/A if (name == null) {
0N/A throw new NullPointerException("name");
0N/A }
0N/A long jzentry = 0;
0N/A synchronized (this) {
0N/A ensureOpen();
1107N/A jzentry = getEntry(jzfile, zc.getBytes(name), true);
0N/A if (jzentry != 0) {
1107N/A ZipEntry ze = getZipEntry(name, jzentry);
0N/A freeEntry(jzfile, jzentry);
0N/A return ze;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
1107N/A private static native long getEntry(long jzfile, byte[] name,
0N/A boolean addSlash);
0N/A
0N/A // freeEntry releases the C jzentry struct.
0N/A private static native void freeEntry(long jzfile, long jzentry);
0N/A
4023N/A // the outstanding inputstreams that need to be closed,
4023N/A // mapped to the inflater objects they use.
4023N/A private final Map<InputStream, Inflater> streams = new WeakHashMap<>();
1239N/A
0N/A /**
0N/A * Returns an input stream for reading the contents of the specified
0N/A * zip file entry.
0N/A *
0N/A * <p> Closing this ZIP file will, in turn, close all input
0N/A * streams that have been returned by invocations of this method.
0N/A *
0N/A * @param entry the zip file entry
0N/A * @return the input stream for reading the contents of the specified
0N/A * zip file entry.
0N/A * @throws ZipException if a ZIP format error has occurred
0N/A * @throws IOException if an I/O error has occurred
0N/A * @throws IllegalStateException if the zip file has been closed
0N/A */
0N/A public InputStream getInputStream(ZipEntry entry) throws IOException {
1107N/A if (entry == null) {
1107N/A throw new NullPointerException("entry");
0N/A }
0N/A long jzentry = 0;
0N/A ZipFileInputStream in = null;
0N/A synchronized (this) {
0N/A ensureOpen();
1107N/A if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
1107N/A jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
1107N/A } else {
1107N/A jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
1107N/A }
0N/A if (jzentry == 0) {
0N/A return null;
0N/A }
0N/A in = new ZipFileInputStream(jzentry);
0N/A
3235N/A switch (getEntryMethod(jzentry)) {
3235N/A case STORED:
4023N/A synchronized (streams) {
4023N/A streams.put(in, null);
4023N/A }
3235N/A return in;
3235N/A case DEFLATED:
3235N/A // MORE: Compute good size for inflater stream:
3235N/A long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
3235N/A if (size > 65536) size = 8192;
3235N/A if (size <= 0) size = 4096;
4023N/A Inflater inf = getInflater();
4023N/A InputStream is =
4023N/A new ZipFileInflaterInputStream(in, inf, (int)size);
4023N/A synchronized (streams) {
4023N/A streams.put(is, inf);
4023N/A }
3235N/A return is;
3235N/A default:
3235N/A throw new ZipException("invalid compression method");
3235N/A }
0N/A }
0N/A }
0N/A
4023N/A private class ZipFileInflaterInputStream extends InflaterInputStream {
4023N/A private volatile boolean closeRequested = false;
4023N/A private boolean eof = false;
4023N/A private final ZipFileInputStream zfin;
4023N/A
4023N/A ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf,
4023N/A int size) {
4023N/A super(zfin, inf, size);
4023N/A this.zfin = zfin;
4023N/A }
4023N/A
4023N/A public void close() throws IOException {
4023N/A if (closeRequested)
4023N/A return;
4023N/A closeRequested = true;
4023N/A
4023N/A super.close();
4023N/A Inflater inf;
4023N/A synchronized (streams) {
4023N/A inf = streams.remove(this);
4023N/A }
4023N/A if (inf != null) {
4023N/A releaseInflater(inf);
4023N/A }
4023N/A }
4023N/A
4023N/A // Override fill() method to provide an extra "dummy" byte
4023N/A // at the end of the input stream. This is required when
4023N/A // using the "nowrap" Inflater option.
4023N/A protected void fill() throws IOException {
4023N/A if (eof) {
4023N/A throw new EOFException("Unexpected end of ZLIB input stream");
4023N/A }
4023N/A len = in.read(buf, 0, buf.length);
4023N/A if (len == -1) {
4023N/A buf[0] = 0;
4023N/A len = 1;
4023N/A eof = true;
4023N/A }
4023N/A inf.setInput(buf, 0, len);
4023N/A }
4023N/A
4023N/A public int available() throws IOException {
4023N/A if (closeRequested)
4023N/A return 0;
4023N/A long avail = zfin.size() - inf.getBytesWritten();
4023N/A return (avail > (long) Integer.MAX_VALUE ?
4023N/A Integer.MAX_VALUE : (int) avail);
4023N/A }
4023N/A
4023N/A protected void finalize() throws Throwable {
4023N/A close();
4023N/A }
4023N/A }
4023N/A
0N/A /*
0N/A * Gets an inflater from the list of available inflaters or allocates
0N/A * a new one.
0N/A */
0N/A private Inflater getInflater() {
4023N/A Inflater inf;
4023N/A synchronized (inflaterCache) {
4023N/A while (null != (inf = inflaterCache.poll())) {
4023N/A if (false == inf.ended()) {
4023N/A return inf;
4023N/A }
0N/A }
0N/A }
4023N/A return new Inflater(true);
0N/A }
0N/A
0N/A /*
0N/A * Releases the specified inflater to the list of available inflaters.
0N/A */
0N/A private void releaseInflater(Inflater inf) {
4023N/A if (false == inf.ended()) {
530N/A inf.reset();
4023N/A synchronized (inflaterCache) {
4023N/A inflaterCache.add(inf);
4023N/A }
0N/A }
0N/A }
0N/A
0N/A // List of available Inflater objects for decompression
4023N/A private Deque<Inflater> inflaterCache = new ArrayDeque<>();
0N/A
0N/A /**
0N/A * Returns the path name of the ZIP file.
0N/A * @return the path name of the ZIP file
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of the ZIP file entries.
0N/A * @return an enumeration of the ZIP file entries
0N/A * @throws IllegalStateException if the zip file has been closed
0N/A */
0N/A public Enumeration<? extends ZipEntry> entries() {
0N/A ensureOpen();
0N/A return new Enumeration<ZipEntry>() {
0N/A private int i = 0;
0N/A public boolean hasMoreElements() {
0N/A synchronized (ZipFile.this) {
0N/A ensureOpen();
0N/A return i < total;
0N/A }
0N/A }
0N/A public ZipEntry nextElement() throws NoSuchElementException {
0N/A synchronized (ZipFile.this) {
0N/A ensureOpen();
0N/A if (i >= total) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A long jzentry = getNextEntry(jzfile, i++);
0N/A if (jzentry == 0) {
0N/A String message;
0N/A if (closeRequested) {
0N/A message = "ZipFile concurrently closed";
0N/A } else {
0N/A message = getZipMessage(ZipFile.this.jzfile);
0N/A }
0N/A throw new ZipError("jzentry == 0" +
0N/A ",\n jzfile = " + ZipFile.this.jzfile +
0N/A ",\n total = " + ZipFile.this.total +
0N/A ",\n name = " + ZipFile.this.name +
0N/A ",\n i = " + i +
0N/A ",\n message = " + message
0N/A );
0N/A }
1107N/A ZipEntry ze = getZipEntry(null, jzentry);
0N/A freeEntry(jzfile, jzentry);
0N/A return ze;
0N/A }
0N/A }
0N/A };
0N/A }
0N/A
1107N/A private ZipEntry getZipEntry(String name, long jzentry) {
1107N/A ZipEntry e = new ZipEntry();
1107N/A e.flag = getEntryFlag(jzentry); // get the flag first
1107N/A if (name != null) {
1107N/A e.name = name;
1107N/A } else {
1107N/A byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
1107N/A if (!zc.isUTF8() && (e.flag & EFS) != 0) {
1107N/A e.name = zc.toStringUTF8(bname, bname.length);
1107N/A } else {
1107N/A e.name = zc.toString(bname, bname.length);
1107N/A }
1107N/A }
1107N/A e.time = getEntryTime(jzentry);
1107N/A e.crc = getEntryCrc(jzentry);
1107N/A e.size = getEntrySize(jzentry);
1107N/A e. csize = getEntryCSize(jzentry);
1107N/A e.method = getEntryMethod(jzentry);
1107N/A e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
1107N/A byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
1107N/A if (bcomm == null) {
1107N/A e.comment = null;
1107N/A } else {
1107N/A if (!zc.isUTF8() && (e.flag & EFS) != 0) {
1107N/A e.comment = zc.toStringUTF8(bcomm, bcomm.length);
1107N/A } else {
1107N/A e.comment = zc.toString(bcomm, bcomm.length);
1107N/A }
1107N/A }
1107N/A return e;
1107N/A }
1107N/A
0N/A private static native long getNextEntry(long jzfile, int i);
0N/A
0N/A /**
0N/A * Returns the number of entries in the ZIP file.
0N/A * @return the number of entries in the ZIP file
0N/A * @throws IllegalStateException if the zip file has been closed
0N/A */
0N/A public int size() {
0N/A ensureOpen();
0N/A return total;
0N/A }
0N/A
0N/A /**
0N/A * Closes the ZIP file.
0N/A * <p> Closing this ZIP file will close all of the input streams
0N/A * previously returned by invocations of the {@link #getInputStream
0N/A * getInputStream} method.
0N/A *
0N/A * @throws IOException if an I/O error has occurred
0N/A */
0N/A public void close() throws IOException {
4023N/A if (closeRequested)
4023N/A return;
4023N/A closeRequested = true;
0N/A
4023N/A synchronized (this) {
4023N/A // Close streams, release their inflaters
4023N/A synchronized (streams) {
4023N/A if (false == streams.isEmpty()) {
4023N/A Map<InputStream, Inflater> copy = new HashMap<>(streams);
4023N/A streams.clear();
4023N/A for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
4023N/A e.getKey().close();
4023N/A Inflater inf = e.getValue();
4023N/A if (inf != null) {
4023N/A inf.end();
4023N/A }
4023N/A }
4023N/A }
4023N/A }
4023N/A
4023N/A // Release cached inflaters
4023N/A Inflater inf;
4023N/A synchronized (inflaterCache) {
4023N/A while (null != (inf = inflaterCache.poll())) {
4023N/A inf.end();
4023N/A }
1239N/A }
1239N/A
0N/A if (jzfile != 0) {
0N/A // Close the zip file
0N/A long zf = this.jzfile;
0N/A jzfile = 0;
0N/A
0N/A close(zf);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
4023N/A * Ensures that the system resources held by this ZipFile object are
4023N/A * released when there are no more references to it.
0N/A *
0N/A * <p>
0N/A * Since the time when GC would invoke this method is undetermined,
0N/A * it is strongly recommended that applications invoke the <code>close</code>
0N/A * method as soon they have finished accessing this <code>ZipFile</code>.
0N/A * This will prevent holding up system resources for an undetermined
0N/A * length of time.
0N/A *
0N/A * @throws IOException if an I/O error has occurred
0N/A * @see java.util.zip.ZipFile#close()
0N/A */
0N/A protected void finalize() throws IOException {
0N/A close();
0N/A }
0N/A
0N/A private static native void close(long jzfile);
0N/A
0N/A private void ensureOpen() {
0N/A if (closeRequested) {
0N/A throw new IllegalStateException("zip file closed");
0N/A }
0N/A
0N/A if (jzfile == 0) {
0N/A throw new IllegalStateException("The object is not initialized.");
0N/A }
0N/A }
0N/A
0N/A private void ensureOpenOrZipException() throws IOException {
0N/A if (closeRequested) {
0N/A throw new ZipException("ZipFile closed");
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Inner class implementing the input stream used to read a
0N/A * (possibly compressed) zip file entry.
0N/A */
0N/A private class ZipFileInputStream extends InputStream {
4023N/A private volatile boolean closeRequested = false;
0N/A protected long jzentry; // address of jzentry data
0N/A private long pos; // current position within entry data
0N/A protected long rem; // number of remaining bytes within entry
0N/A protected long size; // uncompressed size of this entry
0N/A
0N/A ZipFileInputStream(long jzentry) {
0N/A pos = 0;
1107N/A rem = getEntryCSize(jzentry);
1107N/A size = getEntrySize(jzentry);
0N/A this.jzentry = jzentry;
0N/A }
0N/A
0N/A public int read(byte b[], int off, int len) throws IOException {
0N/A if (rem == 0) {
0N/A return -1;
0N/A }
0N/A if (len <= 0) {
0N/A return 0;
0N/A }
0N/A if (len > rem) {
0N/A len = (int) rem;
0N/A }
0N/A synchronized (ZipFile.this) {
0N/A ensureOpenOrZipException();
0N/A
0N/A len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
0N/A off, len);
0N/A }
0N/A if (len > 0) {
0N/A pos += len;
0N/A rem -= len;
0N/A }
0N/A if (rem == 0) {
0N/A close();
0N/A }
0N/A return len;
0N/A }
0N/A
0N/A public int read() throws IOException {
0N/A byte[] b = new byte[1];
0N/A if (read(b, 0, 1) == 1) {
0N/A return b[0] & 0xff;
0N/A } else {
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A public long skip(long n) {
0N/A if (n > rem)
0N/A n = rem;
0N/A pos += n;
0N/A rem -= n;
0N/A if (rem == 0) {
0N/A close();
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A public int available() {
0N/A return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
0N/A }
0N/A
0N/A public long size() {
0N/A return size;
0N/A }
0N/A
0N/A public void close() {
4023N/A if (closeRequested)
4023N/A return;
4023N/A closeRequested = true;
4023N/A
0N/A rem = 0;
0N/A synchronized (ZipFile.this) {
0N/A if (jzentry != 0 && ZipFile.this.jzfile != 0) {
0N/A freeEntry(ZipFile.this.jzfile, jzentry);
0N/A jzentry = 0;
0N/A }
4023N/A }
4023N/A synchronized (streams) {
1239N/A streams.remove(this);
0N/A }
0N/A }
4023N/A
4023N/A protected void finalize() {
4023N/A close();
4023N/A }
0N/A }
0N/A
6329N/A static {
6329N/A sun.misc.SharedSecrets.setJavaUtilZipFileAccess(
6329N/A new sun.misc.JavaUtilZipFileAccess() {
6329N/A public boolean startsWithLocHeader(ZipFile zip) {
6329N/A return zip.startsWithLocHeader();
6329N/A }
6329N/A }
6329N/A );
6329N/A }
6329N/A
6329N/A /**
6329N/A * Returns {@code true} if, and only if, the zip file begins with {@code
6329N/A * LOCSIG}.
6329N/A */
6329N/A private boolean startsWithLocHeader() {
6329N/A return locsig;
6329N/A }
1107N/A
2227N/A private static native long open(String name, int mode, long lastModified,
2227N/A boolean usemmap) throws IOException;
1107N/A private static native int getTotal(long jzfile);
6329N/A private static native boolean startsWithLOC(long jzfile);
0N/A private static native int read(long jzfile, long jzentry,
0N/A long pos, byte[] b, int off, int len);
0N/A
1107N/A // access to the native zentry object
1107N/A private static native long getEntryTime(long jzentry);
1107N/A private static native long getEntryCrc(long jzentry);
1107N/A private static native long getEntryCSize(long jzentry);
1107N/A private static native long getEntrySize(long jzentry);
1107N/A private static native int getEntryMethod(long jzentry);
1107N/A private static native int getEntryFlag(long jzentry);
1332N/A private static native byte[] getCommentBytes(long jzfile);
0N/A
1107N/A private static final int JZENTRY_NAME = 0;
1107N/A private static final int JZENTRY_EXTRA = 1;
1107N/A private static final int JZENTRY_COMMENT = 2;
1107N/A private static native byte[] getEntryBytes(long jzentry, int type);
0N/A
0N/A private static native String getZipMessage(long jzfile);
0N/A}