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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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.util.Vector;
35 import java.util.Enumeration;
36 import java.util.Set;
37 import java.util.HashSet;
38 import java.util.NoSuchElementException;
39 import java.security.AccessController;
40 import sun.security.action.GetPropertyAction;
41 import static java.util.zip.ZipConstants64.*;
42
43 /**
44 * This class is used to read entries from a zip file.
45 *
46 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
47 * or method in this class will cause a {@link NullPointerException} to be
48 * thrown.
49 *
50 * @author David Connelly
51 */
52 public
53 class ZipFile implements ZipConstants, Closeable {
54 private long jzfile; // address of jzfile data
55 private String name; // zip file name
56 private int total; // total number of entries
57 private boolean closeRequested;
58
59 private static final int STORED = ZipEntry.STORED;
60 private static final int DEFLATED = ZipEntry.DEFLATED;
61
62 /**
63 * Mode flag to open a zip file for reading.
64 */
65 public static final int OPEN_READ = 0x1;
66
67 /**
68 * Mode flag to open a zip file and mark it for deletion. The file will be
69 * deleted some time between the moment that it is opened and the moment
70 * that it is closed, but its contents will remain accessible via the
71 * <tt>ZipFile</tt> object until either the close method is invoked or the
72 * virtual machine exits.
73 */
431 private Vector inflaters = new Vector();
432
433 /**
434 * Returns the path name of the ZIP file.
435 * @return the path name of the ZIP file
436 */
437 public String getName() {
438 return name;
439 }
440
441 /**
442 * Returns an enumeration of the ZIP file entries.
443 * @return an enumeration of the ZIP file entries
444 * @throws IllegalStateException if the zip file has been closed
445 */
446 public Enumeration<? extends ZipEntry> entries() {
447 ensureOpen();
448 return new Enumeration<ZipEntry>() {
449 private int i = 0;
450 public boolean hasMoreElements() {
451 synchronized (ZipFile.this) {
452 ensureOpen();
453 return i < total;
454 }
455 }
456 public ZipEntry nextElement() throws NoSuchElementException {
457 synchronized (ZipFile.this) {
458 ensureOpen();
459 if (i >= total) {
460 throw new NoSuchElementException();
461 }
462 long jzentry = getNextEntry(jzfile, i++);
463 if (jzentry == 0) {
464 String message;
465 if (closeRequested) {
466 message = "ZipFile concurrently closed";
467 } else {
468 message = getZipMessage(ZipFile.this.jzfile);
469 }
470 throw new ZipError("jzentry == 0" +
471 ",\n jzfile = " + ZipFile.this.jzfile +
472 ",\n total = " + ZipFile.this.total +
473 ",\n name = " + ZipFile.this.name +
474 ",\n i = " + i +
475 ",\n message = " + message
476 );
477 }
478 ZipEntry ze = getZipEntry(null, jzentry);
479 freeEntry(jzfile, jzentry);
480 return ze;
481 }
482 }
483 };
484 }
485
486 private ZipEntry getZipEntry(String name, long jzentry) {
487 ZipEntry e = new ZipEntry();
488 e.flag = getEntryFlag(jzentry); // get the flag first
489 if (name != null) {
490 e.name = name;
491 } else {
492 byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
493 if (!zc.isUTF8() && (e.flag & EFS) != 0) {
494 e.name = zc.toStringUTF8(bname, bname.length);
495 } else {
496 e.name = zc.toString(bname, bname.length);
497 }
498 }
499 e.time = getEntryTime(jzentry);
500 e.crc = getEntryCrc(jzentry);
501 e.size = getEntrySize(jzentry);
502 e. csize = getEntryCSize(jzentry);
503 e.method = getEntryMethod(jzentry);
504 e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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.util.Iterator;
35 import java.util.Vector;
36 import java.util.Enumeration;
37 import java.util.Set;
38 import java.util.HashSet;
39 import java.util.NoSuchElementException;
40 import java.security.AccessController;
41 import sun.security.action.GetPropertyAction;
42 import static java.util.zip.ZipConstants64.*;
43
44 /**
45 * This class is used to read entries from a zip file.
46 *
47 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
48 * or method in this class will cause a {@link NullPointerException} to be
49 * thrown.
50 *
51 * @author David Connelly
52 */
53 public
54 class ZipFile implements ZipConstants, Closeable, Iterable<ZipEntry> {
55 private long jzfile; // address of jzfile data
56 private String name; // zip file name
57 private int total; // total number of entries
58 private boolean closeRequested;
59
60 private static final int STORED = ZipEntry.STORED;
61 private static final int DEFLATED = ZipEntry.DEFLATED;
62
63 /**
64 * Mode flag to open a zip file for reading.
65 */
66 public static final int OPEN_READ = 0x1;
67
68 /**
69 * Mode flag to open a zip file and mark it for deletion. The file will be
70 * deleted some time between the moment that it is opened and the moment
71 * that it is closed, but its contents will remain accessible via the
72 * <tt>ZipFile</tt> object until either the close method is invoked or the
73 * virtual machine exits.
74 */
432 private Vector inflaters = new Vector();
433
434 /**
435 * Returns the path name of the ZIP file.
436 * @return the path name of the ZIP file
437 */
438 public String getName() {
439 return name;
440 }
441
442 /**
443 * Returns an enumeration of the ZIP file entries.
444 * @return an enumeration of the ZIP file entries
445 * @throws IllegalStateException if the zip file has been closed
446 */
447 public Enumeration<? extends ZipEntry> entries() {
448 ensureOpen();
449 return new Enumeration<ZipEntry>() {
450 private int i = 0;
451 public boolean hasMoreElements() {
452 return hasNextEntry(i);
453 }
454 public ZipEntry nextElement() throws NoSuchElementException {
455 return nextEntry(i++);
456 }
457 };
458 }
459
460 /**
461 * Returns an iterator over the entries in this ZIP file.
462 *
463 * @return an iterator over the entries in this ZIP file.
464 *
465 * @throws IllegalStateException if the zip file has been closed
466 */
467
468 public Iterator<ZipEntry> iterator() {
469 ensureOpen();
470 return new Iterator<ZipEntry>() {
471 private int i = 0;
472
473 public boolean hasNext() {
474 return hasNextEntry(i);
475 }
476 public ZipEntry next() {
477 return nextEntry(i++);
478 }
479 public void remove() {
480 throw new UnsupportedOperationException();
481 }
482 };
483 }
484
485 private boolean hasNextEntry(int i) {
486 synchronized (this) {
487 ensureOpen();
488 return i < total;
489 }
490 }
491
492 private ZipEntry nextEntry(int i) {
493 synchronized (this) {
494 ensureOpen();
495 if (i >= total) {
496 throw new NoSuchElementException();
497 }
498 long jzentry = getNextEntry(jzfile, i);
499 if (jzentry == 0) {
500 String message;
501 if (closeRequested) {
502 message = "ZipFile concurrently closed";
503 } else {
504 message = getZipMessage(jzfile);
505 }
506 throw new ZipError("jzentry == 0" +
507 ",\n jzfile = " + jzfile +
508 ",\n total = " + total +
509 ",\n name = " + name +
510 ",\n i = " + i +
511 ",\n message = " + message
512 );
513 }
514 ZipEntry ze = getZipEntry(null, jzentry);
515 freeEntry(jzfile, jzentry);
516 return ze;
517 }
518 }
519
520 private ZipEntry getZipEntry(String name, long jzentry) {
521 ZipEntry e = new ZipEntry();
522 e.flag = getEntryFlag(jzentry); // get the flag first
523 if (name != null) {
524 e.name = name;
525 } else {
526 byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
527 if (!zc.isUTF8() && (e.flag & EFS) != 0) {
528 e.name = zc.toStringUTF8(bname, bname.length);
529 } else {
530 e.name = zc.toString(bname, bname.length);
531 }
532 }
533 e.time = getEntryTime(jzentry);
534 e.crc = getEntryCrc(jzentry);
535 e.size = getEntrySize(jzentry);
536 e. csize = getEntryCSize(jzentry);
537 e.method = getEntryMethod(jzentry);
538 e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
|