/** * Generates the archive, saving it with the given name. */ public File create(String archiveFile) { return named(archiveFile).create(); }
/** * Deploys an archive file to a remote machine and extracts the contents. * <p> * Copies the archive file from the given URL to the destination directory and extracts * the contents. If the URL is a local directory, the contents are packaged as a Zip archive first. * * @see #deploy(String, SshMachineLocation, String, String) * @see #deploy(Map, String, SshMachineLocation, String, String, String) */ public static void deploy(Map<String, ?> props, String archiveUrl, SshMachineLocation machine, String destDir) { if (Urls.isDirectory(archiveUrl)) { File zipFile = ArchiveBuilder.zip().entry(".", Urls.toFile(archiveUrl)).create(); archiveUrl = zipFile.getAbsolutePath(); } // Determine filename String destFile = archiveUrl.contains("?") ? archiveUrl.substring(0, archiveUrl.indexOf('?')) : archiveUrl; destFile = destFile.substring(destFile.lastIndexOf('/') + 1); deploy(props, archiveUrl, machine, destDir, destFile); }
public ParsingResult<Csar> uploadSingleYaml(InputStream resourceFromUrl, String callerReferenceName) { try { String nameCleaned = Strings.makeValidFilename(callerReferenceName); File tmpBase = new File(tmpRoot, nameCleaned + "_" + Identifiers.makeRandomId(6)); File tmpExpanded = new File(tmpBase, nameCleaned + "_" + Identifiers.makeRandomId(6)); File tmpTarget = new File(tmpExpanded.toString() + ".csar.zip"); boolean created = tmpExpanded.mkdirs(); if (!created) { throw new Exception("Failed to create '" + tmpExpanded + "' when uploading yaml from " + nameCleaned); } FileUtils.copyInputStreamToFile(resourceFromUrl, new File(tmpExpanded, nameCleaned + ".yaml")); ArchiveBuilder.archive(tmpTarget.toString()).addDirContentsAt(tmpExpanded, "").create(); try { return uploadArchive(tmpTarget, callerReferenceName); } finally { Os.deleteRecursively(tmpBase); } } catch (Exception e) { throw Exceptions.propagate(e); } }
private void buildAndValidatePrefix(ArchiveBuilder builder, String prefix) throws FileNotFoundException, IOException { File archive = builder.create(); archive.deleteOnExit(); List<ZipEntry> entries = Lists.newArrayList(); ZipInputStream input = new ZipInputStream(new FileInputStream(archive)); ZipEntry entry = input.getNextEntry(); while (entry != null) { entries.add(entry); entry = input.getNextEntry(); } assertEquals(entries.size(), 3); Iterable<ZipEntry> directories = Iterables.filter(entries, isDirectory); Iterable<ZipEntry> files = Iterables.filter(entries, Predicates.not(isDirectory)); assertTrue(Iterables.isEmpty(directories)); assertEquals(Iterables.size(files), 3); for (ZipEntry file : files) { assertTrue(file.getName().startsWith(prefix), "File is: "+file+"; missing "+prefix); } input.close(); }
private File newZip(Map<String, String> files) throws Exception { File parentDir = Os.newTempDir(getClass().getSimpleName()+"-archive"); for (Map.Entry<String, String> entry : files.entrySet()) { File subFile = new File(Os.mergePaths(parentDir.getAbsolutePath(), entry.getKey())); subFile.getParentFile().mkdirs(); Files.write(entry.getValue(), subFile, Charsets.UTF_8); } return ArchiveBuilder.zip().addDirContentsAt(parentDir, ".").create(); }
private String createJar(File dir, String parentDirInJar) { if (dir.isDirectory()) { File jarFile = ArchiveBuilder.jar().addAt(dir, parentDirInJar).create(Os.newTempDir(getClass())+"/topologies.jar"); return jarFile.getAbsolutePath(); } else { return dir.getAbsolutePath(); // An existing Jar archive? } }
@Test public void testCreateZipFromDir() throws Exception { File archive = ArchiveBuilder.zip().addDirContentsAt(tmpDir, ".").create(); archive.deleteOnExit(); List<ZipEntry> entries = Lists.newArrayList(); ZipInputStream input = new ZipInputStream(new FileInputStream(archive)); ZipEntry entry = input.getNextEntry(); while (entry != null) { entries.add(entry); entry = input.getNextEntry(); } assertEquals(entries.size(), 4); Iterable<ZipEntry> directories = Iterables.filter(entries, isDirectory); Iterable<ZipEntry> files = Iterables.filter(entries, Predicates.not(isDirectory)); assertEquals(Iterables.size(directories), 1); assertEquals(Iterables.size(files), 3); String dirName = Iterables.getOnlyElement(directories).getName(); assertEquals(dirName, "./"); Set<String> names = MutableSet.of(); for (ZipEntry file : files) { assertTrue(file.getName().startsWith(dirName)); names.add(file.getName()); } assertTrue(names.contains("./data01.txt")); assertFalse(names.contains("./data04.txt")); input.close(); }
@Test public void testCreateZipFromTwoDirs() throws Exception { File archive = ArchiveBuilder.zip().addDirContentsAt(tmpDir, ".").addDirContentsAt(tmpDir2, ".").create(); archive.deleteOnExit(); List<ZipEntry> entries = Lists.newArrayList(); ZipInputStream input = new ZipInputStream(new FileInputStream(archive)); ZipEntry entry = input.getNextEntry(); while (entry != null) { entries.add(entry); entry = input.getNextEntry(); } assertEquals(entries.size(), 5); Iterable<ZipEntry> directories = Iterables.filter(entries, isDirectory); Iterable<ZipEntry> files = Iterables.filter(entries, Predicates.not(isDirectory)); assertEquals(Iterables.size(directories), 1); assertEquals(Iterables.size(files), 4); String dirName = Iterables.getOnlyElement(directories).getName(); assertEquals(dirName, "./"); Set<String> names = MutableSet.of(); for (ZipEntry file : files) { assertTrue(file.getName().startsWith(dirName)); names.add(file.getName()); } assertTrue(names.contains("./data01.txt")); assertTrue(names.contains("./data04.txt")); input.close(); }
File jarFile = ArchiveBuilder.jar().addDirContentsAt(new File(entry), "").create(); entry = jarFile.getAbsolutePath();
File jarFile = ArchiveBuilder.jar().addDirContentsAt(new File(url), "").create(); url = jarFile.getAbsolutePath();