RunJar { public void unJar(File jarFile, File toDir) throws IOException { unJar(jarFile, toDir, MATCH_ANY); }  void unJar(File jarFile, File toDir); static void unJar(InputStream inputStream, File toDir,
                           Pattern unpackRegex); @Deprecated static void unJarAndSave(InputStream inputStream, File toDir,
                           String name, Pattern unpackRegex); static void unJar(File jarFile, File toDir, Pattern unpackRegex); static void main(String[] args); void run(String[] args); static final Pattern MATCH_ANY; static final int SHUTDOWN_HOOK_PRIORITY; static final String HADOOP_USE_CLIENT_CLASSLOADER; static final String HADOOP_CLASSPATH; static final String HADOOP_CLIENT_CLASSLOADER_SYSTEM_CLASSES; static final String HADOOP_CLIENT_SKIP_UNJAR; }
@Test public void testUnJar() throws Exception { File unjarDir = getUnjarDir("unjar-all"); RunJar.unJar(new File(TEST_ROOT_DIR, TEST_JAR_NAME), unjarDir, MATCH_ANY); assertTrue("foobar unpacked", new File(unjarDir, TestRunJar.FOOBAR_TXT).exists()); assertTrue("foobaz unpacked", new File(unjarDir, FOOBAZ_TXT).exists()); }
@Test public void testUnJarWithPattern() throws Exception { File unjarDir = getUnjarDir("unjar-pattern"); RunJar.unJar(new File(TEST_ROOT_DIR, TEST_JAR_NAME), unjarDir, Pattern.compile(".*baz.*")); assertFalse("foobar not unpacked", new File(unjarDir, TestRunJar.FOOBAR_TXT).exists()); assertTrue("foobaz unpacked", new File(unjarDir, FOOBAZ_TXT).exists()); }
@Test public void testUnJarDoesNotLooseLastModify() throws Exception { File unjarDir = getUnjarDir("unjar-lastmod"); RunJar.unJar(new File(TEST_ROOT_DIR, TEST_JAR_NAME), unjarDir, MATCH_ANY); String failureMessage = "Last modify time was lost during unJar"; assertEquals(failureMessage, MOCKED_NOW, new File(unjarDir, TestRunJar.FOOBAR_TXT).lastModified()); assertEquals(failureMessage, MOCKED_NOW_PLUS_TWO_SEC, new File(unjarDir, FOOBAZ_TXT).lastModified()); }
@Test public void testUnJar2() throws IOException { File jarFile = new File(TEST_ROOT_DIR, TEST_JAR_NAME); JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jarFile)); JarEntry je = new JarEntry("META-INF/MANIFEST.MF"); byte[] data = "Manifest-Version: 1.0\nCreated-By: 1.8.0_1 (Manual)" .getBytes(StandardCharsets.UTF_8); je.setSize(data.length); jstream.putNextEntry(je); jstream.write(data); jstream.closeEntry(); je = new JarEntry("../outside.path"); data = "any data here".getBytes(StandardCharsets.UTF_8); je.setSize(data.length); jstream.putNextEntry(je); jstream.write(data); jstream.closeEntry(); jstream.close(); File unjarDir = getUnjarDir("unjar-path"); try { RunJar.unJar(jarFile, unjarDir, MATCH_ANY); fail("unJar should throw IOException."); } catch (IOException e) { GenericTestUtils.assertExceptionContains( "would create file outside of", e); } try { RunJar.unJar(new FileInputStream(jarFile), unjarDir, MATCH_ANY); fail("unJar should throw IOException."); } catch (IOException e) { GenericTestUtils.assertExceptionContains( "would create file outside of", e); } }