1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.io.Closeable;
  29 import java.io.InputStream;
  30 import java.io.IOException;
  31 import java.io.EOFException;
  32 import java.io.File;
  33 import java.nio.charset.Charset;
  34 import java.nio.charset.StandardCharsets;
  35 import java.util.ArrayDeque;
  36 import java.util.Deque;
  37 import java.util.Enumeration;
  38 import java.util.HashMap;
  39 import java.util.Iterator;
  40 import java.util.Map;
  41 import java.util.NoSuchElementException;
  42 import java.util.Spliterator;
  43 import java.util.Spliterators;
  44 import java.util.WeakHashMap;
  45 import java.util.stream.Stream;
  46 import java.util.stream.StreamSupport;
  47 import jdk.internal.misc.JavaUtilZipFileAccess;
  48 import jdk.internal.misc.SharedSecrets;
  49 
  50 import static java.util.zip.ZipConstants64.*;
  51 
  52 /**
  53  * This class is used to read entries from a zip file.
  54  *
  55  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
  56  * or method in this class will cause a {@link NullPointerException} to be
  57  * thrown.
  58  *
  59  * @author      David Connelly
  60  */
  61 public
  62 class ZipFile implements ZipConstants, Closeable {
  63     private long jzfile;           // address of jzfile data
  64     private final String name;     // zip file name
  65     private final int total;       // total number of entries
  66     private final boolean locsig;  // if zip file starts with LOCSIG (usually true)
  67     private volatile boolean closeRequested = false;
  68 
  69     private static final int STORED = ZipEntry.STORED;
  70     private static final int DEFLATED = ZipEntry.DEFLATED;
  71 
  72     /**
  73      * Mode flag to open a zip file for reading.
  74      */
  75     public static final int OPEN_READ = 0x1;
  76 
  77     /**
  78      * Mode flag to open a zip file and mark it for deletion.  The file will be
  79      * deleted some time between the moment that it is opened and the moment
  80      * that it is closed, but its contents will remain accessible via the
  81      * {@code ZipFile} object until either the close method is invoked or the
  82      * virtual machine exits.
  83      */
  84     public static final int OPEN_DELETE = 0x4;
  85 
  86     static {
  87         /* Zip library is loaded from System.initializeSystemClass */
  88         initIDs();
  89     }
  90 
  91     private static native void initIDs();
  92 
  93     private static final boolean usemmap;
  94 
  95     static {
  96         // A system prpperty to disable mmap use to avoid vm crash when
  97         // in-use zip file is accidently overwritten by others.
  98         String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping");
  99         usemmap = (prop == null ||
 100                    !(prop.length() == 0 || prop.equalsIgnoreCase("true")));
 101     }
 102 
 103     /**
 104      * Opens a zip file for reading.
 105      *
 106      * <p>First, if there is a security manager, its {@code checkRead}
 107      * method is called with the {@code name} argument as its argument
 108      * to ensure the read is allowed.
 109      *
 110      * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
 111      * decode the entry names and comments.
 112      *
 113      * @param name the name of the zip file
 114      * @throws ZipException if a ZIP format error has occurred
 115      * @throws IOException if an I/O error has occurred
 116      * @throws SecurityException if a security manager exists and its
 117      *         {@code checkRead} method doesn't allow read access to the file.
 118      *
 119      * @see SecurityManager#checkRead(java.lang.String)
 120      */
 121     public ZipFile(String name) throws IOException {
 122         this(new File(name), OPEN_READ);
 123     }
 124 
 125     /**
 126      * Opens a new {@code ZipFile} to read from the specified
 127      * {@code File} object in the specified mode.  The mode argument
 128      * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
 129      *
 130      * <p>First, if there is a security manager, its {@code checkRead}
 131      * method is called with the {@code name} argument as its argument to
 132      * ensure the read is allowed.
 133      *
 134      * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
 135      * decode the entry names and comments
 136      *
 137      * @param file the ZIP file to be opened for reading
 138      * @param mode the mode in which the file is to be opened
 139      * @throws ZipException if a ZIP format error has occurred
 140      * @throws IOException if an I/O error has occurred
 141      * @throws SecurityException if a security manager exists and
 142      *         its {@code checkRead} method
 143      *         doesn't allow read access to the file,
 144      *         or its {@code checkDelete} method doesn't allow deleting
 145      *         the file when the {@code OPEN_DELETE} flag is set.
 146      * @throws IllegalArgumentException if the {@code mode} argument is invalid
 147      * @see SecurityManager#checkRead(java.lang.String)
 148      * @since 1.3
 149      */
 150     public ZipFile(File file, int mode) throws IOException {
 151         this(file, mode, StandardCharsets.UTF_8);
 152     }
 153 
 154     /**
 155      * Opens a ZIP file for reading given the specified File object.
 156      *
 157      * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
 158      * decode the entry names and comments.
 159      *
 160      * @param file the ZIP file to be opened for reading
 161      * @throws ZipException if a ZIP format error has occurred
 162      * @throws IOException if an I/O error has occurred
 163      */
 164     public ZipFile(File file) throws ZipException, IOException {
 165         this(file, OPEN_READ);
 166     }
 167 
 168     private ZipCoder zc;
 169 
 170     /**
 171      * Opens a new {@code ZipFile} to read from the specified
 172      * {@code File} object in the specified mode.  The mode argument
 173      * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
 174      *
 175      * <p>First, if there is a security manager, its {@code checkRead}
 176      * method is called with the {@code name} argument as its argument to
 177      * ensure the read is allowed.
 178      *
 179      * @param file the ZIP file to be opened for reading
 180      * @param mode the mode in which the file is to be opened
 181      * @param charset
 182      *        the {@linkplain java.nio.charset.Charset charset} to
 183      *        be used to decode the ZIP entry name and comment that are not
 184      *        encoded by using UTF-8 encoding (indicated by entry's general
 185      *        purpose flag).
 186      *
 187      * @throws ZipException if a ZIP format error has occurred
 188      * @throws IOException if an I/O error has occurred
 189      *
 190      * @throws SecurityException
 191      *         if a security manager exists and its {@code checkRead}
 192      *         method doesn't allow read access to the file,or its
 193      *         {@code checkDelete} method doesn't allow deleting the
 194      *         file when the {@code OPEN_DELETE} flag is set
 195      *
 196      * @throws IllegalArgumentException if the {@code mode} argument is invalid
 197      *
 198      * @see SecurityManager#checkRead(java.lang.String)
 199      *
 200      * @since 1.7
 201      */
 202     public ZipFile(File file, int mode, Charset charset) throws IOException
 203     {
 204         if (((mode & OPEN_READ) == 0) ||
 205             ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
 206             throw new IllegalArgumentException("Illegal mode: 0x"+
 207                                                Integer.toHexString(mode));
 208         }
 209         String name = file.getPath();
 210         SecurityManager sm = System.getSecurityManager();
 211         if (sm != null) {
 212             sm.checkRead(name);
 213             if ((mode & OPEN_DELETE) != 0) {
 214                 sm.checkDelete(name);
 215             }
 216         }
 217         if (charset == null)
 218             throw new NullPointerException("charset is null");
 219         this.zc = ZipCoder.get(charset);
 220         long t0 = System.nanoTime();
 221         jzfile = open(name, mode, file.lastModified(), usemmap);
 222         sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
 223         sun.misc.PerfCounter.getZipFileCount().increment();
 224         this.name = name;
 225         this.total = getTotal(jzfile);
 226         this.locsig = startsWithLOC(jzfile);
 227     }
 228 
 229     /**
 230      * Opens a zip file for reading.
 231      *
 232      * <p>First, if there is a security manager, its {@code checkRead}
 233      * method is called with the {@code name} argument as its argument
 234      * to ensure the read is allowed.
 235      *
 236      * @param name the name of the zip file
 237      * @param charset
 238      *        the {@linkplain java.nio.charset.Charset charset} to
 239      *        be used to decode the ZIP entry name and comment that are not
 240      *        encoded by using UTF-8 encoding (indicated by entry's general
 241      *        purpose flag).
 242      *
 243      * @throws ZipException if a ZIP format error has occurred
 244      * @throws IOException if an I/O error has occurred
 245      * @throws SecurityException
 246      *         if a security manager exists and its {@code checkRead}
 247      *         method doesn't allow read access to the file
 248      *
 249      * @see SecurityManager#checkRead(java.lang.String)
 250      *
 251      * @since 1.7
 252      */
 253     public ZipFile(String name, Charset charset) throws IOException
 254     {
 255         this(new File(name), OPEN_READ, charset);
 256     }
 257 
 258     /**
 259      * Opens a ZIP file for reading given the specified File object.
 260      * @param file the ZIP file to be opened for reading
 261      * @param charset
 262      *        The {@linkplain java.nio.charset.Charset charset} to be
 263      *        used to decode the ZIP entry name and comment (ignored if
 264      *        the <a href="package-summary.html#lang_encoding"> language
 265      *        encoding bit</a> of the ZIP entry's general purpose bit
 266      *        flag is set).
 267      *
 268      * @throws ZipException if a ZIP format error has occurred
 269      * @throws IOException if an I/O error has occurred
 270      *
 271      * @since 1.7
 272      */
 273     public ZipFile(File file, Charset charset) throws IOException
 274     {
 275         this(file, OPEN_READ, charset);
 276     }
 277 
 278     /**
 279      * Returns the zip file comment, or null if none.
 280      *
 281      * @return the comment string for the zip file, or null if none
 282      *
 283      * @throws IllegalStateException if the zip file has been closed
 284      *
 285      * Since 1.7
 286      */
 287     public String getComment() {
 288         synchronized (this) {
 289             ensureOpen();
 290             byte[] bcomm = getCommentBytes(jzfile);
 291             if (bcomm == null)
 292                 return null;
 293             return zc.toString(bcomm, bcomm.length);
 294         }
 295     }
 296 
 297     /**
 298      * Returns the zip file entry for the specified name, or null
 299      * if not found.
 300      *
 301      * @param name the name of the entry
 302      * @return the zip file entry, or null if not found
 303      * @throws IllegalStateException if the zip file has been closed
 304      */
 305     public ZipEntry getEntry(String name) {
 306         if (name == null) {
 307             throw new NullPointerException("name");
 308         }
 309         long jzentry = 0;
 310         synchronized (this) {
 311             ensureOpen();
 312             jzentry = getEntry(jzfile, zc.getBytes(name), true);
 313             if (jzentry != 0) {
 314                 ZipEntry ze = getZipEntry(name, jzentry);
 315                 freeEntry(jzfile, jzentry);
 316                 return ze;
 317             }
 318         }
 319         return null;
 320     }
 321 
 322     private static native long getEntry(long jzfile, byte[] name,
 323                                         boolean addSlash);
 324 
 325     // freeEntry releases the C jzentry struct.
 326     private static native void freeEntry(long jzfile, long jzentry);
 327 
 328     // the outstanding inputstreams that need to be closed,
 329     // mapped to the inflater objects they use.
 330     private final Map<InputStream, Inflater> streams = new WeakHashMap<>();
 331 
 332     /**
 333      * Returns an input stream for reading the contents of the specified
 334      * zip file entry.
 335      *
 336      * <p> Closing this ZIP file will, in turn, close all input
 337      * streams that have been returned by invocations of this method.
 338      *
 339      * @param entry the zip file entry
 340      * @return the input stream for reading the contents of the specified
 341      * zip file entry.
 342      * @throws ZipException if a ZIP format error has occurred
 343      * @throws IOException if an I/O error has occurred
 344      * @throws IllegalStateException if the zip file has been closed
 345      */
 346     public InputStream getInputStream(ZipEntry entry) throws IOException {
 347         if (entry == null) {
 348             throw new NullPointerException("entry");
 349         }
 350         long jzentry = 0;
 351         ZipFileInputStream in = null;
 352         synchronized (this) {
 353             ensureOpen();
 354             if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
 355                 jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
 356             } else {
 357                 jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
 358             }
 359             if (jzentry == 0) {
 360                 return null;
 361             }
 362             in = new ZipFileInputStream(jzentry);
 363 
 364             switch (getEntryMethod(jzentry)) {
 365             case STORED:
 366                 synchronized (streams) {
 367                     streams.put(in, null);
 368                 }
 369                 return in;
 370             case DEFLATED:
 371                 // MORE: Compute good size for inflater stream:
 372                 long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
 373                 if (size > 65536) size = 8192;
 374                 if (size <= 0) size = 4096;
 375                 Inflater inf = getInflater();
 376                 InputStream is =
 377                     new ZipFileInflaterInputStream(in, inf, (int)size);
 378                 synchronized (streams) {
 379                     streams.put(is, inf);
 380                 }
 381                 return is;
 382             default:
 383                 throw new ZipException("invalid compression method");
 384             }
 385         }
 386     }
 387 
 388     private class ZipFileInflaterInputStream extends InflaterInputStream {
 389         private volatile boolean closeRequested = false;
 390         private boolean eof = false;
 391         private final ZipFileInputStream zfin;
 392 
 393         ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf,
 394                 int size) {
 395             super(zfin, inf, size);
 396             this.zfin = zfin;
 397         }
 398 
 399         public void close() throws IOException {
 400             if (closeRequested)
 401                 return;
 402             closeRequested = true;
 403 
 404             super.close();
 405             Inflater inf;
 406             synchronized (streams) {
 407                 inf = streams.remove(this);
 408             }
 409             if (inf != null) {
 410                 releaseInflater(inf);
 411             }
 412         }
 413 
 414         // Override fill() method to provide an extra "dummy" byte
 415         // at the end of the input stream. This is required when
 416         // using the "nowrap" Inflater option.
 417         protected void fill() throws IOException {
 418             if (eof) {
 419                 throw new EOFException("Unexpected end of ZLIB input stream");
 420             }
 421             len = in.read(buf, 0, buf.length);
 422             if (len == -1) {
 423                 buf[0] = 0;
 424                 len = 1;
 425                 eof = true;
 426             }
 427             inf.setInput(buf, 0, len);
 428         }
 429 
 430         public int available() throws IOException {
 431             if (closeRequested)
 432                 return 0;
 433             long avail = zfin.size() - inf.getBytesWritten();
 434             return (avail > (long) Integer.MAX_VALUE ?
 435                     Integer.MAX_VALUE : (int) avail);
 436         }
 437 
 438         protected void finalize() throws Throwable {
 439             close();
 440         }
 441     }
 442 
 443     /*
 444      * Gets an inflater from the list of available inflaters or allocates
 445      * a new one.
 446      */
 447     private Inflater getInflater() {
 448         Inflater inf;
 449         synchronized (inflaterCache) {
 450             while (null != (inf = inflaterCache.poll())) {
 451                 if (false == inf.ended()) {
 452                     return inf;
 453                 }
 454             }
 455         }
 456         return new Inflater(true);
 457     }
 458 
 459     /*
 460      * Releases the specified inflater to the list of available inflaters.
 461      */
 462     private void releaseInflater(Inflater inf) {
 463         if (false == inf.ended()) {
 464             inf.reset();
 465             synchronized (inflaterCache) {
 466                 inflaterCache.add(inf);
 467             }
 468         }
 469     }
 470 
 471     // List of available Inflater objects for decompression
 472     private Deque<Inflater> inflaterCache = new ArrayDeque<>();
 473 
 474     /**
 475      * Returns the path name of the ZIP file.
 476      * @return the path name of the ZIP file
 477      */
 478     public String getName() {
 479         return name;
 480     }
 481 
 482     private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
 483         private int i = 0;
 484 
 485         public ZipEntryIterator() {
 486             ensureOpen();
 487         }
 488 
 489         public boolean hasMoreElements() {
 490             return hasNext();
 491         }
 492 
 493         public boolean hasNext() {
 494             synchronized (ZipFile.this) {
 495                 ensureOpen();
 496                 return i < total;
 497             }
 498         }
 499 
 500         public ZipEntry nextElement() {
 501             return next();
 502         }
 503 
 504         public ZipEntry next() {
 505             synchronized (ZipFile.this) {
 506                 ensureOpen();
 507                 if (i >= total) {
 508                     throw new NoSuchElementException();
 509                 }
 510                 long jzentry = getNextEntry(jzfile, i++);
 511                 if (jzentry == 0) {
 512                     String message;
 513                     if (closeRequested) {
 514                         message = "ZipFile concurrently closed";
 515                     } else {
 516                         message = getZipMessage(ZipFile.this.jzfile);
 517                     }
 518                     throw new ZipError("jzentry == 0" +
 519                                        ",\n jzfile = " + ZipFile.this.jzfile +
 520                                        ",\n total = " + ZipFile.this.total +
 521                                        ",\n name = " + ZipFile.this.name +
 522                                        ",\n i = " + i +
 523                                        ",\n message = " + message
 524                         );
 525                 }
 526                 ZipEntry ze = getZipEntry(null, jzentry);
 527                 freeEntry(jzfile, jzentry);
 528                 return ze;
 529             }
 530         }
 531 
 532         public Iterator<ZipEntry> asIterator() {
 533             return this;
 534         }
 535     }
 536 
 537     /**
 538      * Returns an enumeration of the ZIP file entries.
 539      * @return an enumeration of the ZIP file entries
 540      * @throws IllegalStateException if the zip file has been closed
 541      */
 542     public Enumeration<? extends ZipEntry> entries() {
 543         return new ZipEntryIterator();
 544     }
 545 
 546     /**
 547      * Returns an ordered {@code Stream} over the ZIP file entries.
 548      * Entries appear in the {@code Stream} in the order they appear in
 549      * the central directory of the ZIP file.
 550      *
 551      * @return an ordered {@code Stream} of entries in this ZIP file
 552      * @throws IllegalStateException if the zip file has been closed
 553      * @since 1.8
 554      */
 555     public Stream<? extends ZipEntry> stream() {
 556         return StreamSupport.stream(Spliterators.spliterator(
 557                 new ZipEntryIterator(), size(),
 558                 Spliterator.ORDERED | Spliterator.DISTINCT |
 559                         Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
 560     }
 561 
 562     private ZipEntry getZipEntry(String name, long jzentry) {
 563         ZipEntry e = new ZipEntry();
 564         e.flag = getEntryFlag(jzentry);  // get the flag first
 565         if (name != null) {
 566             e.name = name;
 567         } else {
 568             byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
 569             if (!zc.isUTF8() && (e.flag & EFS) != 0) {
 570                 e.name = zc.toStringUTF8(bname, bname.length);
 571             } else {
 572                 e.name = zc.toString(bname, bname.length);
 573             }
 574         }
 575         e.xdostime = getEntryTime(jzentry);
 576         e.crc = getEntryCrc(jzentry);
 577         e.size = getEntrySize(jzentry);
 578         e.csize = getEntryCSize(jzentry);
 579         e.method = getEntryMethod(jzentry);
 580         e.setExtra0(getEntryBytes(jzentry, JZENTRY_EXTRA), false);
 581         byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
 582         if (bcomm == null) {
 583             e.comment = null;
 584         } else {
 585             if (!zc.isUTF8() && (e.flag & EFS) != 0) {
 586                 e.comment = zc.toStringUTF8(bcomm, bcomm.length);
 587             } else {
 588                 e.comment = zc.toString(bcomm, bcomm.length);
 589             }
 590         }
 591         return e;
 592     }
 593 
 594     private static native long getNextEntry(long jzfile, int i);
 595 
 596     /**
 597      * Returns the number of entries in the ZIP file.
 598      * @return the number of entries in the ZIP file
 599      * @throws IllegalStateException if the zip file has been closed
 600      */
 601     public int size() {
 602         ensureOpen();
 603         return total;
 604     }
 605 
 606     /**
 607      * Closes the ZIP file.
 608      * <p> Closing this ZIP file will close all of the input streams
 609      * previously returned by invocations of the {@link #getInputStream
 610      * getInputStream} method.
 611      *
 612      * @throws IOException if an I/O error has occurred
 613      */
 614     public void close() throws IOException {
 615         if (closeRequested)
 616             return;
 617         closeRequested = true;
 618 
 619         synchronized (this) {
 620             // Close streams, release their inflaters
 621             synchronized (streams) {
 622                 if (false == streams.isEmpty()) {
 623                     Map<InputStream, Inflater> copy = new HashMap<>(streams);
 624                     streams.clear();
 625                     for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
 626                         e.getKey().close();
 627                         Inflater inf = e.getValue();
 628                         if (inf != null) {
 629                             inf.end();
 630                         }
 631                     }
 632                 }
 633             }
 634 
 635             // Release cached inflaters
 636             Inflater inf;
 637             synchronized (inflaterCache) {
 638                 while (null != (inf = inflaterCache.poll())) {
 639                     inf.end();
 640                 }
 641             }
 642 
 643             if (jzfile != 0) {
 644                 // Close the zip file
 645                 long zf = this.jzfile;
 646                 jzfile = 0;
 647 
 648                 close(zf);
 649             }
 650         }
 651     }
 652 
 653     /**
 654      * Ensures that the system resources held by this ZipFile object are
 655      * released when there are no more references to it.
 656      *
 657      * <p>
 658      * Since the time when GC would invoke this method is undetermined,
 659      * it is strongly recommended that applications invoke the {@code close}
 660      * method as soon they have finished accessing this {@code ZipFile}.
 661      * This will prevent holding up system resources for an undetermined
 662      * length of time.
 663      *
 664      * @throws IOException if an I/O error has occurred
 665      * @see    java.util.zip.ZipFile#close()
 666      */
 667     protected void finalize() throws IOException {
 668         close();
 669     }
 670 
 671     private static native void close(long jzfile);
 672 
 673     private void ensureOpen() {
 674         if (closeRequested) {
 675             throw new IllegalStateException("zip file closed");
 676         }
 677 
 678         if (jzfile == 0) {
 679             throw new IllegalStateException("The object is not initialized.");
 680         }
 681     }
 682 
 683     private void ensureOpenOrZipException() throws IOException {
 684         if (closeRequested) {
 685             throw new ZipException("ZipFile closed");
 686         }
 687     }
 688 
 689     /*
 690      * Inner class implementing the input stream used to read a
 691      * (possibly compressed) zip file entry.
 692      */
 693    private class ZipFileInputStream extends InputStream {
 694         private volatile boolean closeRequested = false;
 695         protected long jzentry; // address of jzentry data
 696         private   long pos;     // current position within entry data
 697         protected long rem;     // number of remaining bytes within entry
 698         protected long size;    // uncompressed size of this entry
 699 
 700         ZipFileInputStream(long jzentry) {
 701             pos = 0;
 702             rem = getEntryCSize(jzentry);
 703             size = getEntrySize(jzentry);
 704             this.jzentry = jzentry;
 705         }
 706 
 707         public int read(byte b[], int off, int len) throws IOException {
 708             synchronized (ZipFile.this) {
 709                 long rem = this.rem;
 710                 long pos = this.pos;
 711                 if (rem == 0) {
 712                     return -1;
 713                 }
 714                 if (len <= 0) {
 715                     return 0;
 716                 }
 717                 if (len > rem) {
 718                     len = (int) rem;
 719                 }
 720 

 721                 ensureOpenOrZipException();
 722                 len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
 723                                    off, len);
 724                 if (len > 0) {
 725                     this.pos = (pos + len);
 726                     this.rem = (rem - len);
 727                 }
 728             }
 729             if (rem == 0) {
 730                 close();
 731             }
 732             return len;
 733         }
 734 
 735         public int read() throws IOException {
 736             byte[] b = new byte[1];
 737             if (read(b, 0, 1) == 1) {
 738                 return b[0] & 0xff;
 739             } else {
 740                 return -1;
 741             }
 742         }
 743 
 744         public long skip(long n) {
 745             if (n > rem)
 746                 n = rem;
 747             pos += n;
 748             rem -= n;
 749             if (rem == 0) {
 750                 close();
 751             }
 752             return n;
 753         }
 754 
 755         public int available() {
 756             return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
 757         }
 758 
 759         public long size() {
 760             return size;
 761         }
 762 
 763         public void close() {
 764             if (closeRequested)
 765                 return;
 766             closeRequested = true;
 767 
 768             rem = 0;
 769             synchronized (ZipFile.this) {
 770                 if (jzentry != 0 && ZipFile.this.jzfile != 0) {
 771                     freeEntry(ZipFile.this.jzfile, jzentry);
 772                     jzentry = 0;
 773                 }
 774             }
 775             synchronized (streams) {
 776                 streams.remove(this);
 777             }
 778         }
 779 
 780         protected void finalize() {
 781             close();
 782         }
 783     }
 784 
 785     static {
 786         SharedSecrets.setJavaUtilZipFileAccess(
 787             new JavaUtilZipFileAccess() {
 788                 public boolean startsWithLocHeader(ZipFile zip) {
 789                     return zip.startsWithLocHeader();
 790                 }
 791             }
 792         );
 793     }
 794 
 795     /**
 796      * Returns {@code true} if, and only if, the zip file begins with {@code
 797      * LOCSIG}.
 798      */
 799     private boolean startsWithLocHeader() {
 800         return locsig;
 801     }
 802 
 803     private static native long open(String name, int mode, long lastModified,
 804                                     boolean usemmap) throws IOException;
 805     private static native int getTotal(long jzfile);
 806     private static native boolean startsWithLOC(long jzfile);
 807     private static native int read(long jzfile, long jzentry,
 808                                    long pos, byte[] b, int off, int len);
 809 
 810     // access to the native zentry object
 811     private static native long getEntryTime(long jzentry);
 812     private static native long getEntryCrc(long jzentry);
 813     private static native long getEntryCSize(long jzentry);
 814     private static native long getEntrySize(long jzentry);
 815     private static native int getEntryMethod(long jzentry);
 816     private static native int getEntryFlag(long jzentry);
 817     private static native byte[] getCommentBytes(long jzfile);
 818 
 819     private static final int JZENTRY_NAME = 0;
 820     private static final int JZENTRY_EXTRA = 1;
 821     private static final int JZENTRY_COMMENT = 2;
 822     private static native byte[] getEntryBytes(long jzentry, int type);
 823 
 824     private static native String getZipMessage(long jzfile);
 825 }
--- EOF ---