use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project pinot by linkedin.
the class TarGzCompressionUtils method createTarGzOfDirectory.
public static String createTarGzOfDirectory(String directoryPath, String tarGzPath, String entryPrefix) throws IOException {
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
}
try {
fOut = new FileOutputStream(new File(tarGzPath));
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
addFileToTarGz(tOut, directoryPath, entryPrefix);
} catch (IOException e) {
LOGGER.error("Failed to create tar.gz file for {} at path: {}", directoryPath, tarGzPath, e);
Utils.rethrowException(e);
} finally {
if (tOut != null) {
tOut.finish();
tOut.close();
}
if (gzOut != null) {
gzOut.close();
}
if (bOut != null) {
bOut.close();
}
if (fOut != null) {
fOut.close();
}
}
return tarGzPath;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.
the class TarUtils method tarDir.
/**
* Add content of directory {@code dir} to tar archive {@code tar}.
*
* @param parentPath
* parent path of tar archive. Typically if need add only content of {@code dir} this path should be absolute path to {@code
* dir} but if need to have in archive some parents this parameter may be used. For example if need add to archive content of
* directory '/a/b/c' but need to save directory 'c' in path:
* <pre>
* {@code File dir = new File("a/b/c");
* File tar = new File("archive.tar");
* TarUtils.tarDir(dir.getParentFile().getAbsolutePath(), dir, tar, -1, IoUtil.ANY_FILTER);
* }
* </pre>
* In this case directory 'c' is added in tar archive.
* @param dir
* dir to add
* @param tar
* tar archive
* @param modTime
* modification time that applied to all entries in archive instead modification time provided by method {@link
* File#lastModified()}. This parameter should be {@code -1} if don't need to set any specified time
* @param filter
* optional filter for files to add in archive
* @throws IOException
* if i/o error occurs
* @throws IllegalArgumentException
* if {@code dir} is not directory or if {@code parentPath} is invalid, e.g. is neither parent nor equals to path of {@code
* dir}
*/
public static void tarDir(String parentPath, File dir, File tar, long modTime, FilenameFilter filter) throws IOException {
if (!dir.isDirectory()) {
throw new IllegalArgumentException("Not a directory.");
}
if (!dir.getAbsolutePath().startsWith(parentPath)) {
throw new IllegalArgumentException("Invalid parent directory path " + parentPath);
}
if (filter == null) {
filter = IoUtil.ANY_FILTER;
}
try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tar)))) {
tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
addDirectoryRecursively(tarOut, parentPath, dir, modTime, filter);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.
the class TarUtils method tarFiles.
public static void tarFiles(File tar, long modTime, File... files) throws IOException {
try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tar)))) {
tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
for (File f : files) {
if (f.isDirectory()) {
addDirectoryEntry(tarOut, f.getName(), f, modTime);
final String parentPath = f.getParentFile().getAbsolutePath();
addDirectoryRecursively(tarOut, parentPath, f, modTime, IoUtil.ANY_FILTER);
} else if (f.isFile()) {
addFileEntry(tarOut, f.getName(), f, modTime);
}
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.
the class TarArchiverTest method createTestTarArchive.
private byte[] createTestTarArchive() throws IOException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteOut);
addDirectoryEntry(tarOut, new TarArchiveEntry("arc/"));
addDirectoryEntry(tarOut, new TarArchiveEntry("arc/a/"));
addFileEntry(tarOut, "arc/a/_a.txt");
addDirectoryEntry(tarOut, new TarArchiveEntry("arc/b/"));
addFileEntry(tarOut, "arc/b/_b.txt");
addDirectoryEntry(tarOut, new TarArchiveEntry("arc/c/"));
addFileEntry(tarOut, "arc/c/_c.txt");
tarOut.close();
return byteOut.toByteArray();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project tdi-studio-se by Talend.
the class Zip method doTarGZip.
private void doTarGZip(Map<String, File> filesMap) throws Exception {
File targetFile = new File(targetZip);
targetFile.setLastModified(System.currentTimeMillis());
FileOutputStream fos = new FileOutputStream(targetFile);
final boolean syncFlush = this.syncFlush;
final int compressLevel = this.compressLevel;
TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(fos, syncFlush) {
{
this.def.setLevel(compressLevel);
}
});
taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
try {
for (String relativeDir : filesMap.keySet()) {
File file = filesMap.get(relativeDir);
taos.putArchiveEntry(new TarArchiveEntry(file, relativeDir));
FileInputStream is = new FileInputStream(file);
try {
IOUtils.copy(is, taos);
taos.closeArchiveEntry();
} finally {
is.close();
}
}
} finally {
taos.close();
fos.close();
}
}
Aggregations