public void unzip(File zip, File destDir) throws IOException { unzip(new ZipInputStream(new BufferedInputStream(new FileInputStream(zip))), destDir); }
void usePackagedCommandRepository(ZipInputStream zipInputStream, File defaultDirectory) throws IOException { FileUtil.deleteDirectoryNoisily(defaultDirectory); zipUtil.unzip(zipInputStream, defaultDirectory); }
void explodePluginJarToBundleDir(File file, File location) { try { wipePluginBundleDirectory(location); ZipUtil zipUtil = new ZipUtil(); zipUtil.unzip(file, location); } catch (IOException e) { throw new RuntimeException(String.format("Failed to copy plugin jar %s to bundle location %s", file, location), e); } }
public boolean saveFile(File dest, InputStream stream, boolean shouldUnzip, int attempt) { String destPath = dest.getAbsolutePath(); try { LOGGER.trace("Saving file [{}]", destPath); if (shouldUnzip) { zipUtil.unzip(new ZipInputStream(stream), dest); } else { systemService.streamToFile(stream, dest); } LOGGER.trace("File [{}] saved.", destPath); return true; } catch (IOException e) { final String message = format("Failed to save the file to: [%s]", destPath); if (attempt < GoConstants.PUBLISH_MAX_RETRIES) { LOGGER.warn(message, e); } else { LOGGER.error(message, e); } return false; } catch (IllegalPathException e) { final String message = format("Failed to save the file to: [%s]", destPath); LOGGER.error(message, e); return false; } }
private void cacheStaticAssets(String pluginId) { LOGGER.info("Caching static assets for plugin: {}", pluginId); String data = this.analyticsExtension.getStaticAssets(pluginId); if (StringUtils.isBlank(data)) { LOGGER.info("No static assets found for plugin: {}", pluginId); return; } try { byte[] payload = Base64.getDecoder().decode(data.getBytes()); byte[] pluginEndpointJsContent = IOUtils.toByteArray(getClass().getResourceAsStream("/" + PLUGIN_ENDPOINT_JS)); ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(payload)); String assetsHash = calculateHash(payload, pluginEndpointJsContent); String pluginAssetsRoot = currentAssetPath(pluginId, assetsHash); zipUtil.unzip(zipInputStream, new File(pluginAssetsRoot)); Files.write(Paths.get(pluginAssetsRoot, DESTINATION_JS), pluginEndpointJsContent); pluginAssetPaths.put(pluginId, Paths.get(pluginStaticAssetsPathRelativeToRailsPublicFolder(pluginId), assetsHash).toString()); } catch (Exception e) { LOGGER.error("Failed to extract static assets from plugin: {}", pluginId, e); ExceptionUtils.bomb(e); } }
public void handle(InputStream stream) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(stream); LOG.info("[Agent Fetch Artifact] Downloading from '{}' to '{}'. Will read from Socket stream to compute MD5 and write to file", srcFile, destOnAgent.getAbsolutePath()); long before = System.currentTimeMillis(); new ZipUtil((entry, stream1) -> { LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Handling the entry: '{}'", srcFile, destOnAgent.getAbsolutePath(), entry.getName()); new ChecksumValidator(artifactMd5Checksums).validate(getSrcFilePath(entry), md5Hex(stream1), checksumValidationPublisher); }).unzip(zipInputStream, destOnAgent); LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Took: {}ms", srcFile, destOnAgent.getAbsolutePath(), System.currentTimeMillis() - before); }
@Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { try { File pluginsFolder = new File(systemEnvironment.get(SystemEnvironment.AGENT_PLUGINS_PATH)); if (pluginsFolder.exists()) { FileUtils.forceDelete(pluginsFolder); } zipUtil.unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), pluginsFolder); defaultPluginJarLocationMonitor.initialize(); pluginManager.startInfrastructure(false); } catch (IOException e) { LOG.warn("could not extract plugin zip", e); } catch (RuntimeException e) { LOG.warn("error while initializing agent plugins", e); } } }
@Override public void initialize() { cleanupOldPluginDirectories(); try { File bundledPluginsDirectory = new File(systemEnvironment.get(PLUGIN_GO_PROVIDED_PATH)); if (shouldReplaceBundledPlugins(bundledPluginsDirectory)) { FileUtils.cleanDirectory(bundledPluginsDirectory); zipUtil.unzip(getPluginsZipStream(), bundledPluginsDirectory); } pluginManager.startInfrastructure(true); } catch (Exception e) { LOG.error("Could not extract bundled plugins to default bundled directory", e); } }
private File createPluginBundle(String bundleName) throws IOException, URISyntaxException { File destinationPluginBundleLocation = new File(tmpDir, bundleName); destinationPluginBundleLocation.mkdirs(); URL resource = getClass().getClassLoader().getResource("defaultFiles/descriptor-aware-test-plugin.jar"); new ZipUtil().unzip(new File(resource.toURI()), destinationPluginBundleLocation); return destinationPluginBundleLocation; } }
private File explodeBundleIntoDirectory(ZipInputStream src, String destinationDir) throws IOException { File destinationPluginBundleLocation = new File(tmpDir, destinationDir); destinationPluginBundleLocation.mkdirs(); new ZipUtil().unzip(src, destinationPluginBundleLocation); return destinationPluginBundleLocation; }
@Test public void shouldAllExceptionsExceptionQuietly() throws Exception { doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); try { doThrow(new RuntimeException("message")).when(pluginJarLocationMonitor).initialize(); agentPluginsInitializer.onApplicationEvent(null); } catch (Exception e) { fail("should have handled IOException"); } } }
@Test public void shouldExtractPluginZip() throws Exception { agentPluginsInitializer.onApplicationEvent(null); verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); }
private File explodeBundleIntoDirectory(ZipInputStream src, String destinationDir) throws IOException, URISyntaxException { File destinationPluginBundleLocation = temporaryFolder.newFolder(destinationDir); new ZipUtil().unzip(src, destinationPluginBundleLocation); return destinationPluginBundleLocation; } }
@Test public void shouldThrowUpWhileTryingToUnzipIfAnyOfTheFilePathsInArchiveHasAPathContainingDotDotSlashPath() throws URISyntaxException, IOException { try { zipUtil.unzip(new File(getClass().getResource("/archive_traversal_attack.zip").toURI()), destDir); fail("squash.zip is capable of causing archive traversal attack and hence should not be allowed."); } catch (IllegalPathException e) { assertThat(e.getMessage(), is("File ../2.txt is outside extraction target directory")); } }
@Test public void shouldHandleIOExceptionQuietly() throws Exception { doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); try { agentPluginsInitializer.onApplicationEvent(null); } catch (Exception e) { fail("should have handled IOException"); } }
@Test @RunIf(value = OSChecker.class, arguments = OSChecker.LINUX) public void shouldZipFileWhoseNameHasSpecialCharactersOnLinux() throws IOException { File specialFile = new File(srcDir, "$`#?@!()?-_{}^'~.+=[];,a.txt"); FileUtils.writeStringToFile(specialFile, "specialFile", UTF_8); zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); zipUtil.unzip(zipFile, destDir); File baseDir = new File(destDir, srcDir.getName()); File actualSpecialFile = new File(baseDir, specialFile.getName()); assertThat(actualSpecialFile.isFile(), is(true)); assertThat(fileContent(actualSpecialFile), is(fileContent(specialFile))); }
@Test public void shouldInitializePluginJarLocationMonitorAndStartPluginInfrastructureAfterPluginZipExtracted() throws Exception { InOrder inOrder = inOrder(zipUtil, pluginManager, pluginJarLocationMonitor); agentPluginsInitializer.onApplicationEvent(null); inOrder.verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); inOrder.verify(pluginJarLocationMonitor).initialize(); inOrder.verify(pluginManager).startInfrastructure(false); }
@Test public void shouldZipFileAndUnzipIt() throws IOException { zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); assertThat(zipFile.isFile(), is(true)); zipUtil.unzip(zipFile, destDir); File baseDir = new File(destDir, srcDir.getName()); assertIsDirectory(new File(baseDir, emptyDir.getName())); assertIsDirectory(new File(baseDir, childDir1.getName())); File actual1 = new File(baseDir, file1.getName()); assertThat(actual1.isFile(), is(true)); assertThat(fileContent(actual1), is(fileContent(file1))); File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName()); assertThat(actual2.isFile(), is(true)); assertThat(fileContent(actual2), is(fileContent(file2))); }
@Test public void shouldZipFileContentsOnly() throws IOException { zipFile = zipUtil.zipFolderContents(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); assertThat(zipFile.isFile(), is(true)); zipUtil.unzip(zipFile, destDir); assertIsDirectory(new File(destDir, emptyDir.getName())); assertIsDirectory(new File(destDir, childDir1.getName())); File actual1 = new File(destDir, file1.getName()); assertThat(actual1.isFile(), is(true)); assertThat(fileContent(actual1), is(fileContent(file1))); File actual2 = new File(destDir, childDir1.getName() + File.separator + file2.getName()); assertThat(actual2.isFile(), is(true)); assertThat(fileContent(actual2), is(fileContent(file2))); }
@Test public void shouldZipFileContentsAndUnzipIt() throws IOException { zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); assertThat(zipFile.isFile(), is(true)); zipUtil.unzip(zipFile, destDir); File baseDir = new File(destDir, srcDir.getName()); assertIsDirectory(new File(baseDir, emptyDir.getName())); assertIsDirectory(new File(baseDir, childDir1.getName())); File actual1 = new File(baseDir, file1.getName()); assertThat(actual1.isFile(), is(true)); assertThat(fileContent(actual1), is(fileContent(file1))); File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName()); assertThat(actual2.isFile(), is(true)); assertThat(fileContent(actual2), is(fileContent(file2))); }