List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
From source file:com.mobilesorcery.sdk.builder.linux.deb.BuilderUtil.java
/** * Recursivly add a directory structure to a tar output stream * * @param t Tar output stream//from w w w .ja va2s. c o m * @param r Relative path up to this point * @param c Current file */ private void doAddFileToTar(TarArchiveOutputStream t, String r, File c) throws IOException { // Update relative path r += (r.isEmpty() ? "" : (r.endsWith("/") ? "" : "/")) + c.getName(); // Is it a file? if (c.isFile() == true) { ArchiveEntry e = t.createArchiveEntry(c, r); t.putArchiveEntry(e); copyFileToOutputStream(t, c); t.closeArchiveEntry(); return; } // It's a directory for (File f : c.listFiles()) doAddFileToTar(t, r, f); }
From source file:algorithm.TarPackaging.java
private void archiveFile(File carrier, TarArchiveOutputStream tarOutputStream) throws IOException, FileNotFoundException { TarArchiveEntry tarEntry = new TarArchiveEntry(carrier, carrier.getName()); tarOutputStream.putArchiveEntry(tarEntry); FileInputStream inputStream = new FileInputStream(carrier); IOUtils.copy(inputStream, tarOutputStream); tarOutputStream.closeArchiveEntry(); inputStream.close();/* w ww. j av a 2 s .c o m*/ }
From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java
/** * Adds the given file to the archive// w w w. ja va 2 s. c om * * @param tOut The tar archive output stream * @param file The file to add * @param base The relative path to the file inside the archive (without the file name) * @throws IOException */ private boolean addFileToArchive(TarArchiveOutputStream tOut, File file, String base) throws IOException { if (sipBuildingProcess.isAborted()) return false; String entryName = base + file.getName(); TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName); tOut.putArchiveEntry(entry); if (file.isFile()) { FileInputStream fis = new FileInputStream(file); IOUtils.copy(fis, tOut); tOut.closeArchiveEntry(); fis.close(); progressManager.archiveProgress(jobId, FileUtils.sizeOf(file)); } if (file.isDirectory()) { tOut.closeArchiveEntry(); File children[] = file.listFiles(); if (children == null) return true; for (int i = 0; i < children.length; i++) { if (!addFileToArchive(tOut, children[i], entryName + "/")) return false; } } return true; }
From source file:com.puppetlabs.geppetto.forge.v2.api.it.ReleaseTestCreate.java
private void putEntry(TarArchiveOutputStream stream, ModuleName moduleName, Version version, String name, String content) throws IOException { StringBuilder fullName = new StringBuilder(); moduleName.toString(fullName);/*from ww w .ja v a 2 s . c om*/ fullName.append('-'); version.toString(fullName); fullName.append('/'); fullName.append(name); TarArchiveEntry entry = new TarArchiveEntry(fullName.toString()); byte[] img = content.getBytes(Charsets.UTF_8); entry.setSize(img.length); stream.putArchiveEntry(entry); stream.write(img); stream.closeArchiveEntry(); }
From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.MavenDeployer.java
private File populateTgzArchive(File archive, List<FileRef> fileset) throws IOException { archive.getParentFile().mkdirs();/* w ww .j av a 2s.c om*/ CompressorOutputStream zip = new GzipCompressorOutputStream(new FileOutputStream(archive)); TarArchiveOutputStream tar = new TarArchiveOutputStream(zip); for (FileRef fileRef : fileset) { TarArchiveEntry entry = new TarArchiveEntry(new File(fileRef.getRelativeLocation())); byte[] bytes = fileRef.getContent().getBytes(); entry.setSize(bytes.length); tar.putArchiveEntry(entry); tar.write(bytes); tar.closeArchiveEntry(); } tar.close(); return archive; }
From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java
/** * @param tOut/* w w w. jav a 2 s .c o m*/ * @param the actual file that should be added * @param base * @throws IOException */ private void addFileToTar(TarArchiveOutputStream tOut, File file, String base) throws IOException { String entryName = base + file.getName(); logger.debug("addFileToTar: " + entryName); TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName); tOut.putArchiveEntry(entry); if (file.isFile()) { FileInputStream fileInputStream = new FileInputStream(file); IOUtils.copy(fileInputStream, tOut); fileInputStream.close(); tOut.closeArchiveEntry(); } if (file.isDirectory()) { tOut.closeArchiveEntry(); File children[] = file.listFiles(); if (children == null) return; for (int i = 0; i < children.length; i++) { addFileToTar(tOut, children[i], entryName + "/"); } } }
From source file:com.netflix.spinnaker.halyard.core.registry.v1.LocalDiskProfileReader.java
public InputStream readArchiveProfileFrom(Path profilePath) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os); ArrayList<Path> filePathsToAdd = java.nio.file.Files .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS) .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new)); for (Path path : filePathsToAdd) { TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString()); tarArchive.putArchiveEntry(tarEntry); IOUtils.copy(Files.newInputStream(path), tarArchive); tarArchive.closeArchiveEntry(); }/* w ww .ja va 2 s .co m*/ tarArchive.finish(); tarArchive.close(); return new ByteArrayInputStream(os.toByteArray()); }
From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java
/** * * @param os//from w w w . j av a 2s .c o m * @throws IOException */ private void doCreateControlTarGZip(File f) throws Exception { File ftemp; FileOutputStream os = new FileOutputStream(f); GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(os); TarArchiveOutputStream tos = new TarArchiveOutputStream(gzos); // Write control file ftemp = File.createTempFile(System.currentTimeMillis() + "", "control"); doWriteControl(ftemp); tos.putArchiveEntry(new TarArchiveEntry(ftemp, "./control")); BuilderUtil.getInstance().copyFileToOutputStream(tos, ftemp); tos.closeArchiveEntry(); ftemp.delete(); // Write md5sums ftemp = File.createTempFile(System.currentTimeMillis() + "", "md5sums"); doWriteMD5SumsToFile(ftemp); tos.putArchiveEntry(new TarArchiveEntry(ftemp, "./md5sums")); BuilderUtil.getInstance().copyFileToOutputStream(tos, ftemp); tos.closeArchiveEntry(); ftemp.delete(); // Add prerm, postrm, preinst, postinst scripts for (Entry<String, String> s : m_scriptMap.entrySet()) { TarArchiveEntry e = new TarArchiveEntry("./" + s.getKey()); e.setSize(s.getValue().length()); tos.putArchiveEntry(e); BuilderUtil.getInstance().copyStringToOutputStream(tos, s.getValue()); tos.closeArchiveEntry(); } // Done tos.close(); gzos.close(); os.close(); }
From source file:hudson.gridmaven.gridlayer.HadoopInstance.java
private void addFileToTar(TarArchiveOutputStream tOut, String path, String base, String root) throws IOException { if (!root.equals(path)) { File f = new File(path); String entryName = base + f.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tOut.putArchiveEntry(tarEntry);/* w w w. j av a 2 s . co m*/ if (f.isFile()) { IOUtils.copy(new FileInputStream(f), tOut); tOut.closeArchiveEntry(); } else { tOut.closeArchiveEntry(); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToTar(tOut, child.getAbsolutePath(), entryName + "/", root); } } } } else { File f = new File(path); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToTar(tOut, child.getAbsolutePath(), "", root); } } } }
From source file:com.netflix.spinnaker.halyard.backup.services.v1.BackupService.java
private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) { File file = new File(path); String fileName = file.getName(); if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) { return;/* w ww . j a v a 2 s . c o m*/ } String tarEntryName = String.join("/", base, fileName); try { if (file.isFile()) { TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName); tarArchiveOutputStream.putArchiveEntry(tarEntry); IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream); tarArchiveOutputStream.closeArchiveEntry(); } else if (file.isDirectory()) { Arrays.stream(file.listFiles()).filter(Objects::nonNull) .forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName)); } else { log.warn("Unknown file type: " + file + " - skipping addition to tar archive"); } } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName() + " to archive entry: " + tarEntryName + " " + e.getMessage(), e); } }