/** * Unzip a file into a directory. The directory is created if it does not exist. * * @return the target directory */ public static File unzip(File zip, File toDir) throws IOException { return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true); }
private File generateReportFile() { try { long startTime = System.currentTimeMillis(); for (ReportPublisherStep publisher : publishers) { publisher.publish(writer); } long stopTime = System.currentTimeMillis(); LOG.info("Analysis report generated in {}ms, dir size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir.toFile()))); startTime = System.currentTimeMillis(); File reportZip = temp.newFile("scanner-report", ".zip"); ZipUtils.zipDir(reportDir.toFile(), reportZip); stopTime = System.currentTimeMillis(); LOG.info("Analysis report compressed in {}ms, zip size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip))); return reportZip; } catch (IOException e) { throw new IllegalStateException("Unable to prepare analysis report", e); } }
private static void unzipEntry(ZipEntry entry, ZipInputStream zipStream, Path targetDirNormalized) throws IOException { File to = targetDirNormalized.resolve(entry.getName()).toFile(); verifyInsideTargetDirectory(entry, to.toPath(), targetDirNormalized); if (entry.isDirectory()) { throwExceptionIfDirectoryIsNotCreatable(to); } else { File parent = to.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipStream, to); } }
@Test public void zip_directory() throws IOException { File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt")); File dir = foo.getParentFile(); File zip = temp.newFile(); ZipUtils.zipDir(dir, zip); assertThat(zip).exists().isFile(); assertThat(zip.length()).isGreaterThan(1L); Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries()); assertThat(zipEntries).hasSize(4); File unzipDir = temp.newFolder(); ZipUtils.unzip(zip, unzipDir); assertThat(new File(unzipDir, "bar.txt")).exists().isFile(); assertThat(new File(unzipDir, "foo.txt")).exists().isFile(); assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile(); }
public static File unzip(InputStream stream, File toDir, ZipEntryFilter filter) throws IOException { if (!toDir.exists()) { FileUtils.forceMkdir(toDir); } ZipInputStream zipStream = new ZipInputStream(stream); try { ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { if (filter.accept(entry)) { File to = new File(toDir, entry.getName()); if (entry.isDirectory()) { throwExceptionIfDirectoryIsNotCreatable(to); } else { File parent = to.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipStream, to); } } } return toDir; } finally { zipStream.close(); } }
private static void doZipDir(File dir, ZipOutputStream out) throws IOException { File[] children = dir.listFiles(); if (children == null) { throw new IllegalStateException("Fail to list files of directory " + dir.getAbsolutePath()); } for (File child : children) { doZip(child.getName(), child, out); } }
public static File unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException { if (!toDir.exists()) { FileUtils.forceMkdir(toDir); } ZipFile zipFile = new ZipFile(zip); try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (filter.accept(entry)) { File to = new File(toDir, entry.getName()); if (entry.isDirectory()) { throwExceptionIfDirectoryIsNotCreatable(to); } else { File parent = to.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipFile, entry, to); } } } return toDir; } finally { zipFile.close(); } }
private static void doZip(String entryName, File file, ZipOutputStream out) throws IOException { if (file.isDirectory()) { entryName += "/"; ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); out.closeEntry(); File[] files = file.listFiles(); // java.io.File#listFiles() returns null if object is a directory (not possible here) or if // an I/O error occurs (weird!) if (files == null) { throw new IllegalStateException("Fail to list files of directory " + file.getAbsolutePath()); } for (File f : files) { doZip(entryName + f.getName(), f, out); } } else { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { doZip(entryName, in, out); } } }
public static File unzip(InputStream zip, File toDir) throws IOException { return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true); }
private File generateReport() throws IOException { File zipDir = tempFolder.newDir(); File metadataFile = new File(zipDir, "metadata.pb"); FileUtils.write(metadataFile, "{metadata}"); File zip = tempFolder.newFile(); ZipUtils.zipDir(zipDir, zip); return zip; } }
File target = new File(toDir, entry.getName()); verifyInsideTargetDirectory(entry, target.toPath(), targetDirNormalizedPath); throwExceptionIfDirectoryIsNotCreatable(target); } else { File parent = target.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipFile, entry, target);
private static void zip(File file, ZipOutputStream out) throws IOException { for (File child : file.listFiles()) { String name = child.getName(); doZip(name, child, out); } }
/** * @deprecated replaced by {@link #unzip(InputStream, File, Predicate)} in 6.2. */ @Deprecated public static File unzip(InputStream stream, File toDir, ZipEntryFilter filter) throws IOException { return unzip(stream, toDir, new ZipEntryFilterDelegate(filter)); }
@Test public void fail_when_jar_is_not_a_plugin() throws IOException { // this JAR has a manifest but is not a plugin File jarRootDir = temp.newFolder(); FileUtils.write(new File(jarRootDir, "META-INF/MANIFEST.MF"), "Build-Jdk: 1.6.0_15"); File jar = temp.newFile(); ZipUtils.zipDir(jarRootDir, jar); expectedException.expect(MessageException.class); expectedException.expectMessage("File is not a plugin. Please delete it and restart: " + jar.getAbsolutePath()); PluginInfo.create(jar); }
private static void unzipEntry(ZipEntry entry, ZipInputStream zipStream, Path targetDirNormalized) throws IOException { File to = targetDirNormalized.resolve(entry.getName()).toFile(); verifyInsideTargetDirectory(entry, to.toPath(), targetDirNormalized); if (entry.isDirectory()) { throwExceptionIfDirectoryIsNotCreatable(to); } else { File parent = to.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipStream, to); } }
private static void doZipDir(File dir, ZipOutputStream out) throws IOException { File[] children = dir.listFiles(); if (children == null) { throw new IllegalStateException("Fail to list files of directory " + dir.getAbsolutePath()); } for (File child : children) { doZip(child.getName(), child, out); } }
/** * @deprecated replaced by {@link #unzip(File, File, Predicate)} in 6.2. */ @Deprecated public static File unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException { return unzip(zip, toDir, new ZipEntryFilterDelegate(filter)); }
private File generateReportFile() { try { long startTime = System.currentTimeMillis(); for (ReportPublisherStep publisher : publishers) { publisher.publish(writer); } long stopTime = System.currentTimeMillis(); LOG.info("Analysis report generated in {}ms, dir size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir.toFile()))); startTime = System.currentTimeMillis(); File reportZip = temp.newFile("scanner-report", ".zip"); ZipUtils.zipDir(reportDir.toFile(), reportZip); stopTime = System.currentTimeMillis(); LOG.info("Analysis report compressed in {}ms, zip size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip))); return reportZip; } catch (IOException e) { throw new IllegalStateException("Unable to prepare analysis report", e); } }
File target = new File(toDir, entry.getName()); verifyInsideTargetDirectory(entry, target.toPath(), targetDirNormalizedPath); throwExceptionIfDirectoryIsNotCreatable(target); } else { File parent = target.getParentFile(); throwExceptionIfDirectoryIsNotCreatable(parent); copy(zipFile, entry, target);
private static void doZip(String entryName, File file, ZipOutputStream out) throws IOException { if (file.isDirectory()) { entryName += '/'; ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); out.closeEntry(); File[] files = file.listFiles(); for (int i = 0, len = files.length; i < len; i++) { doZip(entryName + files[i].getName(), files[i], out); } } else { InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); doZip(entryName, in, out); } finally { IOUtils.closeQuietly(in); } } }