# HG changeset patch # User martin # Date 1410831496 25200 # Node ID 3caa40031b3f881d83ddfcbf26776452d69bfff5 # Parent 1b1ec4291abc0ba6da7bf79b754f08dd759a4a0c 8058520: jar xf does not work on zip files with leading garbage Summary: Fall back to ZipFile if ZipInputStream finds no entries Reviewed-by: sherman diff -r 1b1ec4291abc -r 3caa40031b3f jdk/src/jdk.dev/share/classes/sun/tools/jar/Main.java --- a/jdk/src/jdk.dev/share/classes/sun/tools/jar/Main.java Wed Jul 05 20:01:50 2017 +0200 +++ b/jdk/src/jdk.dev/share/classes/sun/tools/jar/Main.java Mon Sep 15 18:38:16 2014 -0700 @@ -214,7 +214,7 @@ in.close(); } out.close(); - if(nflag) { + if (nflag) { JarFile jarFile = null; File packFile = null; JarOutputStream jos = null; @@ -287,11 +287,14 @@ } } else if (tflag) { replaceFSC(files); + // For the "list table contents" action, access using the + // ZipFile class is always most efficient since only a + // "one-finger" scan through the central directory is required. if (fname != null) { list(fname, files); } else { InputStream in = new FileInputStream(FileDescriptor.in); - try{ + try { list(new BufferedInputStream(in), files); } finally { in.close(); @@ -299,6 +302,15 @@ } } else if (xflag) { replaceFSC(files); + // For the extract action, when extracting all the entries, + // access using the ZipInputStream class is most efficient, + // since only a single sequential scan through the zip file is + // required. When using the ZipFile class, a "two-finger" scan + // is required, but this is likely to be more efficient when a + // partial extract is requested. In case the zip file has + // "leading garbage", we fall back from the ZipInputStream + // implementation to the ZipFile implementation, since only the + // latter can handle it. if (fname != null && files != null) { extract(fname, files); } else { @@ -306,7 +318,9 @@ ? new FileInputStream(FileDescriptor.in) : new FileInputStream(fname); try { - extract(new BufferedInputStream(in), files); + if (!extract(new BufferedInputStream(in), files) && fname != null) { + extract(fname, files); + } } finally { in.close(); } @@ -921,14 +935,19 @@ /** * Extracts specified entries from JAR file. + * + * @return whether entries were found and successfully extracted + * (indicating this was a zip file without "leading garbage") */ - void extract(InputStream in, String files[]) throws IOException { + boolean extract(InputStream in, String files[]) throws IOException { ZipInputStream zis = new ZipInputStream(in); ZipEntry e; // Set of all directory entries specified in archive. Disallows // null entries. Disallows all entries if using pre-6.0 behavior. + boolean entriesFound = false; Set dirs = newDirSet(); while ((e = zis.getNextEntry()) != null) { + entriesFound = true; if (files == null) { dirs.add(extractFile(zis, e)); } else { @@ -947,6 +966,8 @@ // instead of during, because creating a file in a directory changes // that directory's timestamp. updateLastModifiedTime(dirs); + + return entriesFound; } /** @@ -958,7 +979,6 @@ Enumeration zes = zf.entries(); while (zes.hasMoreElements()) { ZipEntry e = zes.nextElement(); - InputStream is; if (files == null) { dirs.add(extractFile(zf.getInputStream(e), e)); } else { diff -r 1b1ec4291abc -r 3caa40031b3f jdk/test/tools/jar/LeadingGarbage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/tools/jar/LeadingGarbage.java Mon Sep 15 18:38:16 2014 -0700 @@ -0,0 +1,139 @@ +/* + * Copyright 2014 Google Inc. 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. + */ + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import sun.tools.jar.Main; + +import jdk.testlibrary.OutputAnalyzer; +import jdk.testlibrary.ProcessTools; + +import org.testng.annotations.Test; + +/** + * @test + * @bug 8058520 + * @summary jar tf and jar xf should work on zip files with leading garbage + * @library /lib/testlibrary + * @run testng LeadingGarbage + */ +@Test +public class LeadingGarbage { + final String jar = + Paths.get(new File(System.getProperty("java.home")).getParent(), + "bin", "jar").toString(); + final File[] files = { new File("a"), new File("b") }; + final File normalZip = new File("normal.zip"); + final File leadingGarbageZip = new File("leadingGarbage.zip"); + + void createFile(File f) throws IOException { + OutputStream fos = new FileOutputStream(f); + fos.write(f.getName().getBytes("UTF-8")); + fos.close(); + } + + void createFiles() throws IOException { + for (File file : files) + createFile(file); + } + + void deleteFiles() throws IOException { + for (File file : files) + assertTrue(file.delete()); + } + + void assertFilesExist() throws IOException { + for (File file : files) + assertTrue(file.exists()); + } + + void createNormalZip() throws Throwable { + createFiles(); + String[] cmd = { jar, "c0Mf", "normal.zip", "a", "b" }; + ProcessBuilder pb = new ProcessBuilder(cmd); + OutputAnalyzer a = ProcessTools.executeProcess(pb); + a.shouldHaveExitValue(0); + a.stdoutShouldMatch("\\A\\Z"); + a.stderrShouldMatch("\\A\\Z"); + deleteFiles(); + } + + void createZipWithLeadingGarbage() throws Throwable { + createNormalZip(); + createFile(leadingGarbageZip); + OutputStream fos = new FileOutputStream(leadingGarbageZip, true); + Files.copy(normalZip.toPath(), fos); + assertTrue(normalZip.length() < leadingGarbageZip.length()); + assertTrue(normalZip.delete()); + } + + public void test_canList() throws Throwable { + createNormalZip(); + assertCanList("normal.zip"); + } + + public void test_canListWithLeadingGarbage() throws Throwable { + createZipWithLeadingGarbage(); + assertCanList("leadingGarbage.zip"); + } + + void assertCanList(String zipFileName) throws Throwable { + String[] cmd = { jar, "tf", zipFileName }; + ProcessBuilder pb = new ProcessBuilder(cmd); + OutputAnalyzer a = ProcessTools.executeProcess(pb); + a.shouldHaveExitValue(0); + StringBuilder expected = new StringBuilder(); + for (File file : files) + expected.append(file.getName()).append('\n'); + a.stdoutShouldMatch(expected.toString()); + a.stderrShouldMatch("\\A\\Z"); + } + + public void test_canExtract() throws Throwable { + createNormalZip(); + assertCanExtract("normal.zip"); + } + + public void test_canExtractWithLeadingGarbage() throws Throwable { + createZipWithLeadingGarbage(); + assertCanExtract("leadingGarbage.zip"); + } + + void assertCanExtract(String zipFileName) throws Throwable { + String[] cmd = { jar, "xf", zipFileName }; + ProcessBuilder pb = new ProcessBuilder(cmd); + OutputAnalyzer a = ProcessTools.executeProcess(pb); + a.shouldHaveExitValue(0); + a.stdoutShouldMatch("\\A\\Z"); + a.stderrShouldMatch("\\A\\Z"); + assertFilesExist(); + } + +}