--- old/src/share/classes/java/util/zip/ZipFile.java 2013-04-29 21:57:20.608301026 -0700 +++ new/src/share/classes/java/util/zip/ZipFile.java 2013-04-29 21:57:20.452301028 -0700 @@ -36,11 +36,15 @@ import java.util.Deque; import java.util.Enumeration; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.WeakHashMap; -import java.security.AccessController; -import sun.security.action.GetPropertyAction; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + import static java.util.zip.ZipConstants64.*; /** @@ -471,49 +475,77 @@ return name; } + protected class ZipEntryIterator implements Enumeration, Iterator { + private int i = 0; + + public ZipEntryIterator() { + ensureOpen(); + } + + public boolean hasMoreElements() { + return hasNext(); + } + + public boolean hasNext() { + synchronized (ZipFile.this) { + ensureOpen(); + return i < total; + } + } + + public ZipEntry nextElement() { + return next(); + } + + public ZipEntry next() { + 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; + } + } + } + /** * Returns an enumeration of the ZIP file entries. * @return an enumeration of the ZIP file entries * @throws IllegalStateException if the zip file has been closed */ public Enumeration entries() { - ensureOpen(); - return new Enumeration() { - private int i = 0; - public boolean hasMoreElements() { - synchronized (ZipFile.this) { - ensureOpen(); - return i < total; - } - } - 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; - } - } - }; + return new ZipEntryIterator(); + } + + /** + * Return a {@code Stream} of the ZIP file entries. + * + * @return a {@code Stream} of entries in this ZIP file + * @throws IllegalStateException if the zip file has been closed + * @since 1.8 + */ + public Stream stream() { + return StreamSupport.stream(Spliterators.spliterator( + new ZipEntryIterator(), size(), + Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL)); } private ZipEntry getZipEntry(String name, long jzentry) {