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 <tt>null</tt> 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      * <tt>ZipFile</tt> 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</code>
 105      * method is called with the <code>name</code> 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</code> 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</code> to read from the specified
 125      * <code>File</code> object in the specified mode.  The mode argument
 126      * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
 127      *
 128      * <p>First, if there is a security manager, its <code>checkRead</code>
 129      * method is called with the <code>name</code> 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</code> method
 141      *         doesn't allow read access to the file,
 142      *         or its <code>checkDelete</code> method doesn't allow deleting
 143      *         the file when the <tt>OPEN_DELETE</tt> flag is set.
 144      * @throws IllegalArgumentException if the <tt>mode</tt> 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</code> to read from the specified
 170      * <code>File</code> object in the specified mode.  The mode argument
 171      * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
 172      *
 173      * <p>First, if there is a security manager, its <code>checkRead</code>
 174      * method is called with the <code>name</code> 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</code>
 190      *         method doesn't allow read access to the file,or its
 191      *         <code>checkDelete</code> method doesn't allow deleting the
 192      *         file when the <tt>OPEN_DELETE</tt> flag is set
 193      *
 194      * @throws IllegalArgumentException if the <tt>mode</tt> 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</code>
 231      * method is called with the <code>name</code> 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</code>
 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 
 531     /**
 532      * Returns an enumeration of the ZIP file entries.
 533      * @return an enumeration of the ZIP file entries
 534      * @throws IllegalStateException if the zip file has been closed
 535      */
 536     public Enumeration<? extends ZipEntry> entries() {
 537         return new ZipEntryIterator();
 538     }
 539 
 540     /**
 541      * Return an ordered {@code Stream} over the ZIP file entries.
 542      * Entries appear in the {@code Stream} in the order they appear in
 543      * the central directory of the ZIP file.
 544      *
 545      * @return an ordered {@code Stream} of entries in this ZIP file
 546      * @throws IllegalStateException if the zip file has been closed
 547      * @since 1.8
 548      */
 549     public Stream<? extends ZipEntry> stream() {
 550         return StreamSupport.stream(Spliterators.spliterator(
 551                 new ZipEntryIterator(), size(),
 552                 Spliterator.ORDERED | Spliterator.DISTINCT |
 553                         Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
 554     }
 555 
 556     private ZipEntry getZipEntry(String name, long jzentry) {
 557         ZipEntry e = new ZipEntry();
 558         e.flag = getEntryFlag(jzentry);  // get the flag first
 559         if (name != null) {
 560             e.name = name;
 561         } else {
 562             byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
 563             if (!zc.isUTF8() && (e.flag & EFS) != 0) {
 564                 e.name = zc.toStringUTF8(bname, bname.length);
 565             } else {
 566                 e.name = zc.toString(bname, bname.length);
 567             }
 568         }
 569         e.xdostime = getEntryTime(jzentry);
 570         e.crc = getEntryCrc(jzentry);
 571         e.size = getEntrySize(jzentry);
 572         e.csize = getEntryCSize(jzentry);
 573         e.method = getEntryMethod(jzentry);
 574         e.setExtra0(getEntryBytes(jzentry, JZENTRY_EXTRA), false);
 575         byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
 576         if (bcomm == null) {
 577             e.comment = null;
 578         } else {
 579             if (!zc.isUTF8() && (e.flag & EFS) != 0) {
 580                 e.comment = zc.toStringUTF8(bcomm, bcomm.length);
 581             } else {
 582                 e.comment = zc.toString(bcomm, bcomm.length);
 583             }
 584         }
 585         return e;
 586     }
 587 
 588     private static native long getNextEntry(long jzfile, int i);
 589 
 590     /**
 591      * Returns the number of entries in the ZIP file.
 592      * @return the number of entries in the ZIP file
 593      * @throws IllegalStateException if the zip file has been closed
 594      */
 595     public int size() {
 596         ensureOpen();
 597         return total;
 598     }
 599 
 600     /**
 601      * Closes the ZIP file.
 602      * <p> Closing this ZIP file will close all of the input streams
 603      * previously returned by invocations of the {@link #getInputStream
 604      * getInputStream} method.
 605      *
 606      * @throws IOException if an I/O error has occurred
 607      */
 608     public void close() throws IOException {
 609         if (closeRequested)
 610             return;
 611         closeRequested = true;
 612 
 613         synchronized (this) {
 614             // Close streams, release their inflaters
 615             synchronized (streams) {
 616                 if (false == streams.isEmpty()) {
 617                     Map<InputStream, Inflater> copy = new HashMap<>(streams);
 618                     streams.clear();
 619                     for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
 620                         e.getKey().close();
 621                         Inflater inf = e.getValue();
 622                         if (inf != null) {
 623                             inf.end();
 624                         }
 625                     }
 626                 }
 627             }
 628 
 629             // Release cached inflaters
 630             Inflater inf;
 631             synchronized (inflaterCache) {
 632                 while (null != (inf = inflaterCache.poll())) {
 633                     inf.end();
 634                 }
 635             }
 636 
 637             if (jzfile != 0) {
 638                 // Close the zip file
 639                 long zf = this.jzfile;
 640                 jzfile = 0;
 641 
 642                 close(zf);
 643             }
 644         }
 645     }
 646 
 647     /**
 648      * Ensures that the system resources held by this ZipFile object are
 649      * released when there are no more references to it.
 650      *
 651      * <p>
 652      * Since the time when GC would invoke this method is undetermined,
 653      * it is strongly recommended that applications invoke the <code>close</code>
 654      * method as soon they have finished accessing this <code>ZipFile</code>.
 655      * This will prevent holding up system resources for an undetermined
 656      * length of time.
 657      *
 658      * @throws IOException if an I/O error has occurred
 659      * @see    java.util.zip.ZipFile#close()
 660      */
 661     protected void finalize() throws IOException {
 662         close();
 663     }
 664 
 665     private static native void close(long jzfile);
 666 
 667     private void ensureOpen() {
 668         if (closeRequested) {
 669             throw new IllegalStateException("zip file closed");
 670         }
 671 
 672         if (jzfile == 0) {
 673             throw new IllegalStateException("The object is not initialized.");
 674         }
 675     }
 676 
 677     private void ensureOpenOrZipException() throws IOException {
 678         if (closeRequested) {
 679             throw new ZipException("ZipFile closed");
 680         }
 681     }
 682 
 683     /*
 684      * Inner class implementing the input stream used to read a
 685      * (possibly compressed) zip file entry.
 686      */
 687    private class ZipFileInputStream extends InputStream {
 688         private volatile boolean closeRequested = false;
 689         protected long jzentry; // address of jzentry data
 690         private   long pos;     // current position within entry data
 691         protected long rem;     // number of remaining bytes within entry
 692         protected long size;    // uncompressed size of this entry
 693 
 694         ZipFileInputStream(long jzentry) {
 695             pos = 0;
 696             rem = getEntryCSize(jzentry);
 697             size = getEntrySize(jzentry);
 698             this.jzentry = jzentry;
 699         }
 700 
 701         public int read(byte b[], int off, int len) throws IOException {
 702             synchronized (ZipFile.this) {
 703                 long rem = this.rem;
 704                 long pos = this.pos;
 705                 if (rem == 0) {
 706                     return -1;
 707                 }
 708                 if (len <= 0) {
 709                     return 0;
 710                 }
 711                 if (len > rem) {
 712                     len = (int) rem;
 713                 }
 714 
 715                 ensureOpenOrZipException();
 716                 len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
 717                                    off, len);
 718                 if (len > 0) {
 719                     this.pos = (pos + len);
 720                     this.rem = (rem - len);
 721                 }
 722             }
 723             if (rem == 0) {
 724                 close();
 725             }
 726             return len;
 727         }
 728 
 729         public int read() throws IOException {
 730             byte[] b = new byte[1];
 731             if (read(b, 0, 1) == 1) {
 732                 return b[0] & 0xff;
 733             } else {
 734                 return -1;
 735             }
 736         }
 737 
 738         public long skip(long n) {
 739             if (n > rem)
 740                 n = rem;
 741             pos += n;
 742             rem -= n;
 743             if (rem == 0) {
 744                 close();
 745             }
 746             return n;
 747         }
 748 
 749         public int available() {
 750             return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
 751         }
 752 
 753         public long size() {
 754             return size;
 755         }
 756 
 757         public void close() {
 758             if (closeRequested)
 759                 return;
 760             closeRequested = true;
 761 
 762             rem = 0;
 763             synchronized (ZipFile.this) {
 764                 if (jzentry != 0 && ZipFile.this.jzfile != 0) {
 765                     freeEntry(ZipFile.this.jzfile, jzentry);
 766                     jzentry = 0;
 767                 }
 768             }
 769             synchronized (streams) {
 770                 streams.remove(this);
 771             }
 772         }
 773 
 774         protected void finalize() {
 775             close();
 776         }
 777     }
 778 
 779     static {
 780         sun.misc.SharedSecrets.setJavaUtilZipFileAccess(
 781             new sun.misc.JavaUtilZipFileAccess() {
 782                 public boolean startsWithLocHeader(ZipFile zip) {
 783                     return zip.startsWithLocHeader();
 784                 }
 785              }
 786         );
 787     }
 788 
 789     /**
 790      * Returns {@code true} if, and only if, the zip file begins with {@code
 791      * LOCSIG}.
 792      */
 793     private boolean startsWithLocHeader() {
 794         return locsig;
 795     }
 796 
 797     private static native long open(String name, int mode, long lastModified,
 798                                     boolean usemmap) throws IOException;
 799     private static native int getTotal(long jzfile);
 800     private static native boolean startsWithLOC(long jzfile);
 801     private static native int read(long jzfile, long jzentry,
 802                                    long pos, byte[] b, int off, int len);
 803 
 804     // access to the native zentry object
 805     private static native long getEntryTime(long jzentry);
 806     private static native long getEntryCrc(long jzentry);
 807     private static native long getEntryCSize(long jzentry);
 808     private static native long getEntrySize(long jzentry);
 809     private static native int getEntryMethod(long jzentry);
 810     private static native int getEntryFlag(long jzentry);
 811     private static native byte[] getCommentBytes(long jzfile);
 812 
 813     private static final int JZENTRY_NAME = 0;
 814     private static final int JZENTRY_EXTRA = 1;
 815     private static final int JZENTRY_COMMENT = 2;
 816     private static native byte[] getEntryBytes(long jzentry, int type);
 817 
 818     private static native String getZipMessage(long jzfile);
 819 }