Skip to content

YARN-9591 Expand all entries in JAR archive #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
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;
Expand Down Expand Up @@ -115,12 +116,12 @@ public void unJar(File jarFile, File toDir) throws IOException {
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());
Expand All @@ -130,7 +131,7 @@ public static void unJar(InputStream inputStream, File toDir,
}
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++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;

Expand Down Expand Up @@ -84,7 +85,7 @@ public void tearDown() {
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);
Expand Down Expand Up @@ -297,4 +298,20 @@ public void testUnJar2() throws IOException {
"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());
}
}
}