List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream
public TarArchiveOutputStream(OutputStream os)
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param basePath/* w ww.j av a 2s.c o m*/ * @param tarPath * @param filePaths * @throws java.io.IOException */ public static void tar(String basePath, String tarPath, Map<String, String> filePaths) throws IOException { BufferedInputStream origin = null; TarArchiveOutputStream out = null; FileOutputStream dest = null; try { dest = new FileOutputStream(tarPath); out = new TarArchiveOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; for (Map.Entry<String, String> entryFile : filePaths.entrySet()) { String filename = entryFile.getKey(); String filePath = entryFile.getValue(); System.out.println("Adding: " + filename + " => " + filePath); FileInputStream fi = new FileInputStream(basePath + filePath); origin = new BufferedInputStream(fi, BUFFER); TarArchiveEntry entry = new TarArchiveEntry(filename); out.putArchiveEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } } catch (Exception e) { e.printStackTrace(); } finally { if (origin != null) origin.close(); if (out != null) out.close(); if (dest != null) dest.close(); } }
From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java
public static void compressTarGzFile(final File temporaryTarGzFile, final Path pathToCompress, final BuildListener listener) throws IOException { try (final TarArchiveOutputStream tarGzArchiveOutputStream = new TarArchiveOutputStream( new BufferedOutputStream( new GzipCompressorOutputStream(new FileOutputStream(temporaryTarGzFile))))) { compressArchive(pathToCompress, tarGzArchiveOutputStream, new ArchiveEntryFactory(CompressionType.TarGz), CompressionType.TarGz, listener); }/*from w ww .j av a 2s. c o m*/ }
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 w w .j a va2 s. c om } tarArchive.finish(); tarArchive.close(); return new ByteArrayInputStream(os.toByteArray()); }
From source file:de.uzk.hki.da.pkg.TarGZArchiveBuilder.java
public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception { FileOutputStream fOut = null; BufferedOutputStream bOut = null; GzipCompressorOutputStream gzOut = null; TarArchiveOutputStream tOut = null;//from w ww.j av a 2s . co m try { fOut = new FileOutputStream(destFile); bOut = new BufferedOutputStream(fOut); gzOut = new GzipCompressorOutputStream(bOut); tOut = new TarArchiveOutputStream(gzOut); tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tOut.setBigNumberMode(2); if (includeFolder) addFileToTarGZ(tOut, srcFolder, ""); else { File children[] = srcFolder.listFiles(); for (int i = 0; i < children.length; i++) { addFileToTarGZ(tOut, children[i], ""); } } } finally { tOut.finish(); tOut.close(); gzOut.close(); bOut.close(); fOut.close(); } }
From source file:algorithm.TarPackaging.java
@Override public File encapsulate(File carrier, List<File> payloadFiles) throws IOException { String outputName = getOutputFileName(carrier); int dotIndex = outputName.lastIndexOf('.'); if (dotIndex > 0) { if (compression()) { if (gzip()) { outputName = outputName.substring(0, dotIndex) + ".tgz"; } else if (bzip()) { outputName = outputName.substring(0, dotIndex) + ".tbz2"; }//from w w w . j av a 2s . c om } else { outputName = outputName.substring(0, dotIndex) + ".tar"; } } File tarFile = new File(outputName); FileOutputStream outputStream = new FileOutputStream(tarFile); if (compression()) { if (gzip()) { TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(outputStream))); archiveFile(carrier, tarOutputStream); for (File payload : payloadFiles) { archiveFile(payload, tarOutputStream); } tarOutputStream.close(); } else if (bzip()) { TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( new BZip2CompressorOutputStream(new BufferedOutputStream(outputStream))); archiveFile(carrier, tarOutputStream); for (File payload : payloadFiles) { archiveFile(payload, tarOutputStream); } tarOutputStream.close(); } } else { TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( new BufferedOutputStream(outputStream)); archiveFile(carrier, tarOutputStream); for (File payload : payloadFiles) { archiveFile(payload, tarOutputStream); } tarOutputStream.close(); } outputStream.close(); return tarFile; }
From source file:adams.core.io.TarUtils.java
/** * Returns an output stream for the specified tar archive. Automatically * determines the compression used for the archive. Uses GNU long filename * support./* w ww . j a v a 2 s .c o m*/ * * @param input the tar archive to create the output stream for * @param stream the output stream to wrap * @return the output stream * @throws Exception if file not found or similar problems * @see TarArchiveOutputStream#LONGFILE_GNU */ protected static TarArchiveOutputStream openArchiveForWriting(File input, FileOutputStream stream) throws Exception { TarArchiveOutputStream result; Compression comp; comp = determineCompression(input); if (comp == Compression.GZIP) result = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(stream))); else if (comp == Compression.BZIP2) result = new TarArchiveOutputStream(new BZip2CompressorOutputStream(new BufferedOutputStream(stream))); else result = new TarArchiveOutputStream(new BufferedOutputStream(stream)); result.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); return result; }
From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java
/** * There is an option to override the name of the first level entry if you want to pack * a directory. Set includeFolder = true so that it not only packs the contents but also * the containing folder. Then use the setter setFirstLevelEntryName and set the name * of the folder which contains the files to pack. The name of the folder then gets replaced * in the resulting tar. Note that after calling archiveFolder once, the variable gets automatically * reset so that you have to call the setter again if you want to set the override setting again. *//*from w w w.ja va 2s .c o m*/ public void archiveFolder(File srcFolder, File destFile, boolean includeFolder) throws Exception { FileOutputStream fOut = null; BufferedOutputStream bOut = null; TarArchiveOutputStream tOut = null; fOut = new FileOutputStream(destFile); bOut = new BufferedOutputStream(fOut); tOut = new TarArchiveOutputStream(bOut); tOut.setLongFileMode(longFileMode); tOut.setBigNumberMode(bigNumberMode); try { String base = ""; if (firstLevelEntryName.isEmpty()) firstLevelEntryName = srcFolder.getName() + "/"; if (includeFolder) { logger.debug("addFileToTar: " + firstLevelEntryName); TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(srcFolder, firstLevelEntryName); tOut.putArchiveEntry(entry); tOut.closeArchiveEntry(); base = firstLevelEntryName; } File children[] = srcFolder.listFiles(); for (int i = 0; i < children.length; i++) { addFileToTar(tOut, children[i], base); } } finally { tOut.finish(); tOut.close(); bOut.close(); fOut.close(); firstLevelEntryName = ""; } }
From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java
public static void pack(File sourceFolder, OutputStream output, FileFilter filter, boolean includeTopFolder, String addedTopFolder) throws IOException { TarArchiveOutputStream tarOut = new TarArchiveOutputStream(output); tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); String absName = sourceFolder.getAbsolutePath(); int baseNameLen = absName.length() + 1; if (includeTopFolder) baseNameLen -= (sourceFolder.getName().length() + 1); try {/* w ww .ja v a 2 s .c o m*/ append(sourceFolder, filter, baseNameLen, addedTopFolder, tarOut); } finally { StreamUtil.close(tarOut); } }
From source file:com.moss.simpledeb.core.DebWriter.java
private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception { byte[] tarData; {// w w w.j a v a 2s . c om ByteArrayOutputStream tarOut = new ByteArrayOutputStream(); TarArchiveOutputStream tar = new TarArchiveOutputStream(tarOut); Set<String> writtenPaths = new HashSet<String>(); for (ArchivePath path : paths) { String name = path.entry().getName(); if (writtenPaths.contains(name)) { throw new RuntimeException("Duplicate archive entry: " + name); } writtenPaths.add(name); tar.putArchiveEntry(path.entry()); if (!path.entry().isDirectory()) { InputStream in = path.read(); byte[] buffer = new byte[1024 * 10]; for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) { tar.write(buffer, 0, numRead); } in.close(); } tar.closeArchiveEntry(); } tar.close(); tarData = tarOut.toByteArray(); } byte[] gzipData; { ByteArrayOutputStream gzipOut = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(gzipOut); gzip.write(tarData); gzip.close(); gzipData = gzipOut.toByteArray(); } return gzipData; }
From source file:jetbrains.exodus.util.CompressBackupUtil.java
/** * Compresses the content of source and stores newly created archive in dest. * In case source is a directory, it will be compressed recursively. * * @param source file or folder to be archived. Should exist on method call. * @param dest path to the archive to be created. Should not exist on method call. * @throws IOException in case of any issues with underlying store. * @throws FileNotFoundException in case source does not exist. *//* w ww.j a va 2 s .c om*/ public static void tar(@NotNull File source, @NotNull File dest) throws IOException { if (!source.exists()) { throw new IllegalArgumentException("No source file or folder exists: " + source.getAbsolutePath()); } if (dest.exists()) { throw new IllegalArgumentException( "Destination refers to existing file or folder: " + dest.getAbsolutePath()); } TarArchiveOutputStream tarOut = null; try { tarOut = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest)), 0x1000)); doTar("", source, tarOut); tarOut.close(); } catch (IOException e) { cleanUp(tarOut, dest); // operation filed, let's remove the destination archive throw e; } }