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 return getInputStream(entry, null); 348 } 349 350 /** 351 * Returns an input stream for reading the contents of the specified 352 * zip file entry. 353 * 354 * <p> Closing this ZIP file will, in turn, close all input 355 * streams that have been returned by invocations of this method. 356 * 357 * @param entry the zip file entry 358 * @param zipCryption instance of ZipCryption 359 * @return the input stream for reading the contents of the specified 360 * zip file entry. 361 * @throws ZipException if a ZIP format error has occurred 362 * @throws IOException if an I/O error has occurred 363 * @throws IllegalStateException if the zip file has been closed 364 */ 365 public InputStream getInputStream(ZipEntry entry, ZipCryption zipCryption) 366 throws IOException { 367 if (entry == null) { 368 throw new NullPointerException("entry"); 369 } 370 371 if ((entry.flag & 1) == 1) { 372 if (zipCryption == null) { 373 throw new ZipException("Passphrase is required."); 374 } else { 375 zipCryption.reset(); 376 } 377 } 378 379 long jzentry = 0; 380 ZipFileInputStream in = null; 381 synchronized (this) { 382 ensureOpen(); 383 if (!zc.isUTF8() && (entry.flag & EFS) != 0) { 384 jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false); 385 } else { 386 jzentry = getEntry(jzfile, zc.getBytes(entry.name), false); 387 } 388 if (jzentry == 0) { 389 return null; 390 } 391 in = new ZipFileInputStream(jzentry, zipCryption); 392 393 switch (getEntryMethod(jzentry)) { 394 case STORED: 395 396 if ((entry.flag & 1) == 1) { 397 byte[] encryptionHeader = 398 new byte[zipCryption.getEncryptionHeaderSize()]; 399 in.readRaw(encryptionHeader, 0, encryptionHeader.length); 400 zipCryption.decryptBytes(encryptionHeader); 401 402 if (!zipCryption.isValid(entry, encryptionHeader)) { 403 throw new ZipException("possibly incorrect passphrase"); 404 } 405 } 406 407 synchronized (streams) { 408 streams.put(in, null); 409 } 410 return in; 411 case DEFLATED: 412 // MORE: Compute good size for inflater stream: 413 long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack 414 if (size > 65536) size = 8192; 415 if (size <= 0) size = 4096; 416 Inflater inf = getInflater(); 417 418 if ((entry.flag & 1) == 1) { 419 byte[] encryptionHeader = 420 new byte[zipCryption.getEncryptionHeaderSize()]; 421 in.readRaw(encryptionHeader, 0, encryptionHeader.length); 422 zipCryption.decryptBytes(encryptionHeader); 423 424 if (!zipCryption.isValid(entry, encryptionHeader)) { 425 throw new ZipException("possibly incorrect passphrase"); 426 } 427 } 428 429 InputStream is = 430 new ZipFileInflaterInputStream(in, inf, (int)size); 431 synchronized (streams) { 432 streams.put(is, inf); 433 } 434 return is; 435 default: 436 throw new ZipException("invalid compression method"); 437 } 438 } 439 } 440 441 private class ZipFileInflaterInputStream extends InflaterInputStream { 442 private volatile boolean closeRequested = false; 443 private boolean eof = false; 444 private final ZipFileInputStream zfin; 445 446 ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf, 447 int size) { 448 super(zfin, inf, size); 449 this.zfin = zfin; 450 } 451 452 public void close() throws IOException { 453 if (closeRequested) 454 return; 455 closeRequested = true; 456 457 super.close(); 458 Inflater inf; 459 synchronized (streams) { 460 inf = streams.remove(this); 461 } 462 if (inf != null) { 463 releaseInflater(inf); 464 } 465 } 466 467 // Override fill() method to provide an extra "dummy" byte 468 // at the end of the input stream. This is required when 469 // using the "nowrap" Inflater option. 470 protected void fill() throws IOException { 471 if (eof) { 472 throw new EOFException("Unexpected end of ZLIB input stream"); 473 } 474 len = in.read(buf, 0, buf.length); 475 if (len == -1) { 476 buf[0] = 0; 477 len = 1; 478 eof = true; 479 } 480 inf.setInput(buf, 0, len); 481 } 482 483 public int available() throws IOException { 484 if (closeRequested) 485 return 0; 486 long avail = zfin.size() - inf.getBytesWritten(); 487 return (avail > (long) Integer.MAX_VALUE ? 488 Integer.MAX_VALUE : (int) avail); 489 } 490 491 protected void finalize() throws Throwable { 492 close(); 493 } 494 } 495 496 /* 497 * Gets an inflater from the list of available inflaters or allocates 498 * a new one. 499 */ 500 private Inflater getInflater() { 501 Inflater inf; 502 synchronized (inflaterCache) { 503 while (null != (inf = inflaterCache.poll())) { 504 if (false == inf.ended()) { 505 return inf; 506 } 507 } 508 } 509 return new Inflater(true); 510 } 511 512 /* 513 * Releases the specified inflater to the list of available inflaters. 514 */ 515 private void releaseInflater(Inflater inf) { 516 if (false == inf.ended()) { 517 inf.reset(); 518 synchronized (inflaterCache) { 519 inflaterCache.add(inf); 520 } 521 } 522 } 523 524 // List of available Inflater objects for decompression 525 private Deque<Inflater> inflaterCache = new ArrayDeque<>(); 526 527 /** 528 * Returns the path name of the ZIP file. 529 * @return the path name of the ZIP file 530 */ 531 public String getName() { 532 return name; 533 } 534 535 private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> { 536 private int i = 0; 537 538 public ZipEntryIterator() { 539 ensureOpen(); 540 } 541 542 public boolean hasMoreElements() { 543 return hasNext(); 544 } 545 546 public boolean hasNext() { 547 synchronized (ZipFile.this) { 548 ensureOpen(); 549 return i < total; 550 } 551 } 552 553 public ZipEntry nextElement() { 554 return next(); 555 } 556 557 public ZipEntry next() { 558 synchronized (ZipFile.this) { 559 ensureOpen(); 560 if (i >= total) { 561 throw new NoSuchElementException(); 562 } 563 long jzentry = getNextEntry(jzfile, i++); 564 if (jzentry == 0) { 565 String message; 566 if (closeRequested) { 567 message = "ZipFile concurrently closed"; 568 } else { 569 message = getZipMessage(ZipFile.this.jzfile); 570 } 571 throw new ZipError("jzentry == 0" + 572 ",\n jzfile = " + ZipFile.this.jzfile + 573 ",\n total = " + ZipFile.this.total + 574 ",\n name = " + ZipFile.this.name + 575 ",\n i = " + i + 576 ",\n message = " + message 577 ); 578 } 579 ZipEntry ze = getZipEntry(null, jzentry); 580 freeEntry(jzfile, jzentry); 581 return ze; 582 } 583 } 584 585 public Iterator<ZipEntry> asIterator() { 586 return this; 587 } 588 } 589 590 /** 591 * Returns an enumeration of the ZIP file entries. 592 * @return an enumeration of the ZIP file entries 593 * @throws IllegalStateException if the zip file has been closed 594 */ 595 public Enumeration<? extends ZipEntry> entries() { 596 return new ZipEntryIterator(); 597 } 598 599 /** 600 * Returns an ordered {@code Stream} over the ZIP file entries. 601 * Entries appear in the {@code Stream} in the order they appear in 602 * the central directory of the ZIP file. 603 * 604 * @return an ordered {@code Stream} of entries in this ZIP file 605 * @throws IllegalStateException if the zip file has been closed 606 * @since 1.8 607 */ 608 public Stream<? extends ZipEntry> stream() { 609 return StreamSupport.stream(Spliterators.spliterator( 610 new ZipEntryIterator(), size(), 611 Spliterator.ORDERED | Spliterator.DISTINCT | 612 Spliterator.IMMUTABLE | Spliterator.NONNULL), false); 613 } 614 615 private ZipEntry getZipEntry(String name, long jzentry) { 616 ZipEntry e = new ZipEntry(); 617 e.flag = getEntryFlag(jzentry); // get the flag first 618 if (name != null) { 619 e.name = name; 620 } else { 621 byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME); 622 if (!zc.isUTF8() && (e.flag & EFS) != 0) { 623 e.name = zc.toStringUTF8(bname, bname.length); 624 } else { 625 e.name = zc.toString(bname, bname.length); 626 } 627 } 628 e.xdostime = getEntryTime(jzentry); 629 e.crc = getEntryCrc(jzentry); 630 e.size = getEntrySize(jzentry); 631 e.csize = getEntryCSize(jzentry); 632 e.method = getEntryMethod(jzentry); 633 e.setExtra0(getEntryBytes(jzentry, JZENTRY_EXTRA), false); 634 byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT); 635 if (bcomm == null) { 636 e.comment = null; 637 } else { 638 if (!zc.isUTF8() && (e.flag & EFS) != 0) { 639 e.comment = zc.toStringUTF8(bcomm, bcomm.length); 640 } else { 641 e.comment = zc.toString(bcomm, bcomm.length); 642 } 643 } 644 return e; 645 } 646 647 private static native long getNextEntry(long jzfile, int i); 648 649 /** 650 * Returns the number of entries in the ZIP file. 651 * @return the number of entries in the ZIP file 652 * @throws IllegalStateException if the zip file has been closed 653 */ 654 public int size() { 655 ensureOpen(); 656 return total; 657 } 658 659 /** 660 * Closes the ZIP file. 661 * <p> Closing this ZIP file will close all of the input streams 662 * previously returned by invocations of the {@link #getInputStream 663 * getInputStream} method. 664 * 665 * @throws IOException if an I/O error has occurred 666 */ 667 public void close() throws IOException { 668 if (closeRequested) 669 return; 670 closeRequested = true; 671 672 synchronized (this) { 673 // Close streams, release their inflaters 674 synchronized (streams) { 675 if (false == streams.isEmpty()) { 676 Map<InputStream, Inflater> copy = new HashMap<>(streams); 677 streams.clear(); 678 for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) { 679 e.getKey().close(); 680 Inflater inf = e.getValue(); 681 if (inf != null) { 682 inf.end(); 683 } 684 } 685 } 686 } 687 688 // Release cached inflaters 689 Inflater inf; 690 synchronized (inflaterCache) { 691 while (null != (inf = inflaterCache.poll())) { 692 inf.end(); 693 } 694 } 695 696 if (jzfile != 0) { 697 // Close the zip file 698 long zf = this.jzfile; 699 jzfile = 0; 700 701 close(zf); 702 } 703 } 704 } 705 706 /** 707 * Ensures that the system resources held by this ZipFile object are 708 * released when there are no more references to it. 709 * 710 * <p> 711 * Since the time when GC would invoke this method is undetermined, 712 * it is strongly recommended that applications invoke the {@code close} 713 * method as soon they have finished accessing this {@code ZipFile}. 714 * This will prevent holding up system resources for an undetermined 715 * length of time. 716 * 717 * @throws IOException if an I/O error has occurred 718 * @see java.util.zip.ZipFile#close() 719 */ 720 protected void finalize() throws IOException { 721 close(); 722 } 723 724 private static native void close(long jzfile); 725 726 private void ensureOpen() { 727 if (closeRequested) { 728 throw new IllegalStateException("zip file closed"); 729 } 730 731 if (jzfile == 0) { 732 throw new IllegalStateException("The object is not initialized."); 733 } 734 } 735 736 private void ensureOpenOrZipException() throws IOException { 737 if (closeRequested) { 738 throw new ZipException("ZipFile closed"); 739 } 740 } 741 742 /* 743 * Inner class implementing the input stream used to read a 744 * (possibly compressed) zip file entry. 745 */ 746 private class ZipFileInputStream extends InputStream { 747 private volatile boolean zfisCloseRequested = false; 748 protected long jzentry; // address of jzentry data 749 private long pos; // current position within entry data 750 protected long rem; // number of remaining bytes within entry 751 protected long size; // uncompressed size of this entry 752 private ZipCryption zipCryption; // ZIP encrypt/decrypt engine 753 754 ZipFileInputStream(long jzentry, ZipCryption zipCryption) { 755 pos = 0; 756 rem = getEntryCSize(jzentry); 757 size = getEntrySize(jzentry); 758 this.jzentry = jzentry; 759 this.zipCryption = zipCryption; 760 } 761 762 public int read(byte b[], int off, int len) throws IOException { 763 len = readRaw(b, off, len); 764 765 if (zipCryption != null) { 766 zipCryption.decryptBytes(b, off, len); 767 } 768 769 return len; 770 } 771 772 public int readRaw(byte b[], int off, int len) throws IOException { 773 synchronized (ZipFile.this) { 774 long rem = this.rem; 775 long pos = this.pos; 776 if (rem == 0) { 777 return -1; 778 } 779 if (len <= 0) { 780 return 0; 781 } 782 if (len > rem) { 783 len = (int) rem; 784 } 785 786 // Check if ZipFile open 787 ensureOpenOrZipException(); 788 len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b, 789 off, len); 790 if (len > 0) { 791 this.pos = (pos + len); 792 this.rem = (rem - len); 793 } 794 } 795 if (rem == 0) { 796 close(); 797 } 798 return len; 799 } 800 801 public int read() throws IOException { 802 byte[] b = new byte[1]; 803 if (read(b, 0, 1) == 1) { 804 return b[0] & 0xff; 805 } else { 806 return -1; 807 } 808 } 809 810 public long skip(long n) { 811 if (n > rem) 812 n = rem; 813 pos += n; 814 rem -= n; 815 if (rem == 0) { 816 close(); 817 } 818 return n; 819 } 820 821 public int available() { 822 return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem; 823 } 824 825 public long size() { 826 return size; 827 } 828 829 public void close() { 830 if (zfisCloseRequested) 831 return; 832 zfisCloseRequested = true; 833 834 rem = 0; 835 synchronized (ZipFile.this) { 836 if (jzentry != 0 && ZipFile.this.jzfile != 0) { 837 freeEntry(ZipFile.this.jzfile, jzentry); 838 jzentry = 0; 839 } 840 } 841 synchronized (streams) { 842 streams.remove(this); 843 } 844 } 845 846 protected void finalize() { 847 close(); 848 } 849 } 850 851 static { 852 SharedSecrets.setJavaUtilZipFileAccess( 853 new JavaUtilZipFileAccess() { 854 public boolean startsWithLocHeader(ZipFile zip) { 855 return zip.startsWithLocHeader(); 856 } 857 } 858 ); 859 } 860 861 /** 862 * Returns {@code true} if, and only if, the zip file begins with {@code 863 * LOCSIG}. 864 */ 865 private boolean startsWithLocHeader() { 866 return locsig; 867 } 868 869 private static native long open(String name, int mode, long lastModified, 870 boolean usemmap) throws IOException; 871 private static native int getTotal(long jzfile); 872 private static native boolean startsWithLOC(long jzfile); 873 private static native int read(long jzfile, long jzentry, 874 long pos, byte[] b, int off, int len); 875 876 // access to the native zentry object 877 private static native long getEntryTime(long jzentry); 878 private static native long getEntryCrc(long jzentry); 879 private static native long getEntryCSize(long jzentry); 880 private static native long getEntrySize(long jzentry); 881 private static native int getEntryMethod(long jzentry); 882 private static native int getEntryFlag(long jzentry); 883 private static native byte[] getCommentBytes(long jzfile); 884 885 private static final int JZENTRY_NAME = 0; 886 private static final int JZENTRY_EXTRA = 1; 887 private static final int JZENTRY_COMMENT = 2; 888 private static native byte[] getEntryBytes(long jzentry, int type); 889 890 private static native String getZipMessage(long jzfile); 891 }