--- old/src/share/classes/java/util/zip/ZipFile.java Sun May 16 00:27:01 2010 +++ new/src/share/classes/java/util/zip/ZipFile.java Sun May 16 00:26:59 2010 @@ -31,6 +31,7 @@ import java.io.EOFException; import java.io.File; import java.nio.charset.Charset; +import java.util.Iterator; import java.util.Vector; import java.util.Enumeration; import java.util.Set; @@ -50,7 +51,7 @@ * @author David Connelly */ public -class ZipFile implements ZipConstants, Closeable { +class ZipFile implements ZipConstants, Closeable, Iterable { private long jzfile; // address of jzfile data private String name; // zip file name private int total; // total number of entries @@ -446,41 +447,74 @@ public Enumeration entries() { ensureOpen(); return new Enumeration() { - private int i = 0; - public boolean hasMoreElements() { - synchronized (ZipFile.this) { - ensureOpen(); - return i < total; - } + private int i = 0; + public boolean hasMoreElements() { + return hasNextEntry(i); + } + public ZipEntry nextElement() throws NoSuchElementException { + return nextEntry(i++); + } + }; + } + + /** + * Returns an iterator over the entries in this ZIP file. + * + * @return an iterator over the entries in this ZIP file. + * + * @throws IllegalStateException if the zip file has been closed + */ + + public Iterator iterator() { + ensureOpen(); + return new Iterator() { + private int i = 0; + + public boolean hasNext() { + return hasNextEntry(i); + } + public ZipEntry next() { + return nextEntry(i++); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + private boolean hasNextEntry(int i) { + synchronized (this) { + ensureOpen(); + return i < total; + } + } + + private ZipEntry nextEntry(int i) { + synchronized (this) { + ensureOpen(); + if (i >= total) { + throw new NoSuchElementException(); + } + long jzentry = getNextEntry(jzfile, i); + if (jzentry == 0) { + String message; + if (closeRequested) { + message = "ZipFile concurrently closed"; + } else { + message = getZipMessage(jzfile); } - public ZipEntry nextElement() throws NoSuchElementException { - synchronized (ZipFile.this) { - ensureOpen(); - if (i >= total) { - throw new NoSuchElementException(); - } - long jzentry = getNextEntry(jzfile, i++); - if (jzentry == 0) { - String message; - if (closeRequested) { - message = "ZipFile concurrently closed"; - } else { - message = getZipMessage(ZipFile.this.jzfile); - } - throw new ZipError("jzentry == 0" + - ",\n jzfile = " + ZipFile.this.jzfile + - ",\n total = " + ZipFile.this.total + - ",\n name = " + ZipFile.this.name + - ",\n i = " + i + - ",\n message = " + message - ); - } - ZipEntry ze = getZipEntry(null, jzentry); - freeEntry(jzfile, jzentry); - return ze; - } - } - }; + throw new ZipError("jzentry == 0" + + ",\n jzfile = " + jzfile + + ",\n total = " + total + + ",\n name = " + name + + ",\n i = " + i + + ",\n message = " + message + ); + } + ZipEntry ze = getZipEntry(null, jzentry); + freeEntry(jzfile, jzentry); + return ze; + } } private ZipEntry getZipEntry(String name, long jzentry) {