ZipUtil { public static void unzip(File zipFile, File destDir, String selector) throws IOException { final String sourceMethod = "unzip"; final boolean isTraceLogging = log.isLoggable(Level.FINER); if (isTraceLogging) { log.entering(sourceClass, sourceMethod, new Object[]{zipFile, destDir}); } boolean selectorIsFolder = selector != null && selector.charAt(selector.length()-1) == '/'; ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile)); try { ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String entryName = entry.getName(); if (selector == null || !selectorIsFolder && entryName.equals(selector) || selectorIsFolder && entryName.startsWith(selector) && entryName.length() != selector.length()) { if (selector != null) { if (selectorIsFolder) { entryName = entryName.substring(selector.length()); } else { int idx = entryName.lastIndexOf("/"); if (idx != -1) { entryName = entryName.substring(idx+1); } } } File file = new File(destDir, entryName.replace("/", File.separator)); if (!entry.isDirectory()) { extractFile(entry, zipIn, file); } else { extractDirectory(entry, file); } zipIn.closeEntry(); } entry = zipIn.getNextEntry(); } } finally { zipIn.close(); } if (isTraceLogging) { log.exiting(sourceClass, sourceMethod); } }  static void unzip(File zipFile, File destDir, String selector); static byte[] zip(InputStream in); static byte[] unzip(InputStream in);  }
@Test public void testNullPackerRoot() throws IOException { File zipFile = new File(tmpdir, "test.zip"); ZipUtil.Packer packer = new ZipUtil.Packer(); packer.open(zipFile); packer.packDirectory(sourceDir, null); packer.close(); ZipUtil.unzip(zipFile, targetDir, null); assertDirectoriesSame(sourceDir, targetDir); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "rootDir/"); assertDirectoriesSame(new File(sourceDir, "rootDir"), targetDir); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "rootDir/subDir/"); assertDirectoriesSame(new File(sourceDir, "rootDir/subDir"), targetDir); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "rootFile.txt"); Assert.assertEquals(1, targetDir.listFiles().length); assertFilesSame(new File(sourceDir, "rootFile.txt"), new File(targetDir, "rootFile.txt")); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "rootDir/subDir/file2"); Assert.assertEquals(1, targetDir.listFiles().length); assertFilesSame(new File(sourceDir, "rootDir/subDir/file2"), new File(targetDir, "file2")); }
@Test public void testNonNullPackerRoot() throws Exception { File zipFile = new File(tmpdir, "test.zip"); ZipUtil.Packer packer = new ZipUtil.Packer(); packer.open(zipFile); packer.packDirectory(sourceDir, "foo/bar"); packer.close(); ZipUtil.unzip(zipFile, targetDir, null); assertDirectoriesSame(sourceDir, new File(targetDir, "foo/bar")); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "foo/bar/rootDir/"); assertDirectoriesSame(new File(sourceDir, "rootDir"), targetDir); FileUtils.deleteDirectory(targetDir); targetDir.mkdir(); ZipUtil.unzip(zipFile, targetDir, "foo/bar/rootFile.txt"); Assert.assertEquals(1, targetDir.listFiles().length); assertFilesSame(new File(sourceDir, "rootFile.txt"), new File(targetDir, "rootFile.txt")); }