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