YARN-9591 Expand all entries in JAR archive

This commit is contained in:
Terence Yim 2019-06-03 11:52:23 -07:00
parent bd2590d71b
commit da30c85964
2 changed files with 25 additions and 7 deletions

View File

@ -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++;

View File

@ -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);
}
}
@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());
}
}
}