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 camel by apache.
the class TarAggregationStrategy method addFileToTar.
private void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException {
File tmpTar = File.createTempFile(source.getName(), null, parentDir);
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Could not make temp file (" + source.getName() + ")");
}
FileInputStream fis = new FileInputStream(tmpTar);
TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
InputStream in = new FileInputStream(file);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = tin.getNextEntry()) != null) {
tos.putArchiveEntry(nextEntry);
IOUtils.copy(tin, tos);
tos.closeArchiveEntry();
}
// Add the new entry
TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName);
entry.setSize(file.length());
tos.putArchiveEntry(entry);
IOUtils.copy(in, tos);
tos.closeArchiveEntry();
IOHelper.close(fis, in, tin, tos);
LOG.trace("Deleting temporary file: {}", tmpTar);
FileUtil.deleteFile(tmpTar);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project camel by apache.
the class TarUtils method getTaredText.
static byte[] getTaredText(String entryName) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tos = new TarArchiveOutputStream(baos);
try {
TarArchiveEntry entry = new TarArchiveEntry(entryName);
entry.setSize(bais.available());
tos.putArchiveEntry(entry);
IOHelper.copy(bais, tos);
} finally {
tos.closeArchiveEntry();
IOHelper.close(bais, tos);
}
return baos.toByteArray();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project gitblit by gitblit.
the class CompressionUtils method tar.
/**
* Compresses/archives the contents of the tree at the (optionally)
* specified revision and the (optionally) specified basepath to the
* supplied outputstream.
*
* @param algorithm
* compression algorithm for tar (optional)
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output
* stream
*/
private static boolean tar(String algorithm, Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
OutputStream cos = os;
if (!StringUtils.isEmpty(algorithm)) {
try {
cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os);
} catch (CompressorException e1) {
error(e1, repository, "{0} failed to open {1} stream", algorithm);
}
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
tos.setAddPaxHeadersForNonAsciiNames(true);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ObjectLoader loader = repository.open(id);
if (FileMode.SYMLINK == mode) {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
loader.copyTo(bos);
entry.setLinkName(bos.toString());
entry.setModTime(modified);
tos.putArchiveEntry(entry);
tos.closeArchiveEntry();
} else {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
entry.setMode(mode.getBits());
entry.setModTime(modified);
FilestoreModel filestoreItem = null;
if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) {
filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id));
}
final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize();
entry.setSize(size);
tos.putArchiveEntry(entry);
if (filestoreItem == null) {
//Copy repository stored file
loader.copyTo(tos);
} else {
//Copy filestore file
try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
IOUtils.copyLarge(streamIn, tos);
} catch (Throwable e) {
LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
//Handle as per other errors
throw e;
}
}
tos.closeArchiveEntry();
}
}
tos.finish();
tos.close();
cos.close();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
} finally {
tw.close();
rw.dispose();
}
return success;
}
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