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); } }
private void setOutput(boolean needToZip, File file, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); if (needToZip) { new ZipUtil().zip(file, out, Deflater.NO_COMPRESSION); } else { IOUtils.copy(new FileInputStream(file), out); } out.flush(); }
private boolean shouldReplaceBundledPlugins(File bundledPluginsDirectory) throws IOException { File versionFile = new File(bundledPluginsDirectory, "version.txt"); if (!versionFile.exists()) return true; String currentlyInstalledVersion = FileUtils.readFileToString(versionFile, UTF_8); String versionNumberInZip = zipUtil.getFileContentInsideZip(getPluginsZipStream(), "version.txt"); return !currentlyInstalledVersion.equals(versionNumberInZip); }
@Autowired public AnalyticsPluginAssetsService(AnalyticsExtension analyticsExtension, AnalyticsMetadataLoader metadataLoader) { this.zipUtil = new ZipUtil(); this.analyticsExtension = analyticsExtension; this.pluginAssetPaths = new HashMap<>(); metadataLoader.registerListeners(this); metadataStore = AnalyticsMetadataStore.instance(); }
public void unzip(File zip, File destDir) throws IOException { unzip(new ZipInputStream(new BufferedInputStream(new FileInputStream(zip))), destDir); }
@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 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))); }
@Override void createCachedFile(ArtifactFolder artifactFolder) throws IOException { File originalFolder = artifactFolder.getRootFolder(); File cachedZip = cachedFile(artifactFolder); File cachedTempZip = zipToTempFile(cachedZip); cachedTempZip.getParentFile().mkdirs(); try { zipUtil.zip(originalFolder, cachedTempZip, Deflater.DEFAULT_COMPRESSION); } catch (IOException e) { cachedTempZip.delete(); throw e; } FileUtils.moveFile(cachedTempZip, cachedZip); }
@Test public void shouldReadContentsOfAFileWhichIsInsideAZip() throws Exception { FileUtils.writeStringToFile(new File(srcDir, "some-file.txt"), "some-text-here", UTF_8); zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); String someStuff = zipUtil.getFileContentInsideZip(new ZipInputStream(new FileInputStream(zipFile)), "some-file.txt"); assertThat(someStuff, is("some-text-here")); }
@Test public void shouldZipMultipleFolderContentsAndExcludeRootDirectory() throws IOException { File folderOne = temporaryFolder.newFolder("a-folder1"); FileUtils.writeStringToFile(new File(folderOne, "folder1-file1.txt"), "folder1-file1", UTF_8); FileUtils.writeStringToFile(new File(folderOne, "folder1-file2.txt"), "folder1-file2", UTF_8); File folderTwo = temporaryFolder.newFolder("a-folder2"); FileUtils.writeStringToFile(new File(folderTwo, "folder2-file1.txt"), "folder2-file1", UTF_8); FileUtils.writeStringToFile(new File(folderTwo, "folder2-file2.txt"), "folder2-file2", UTF_8); File targetZipFile = temporaryFolder.newFile("final1.zip"); ZipBuilder zipBuilder = zipUtil.zipContentsOfMultipleFolders(targetZipFile, true); zipBuilder.add("folder-one", folderOne); zipBuilder.add("folder-two", folderTwo); zipBuilder.done(); assertContent(targetZipFile, "folder-one/folder1-file1.txt", "folder1-file1"); assertContent(targetZipFile, "folder-one/folder1-file2.txt", "folder1-file2"); assertContent(targetZipFile, "folder-two/folder2-file1.txt", "folder2-file1"); assertContent(targetZipFile, "folder-two/folder2-file2.txt", "folder2-file2"); }
public void zipFolderContents(File destDir, File destZipFile) throws IOException { zipFolderContents(destDir, destZipFile, Deflater.BEST_SPEED); }
public GoArtifactsManipulatorStub(List<Property> properties, List<String> consoleOuts, HttpService service) { super(service, new URLService(), new ZipUtil()); this.properties = properties; this.consoleOuts = consoleOuts; }
void usePackagedCommandRepository(ZipInputStream zipInputStream, File defaultDirectory) throws IOException { FileUtil.deleteDirectoryNoisily(defaultDirectory); zipUtil.unzip(zipInputStream, defaultDirectory); }
@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))); }
@Test public void shouldPreserveFileTimestampWhileGeneratingTheZipFile() throws Exception { File file = temporaryFolder.newFile("foo.txt"); file.setLastModified(1297989100000L); // Set this to any date in the past which is greater than the epoch File zip = zipUtil.zip(file, temporaryFolder.newFile("foo.zip"), Deflater.DEFAULT_COMPRESSION); ZipFile actualZip = new ZipFile(zip.getAbsolutePath()); ZipEntry entry = actualZip.getEntry(file.getName()); assertThat(entry.getTime(), is(file.lastModified())); }
@Test public void shouldZipMultipleFolderContentsWhenNotExcludingRootDirectory() throws IOException { File folderOne = temporaryFolder.newFolder("folder1"); FileUtils.writeStringToFile(new File(folderOne, "folder1-file1.txt"), "folder1-file1", UTF_8); FileUtils.writeStringToFile(new File(folderOne, "folder1-file2.txt"), "folder1-file2", UTF_8); File folderTwo = temporaryFolder.newFolder("folder2"); FileUtils.writeStringToFile(new File(folderTwo, "folder2-file1.txt"), "folder2-file1", UTF_8); FileUtils.writeStringToFile(new File(folderTwo, "folder2-file2.txt"), "folder2-file2", UTF_8); File targetZipFile = temporaryFolder.newFile("final2.zip"); ZipBuilder zipBuilder = zipUtil.zipContentsOfMultipleFolders(targetZipFile, false); zipBuilder.add("folder-one", folderOne); zipBuilder.add("folder-two", folderTwo); zipBuilder.done(); assertContent(targetZipFile, "folder-one/folder1/folder1-file1.txt", "folder1-file1"); assertContent(targetZipFile, "folder-one/folder1/folder1-file2.txt", "folder1-file2"); assertContent(targetZipFile, "folder-two/folder2/folder2-file1.txt", "folder2-file1"); assertContent(targetZipFile, "folder-two/folder2/folder2-file2.txt", "folder2-file2"); }
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); }
private File createZip(String subDirectoryName) throws IOException { File first = new File(artifactDest, "first"); FileUtils.writeStringToFile(first, "First File", UTF_8); File second = new File(artifactDest, subDirectoryName + "/second"); FileUtils.writeStringToFile(second, "Second File", UTF_8); new ZipUtil().zip(artifactDest, zip, 0); return zip; }
public GoArtifactsManipulatorStub(HttpService service) { super(service, new URLService(), new ZipUtil()); properties = new ArrayList<>(); consoleOuts = new ArrayList<>(); }
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; } }