public static File unzip(InputStream zip, File toDir) throws IOException { return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true); }
/** * 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); }
/** * @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)); }
/** * @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 unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = pluginFiles.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }
@Override public ExplodedPlugin explode(PluginInfo pluginInfo) { File tempDir = new File(fs.getTempDir(), TEMP_RELATIVE_PATH); File toDir = new File(tempDir, pluginInfo.getKey()); try { org.sonar.core.util.FileUtils.cleanDirectory(toDir); File jarSource = pluginInfo.getNonNullJarFile(); File jarTarget = new File(toDir, jarSource.getName()); FileUtils.copyFile(jarSource, jarTarget); ZipUtils.unzip(jarSource, toDir, newLibFilter()); return explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); } catch (Exception e) { throw new IllegalStateException(String.format( "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e); } } }
/** * JAR files of directory extensions/plugins can be moved when server is up and plugins are uninstalled. * For this reason these files must not be locked by classloaders. They are copied to the directory * web/deploy/plugins in order to be loaded by {@link org.sonar.core.platform.PluginLoader}. */ @Override public ExplodedPlugin explode(PluginInfo pluginInfo) { File toDir = new File(fs.getDeployedPluginsDir(), pluginInfo.getKey()); try { forceMkdir(toDir); org.sonar.core.util.FileUtils.cleanDirectory(toDir); File jarSource = pluginInfo.getNonNullJarFile(); File jarTarget = new File(toDir, jarSource.getName()); FileUtils.copyFile(jarSource, jarTarget); ZipUtils.unzip(jarSource, toDir, newLibFilter()); ExplodedPlugin explodedPlugin = explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); pluginFileSystem.addInstalledPlugin(pluginInfo, jarTarget); return explodedPlugin; } catch (Exception e) { throw new IllegalStateException(String.format( "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e); } } }
@Test public void unzipping_stream_extracts_subset_of_files() throws IOException { InputStream zip = urlToZip().openStream(); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt")); assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt")); }
@Test public void fail_if_unzipping_stream_outside_target_directory() throws Exception { File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI()); File toDir = temp.newFolder(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"); try (InputStream input = new FileInputStream(zip)) { ZipUtils.unzip(input, toDir); } }
@Test public void fail_if_unzipping_file_outside_target_directory() throws Exception { File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI()); File toDir = temp.newFolder(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"); ZipUtils.unzip(zip, toDir); }
@Test public void unzipping_file_extracts_subset_of_files() throws IOException { File zip = FileUtils.toFile(urlToZip()); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt")); assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt")); }
@Test public void unzip_stream() throws Exception { InputStream zip = urlToZip().openStream(); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir); assertThat(toDir.list()).hasSize(3); }
@Test public void unzip_file() throws IOException { File zip = FileUtils.toFile(urlToZip()); File toDir = temp.newFolder(); ZipUtils.unzip(zip, toDir); assertThat(toDir.list()).hasSize(3); }
@Override public ExplodedPlugin explode(PluginInfo info) { try { ZipUtils.unzip(jarFile, toDir, newLibFilter()); return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), toDir); } catch (Exception e) { throw new IllegalStateException(e); } } };
@Test public void unzipping_creates_target_directory_if_it_does_not_exist() throws IOException { File zip = FileUtils.toFile(urlToZip()); File tempDir = temp.newFolder(); Files.delete(tempDir); File subDir = new File(tempDir, "subDir"); ZipUtils.unzip(zip, subDir); assertThat(subDir.list()).hasSize(3); }
@Override public void execute(ComputationStep.Context context) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid()); if (opt.isPresent()) { File unzippedDir = tempFolder.newDir(); try (CeTaskInputDao.DataStream reportStream = opt.get(); InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) { ZipUtils.unzip(zipStream, unzippedDir); } catch (IOException e) { throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e); } reportDirectoryHolder.setDirectory(unzippedDir); if (LOGGER.isDebugEnabled()) { // size is not added to context statistics because computation // can take time. It's enabled only if log level is DEBUG. try { String dirSize = FileUtils.byteCountToDisplaySize(FileUtils2.sizeOf(unzippedDir.toPath())); LOGGER.debug("Analysis report is {} uncompressed", dirSize); } catch (IOException e) { LOGGER.warn("Fail to compute size of directory " + unzippedDir, e); } } } else { throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database"); } } }
@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 zip, File toDir) throws IOException { unzip(zip, toDir, new ZipEntryFilter() { @Override public boolean accept(ZipEntry entry) { return true; } }); return toDir; }
/** * 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); }
/** * @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)); }