use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project jib by google.
the class TarStreamBuilder method writeEntriesAsTarArchive.
/**
* Writes each entry in the filesystem to the tarball archive stream.
*/
private static void writeEntriesAsTarArchive(List<TarArchiveEntry> entries, OutputStream tarByteStream) throws IOException {
try (TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(tarByteStream)) {
// Enables PAX extended headers to support long file names.
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
for (TarArchiveEntry entry : entries) {
tarArchiveOutputStream.putArchiveEntry(entry);
if (entry.isFile()) {
InputStream contentStream = new BufferedInputStream(new FileInputStream(entry.getFile()));
ByteStreams.copy(contentStream, tarArchiveOutputStream);
}
tarArchiveOutputStream.closeArchiveEntry();
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project ovirt-engine by oVirt.
the class Tar method doTar.
/**
* Crete tar.
* @param os output stream to write into.
* @param base base directory.
*
* Only regular files and directories are supported.
* Files will be owner rw and optional execute bit.
*/
public static void doTar(OutputStream os, File base) throws SecurityException, IOException {
if (!base.exists()) {
throw new FileNotFoundException(String.format("File or directory %1$s not found", base));
}
try (TarArchiveOutputStream archive = new TarArchiveOutputStream(os)) {
// TODO: use LONGFILE_POSIX in newer version of commons-compress
archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
recurse(archive, base, "./");
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project vespa by vespa-engine.
the class CompressedApplicationInputStreamTest method require_that_valid_tar_application_in_subdir_can_be_unpacked.
@Test
public void require_that_valid_tar_application_in_subdir_can_be_unpacked() throws IOException {
File outFile = File.createTempFile("testapp", ".tar.gz");
ArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(outFile)));
File app = new File("src/test/resources/deploy/validapp");
File file = new File(app, "services.xml");
archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
file = new File(app, "hosts.xml");
archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
file = new File(app, "deployment.xml");
archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
archiveOutputStream.close();
CompressedApplicationInputStream unpacked = CompressedApplicationInputStream.createFromCompressedStream(new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(outFile))));
File outApp = unpacked.decompress();
// gets the name of the subdir
assertThat(outApp.getName(), is("application"));
assertTestApp(outApp);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project xodus by JetBrains.
the class CompressBackupUtil method tar.
/**
* 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.
*/
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) {
// operation filed, let's remove the destination archive
cleanUp(tarOut, dest);
throw e;
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project elastest-torm by elastest.
the class FilesService method createTarFile.
public File createTarFile(String path, File... files) throws IOException {
logger.info("Directory path to cormpress: {}", path);
logger.info("Init tar compression.");
try (TarArchiveOutputStream out = getTarArchiveOutputStream(path)) {
for (File file : files) {
addToArchiveCompression(out, file, ".");
}
}
logger.info("Path of the tar file: {}", files[0].getAbsolutePath() + ".tar");
return new File(files[0].getAbsolutePath() + ".tar");
}
Aggregations