diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/RunJar.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/RunJar.java index 50126002b7b..599bad31d7d 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/RunJar.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/RunJar.java @@ -34,9 +34,10 @@ import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import java.util.jar.JarInputStream; import java.util.jar.Manifest; import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.apache.commons.io.input.TeeInputStream; import org.apache.hadoop.classification.InterfaceAudience; @@ -115,12 +116,12 @@ public class RunJar { public static void unJar(InputStream inputStream, File toDir, Pattern unpackRegex) throws IOException { - try (JarInputStream jar = new JarInputStream(inputStream)) { + try (ZipInputStream zip = new ZipInputStream(inputStream)) { int numOfFailedLastModifiedSet = 0; String targetDirPath = toDir.getCanonicalPath() + File.separator; - for (JarEntry entry = jar.getNextJarEntry(); + for (ZipEntry entry = zip.getNextEntry(); entry != null; - entry = jar.getNextJarEntry()) { + entry = zip.getNextEntry()) { if (!entry.isDirectory() && unpackRegex.matcher(entry.getName()).matches()) { File file = new File(toDir, entry.getName()); @@ -130,7 +131,7 @@ public class RunJar { } ensureDirectory(file.getParentFile()); try (OutputStream out = Files.newOutputStream(file.toPath())) { - IOUtils.copyBytes(jar, out, BUFFER_SIZE); + IOUtils.copyBytes(zip, out, BUFFER_SIZE); } if (!file.setLastModified(entry.getTime())) { numOfFailedLastModifiedSet++; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestRunJar.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestRunJar.java index 1f7c71222fb..3ed2a36d5e1 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestRunJar.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestRunJar.java @@ -37,6 +37,7 @@ import java.nio.charset.StandardCharsets; import java.util.Random; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; import java.util.regex.Pattern; import java.util.zip.ZipEntry; @@ -84,7 +85,7 @@ public class TestRunJar { private void makeTestJar() throws IOException { File jarFile = new File(TEST_ROOT_DIR, TEST_JAR_NAME); JarOutputStream jstream = - new JarOutputStream(new FileOutputStream(jarFile)); + new JarOutputStream(new FileOutputStream(jarFile), new Manifest()); ZipEntry zipEntry1 = new ZipEntry(FOOBAR_TXT); zipEntry1.setTime(MOCKED_NOW); jstream.putNextEntry(zipEntry1); @@ -297,4 +298,20 @@ public class TestRunJar { "would create file outside of", e); } } -} \ No newline at end of file + + @Test + public void testUnJarStream() throws IOException { + File unjarDir = getUnjarDir("unjar-stream"); + + try (InputStream is = new FileInputStream(new File(TEST_ROOT_DIR, TEST_JAR_NAME))) { + RunJar.unJar(is, unjarDir, MATCH_ANY); + + assertTrue("foobar unpacked", + new File(unjarDir, TestRunJar.FOOBAR_TXT).exists()); + assertTrue("foobaz unpacked", + new File(unjarDir, FOOBAZ_TXT).exists()); + + assertTrue("MANIFEST.MF exists", new File(new File(unjarDir, "META-INF"), "MANIFEST.MF").exists()); + } + } +}