Mercurial > hg > openjdk > lambda > jdk
changeset 7549:3f73ae4dc13f
add Stream of zip entries
Contributed-by: Akhil Arora <akhil.arora@oracle.com>
author | mduigou |
---|---|
date | Thu, 28 Feb 2013 13:45:03 -0800 |
parents | 74949845c660 |
children | 4bf1ce19c7cd |
files | src/share/classes/java/util/zip/ZipFile.java test/java/util/zip/ZipFile/StreamZipEntriesTest.java |
diffstat | 2 files changed, 99 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/zip/ZipFile.java Thu Feb 28 12:59:41 2013 -0800 +++ b/src/share/classes/java/util/zip/ZipFile.java Thu Feb 28 13:45:03 2013 -0800 @@ -36,10 +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 java.util.stream.Stream; +import java.util.stream.Streams; import sun.security.action.GetPropertyAction; import static java.util.zip.ZipConstants64.*; @@ -516,6 +521,30 @@ }; } + /** + * Create an ordered {@code Stream} over the ZIP file entries. + * Entries appear in the {@code Stream} in the order they appear in + * the ZIP file. + * + * @return a {@code Stream} of entries in this ZIP file + * @throws IllegalStateException if the zip file has been closed + * @since 1.8 + */ + public Stream<? extends ZipEntry> stream() { + final Enumeration<? extends ZipEntry> entries = entries(); + return Streams.stream(Spliterators.spliterator(new Iterator<ZipEntry>() { + @Override + public boolean hasNext() { + return entries.hasMoreElements(); + } + + @Override + public ZipEntry next() { + return entries.nextElement(); + } + }, size(), Spliterator.ORDERED)); + } + private ZipEntry getZipEntry(String name, long jzentry) { ZipEntry e = new ZipEntry(); e.flag = getEntryFlag(jzentry); // get the flag first
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/zip/ZipFile/StreamZipEntriesTest.java Thu Feb 28 13:45:03 2013 -0800 @@ -0,0 +1,70 @@ +/* + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @run testng StreamZipEntriesTest + * @summary Make sure we can stream entries of a zip file. + */ + +import java.io.File; +import java.io.IOException; +import java.lang.Object; +import java.lang.System; +import java.util.jar.JarFile; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +public class StreamZipEntriesTest { + + @Test + public void testStreamZip() throws IOException { + try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), "input.zip"))) { + zf.stream().forEach(e -> assertTrue(e instanceof ZipEntry)); + zf.stream().forEach(e -> assertEquals(e.toString(), "ReadZip.java")); + + Object elements[] = zf.stream().toArray(); + assertEquals(1, elements.length); + assertEquals(elements[0].toString(), "ReadZip.java"); + } + } + + @Test + public void testStreamJar() throws IOException { + try (JarFile jf = new JarFile(new File(System.getProperty("test.src", "."), "input.jar"))) { + jf.stream().forEach(e -> assertTrue(e instanceof ZipEntry)); + + Object elements[] = jf.stream().toArray(); + assertEquals(3, elements.length); + assertEquals(elements[0].toString(), "META-INF/"); + assertEquals(elements[1].toString(), "META-INF/MANIFEST.MF"); + assertEquals(elements[2].toString(), "ReleaseInflater.java"); + } + } +}