use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project MSEC by Tencent.
the class TarUtil method archive.
public static void archive(File srcFile, File destFile) throws Exception {
TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(destFile));
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
archive(srcFile, taos, "");
taos.flush();
taos.close();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project tomee by apache.
the class BuildTomEEMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
super.execute();
if (formats == null) {
formats = Collections.emptyMap();
}
String prefix = catalinaBase.getParentFile().getAbsolutePath();
if (!prefix.endsWith(File.separator)) {
prefix += File.separator;
}
if (skipArchiveRootFolder) {
prefix += catalinaBase.getName() + File.separator;
}
if (zip || formats.containsKey("zip")) {
getLog().info("Zipping Custom TomEE Distribution");
final String zip = formats.get("zip");
final File output = zip != null ? new File(zip) : zipFile;
try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(output))) {
for (final String entry : catalinaBase.list()) {
zip(zos, new File(catalinaBase, entry), prefix);
}
} catch (final IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
attach("zip", output);
}
if (formats != null) {
//handled previously for compatibility
formats.remove("zip");
for (final Map.Entry<String, String> format : formats.entrySet()) {
final String key = format.getKey();
getLog().info(key + "-ing Custom TomEE Distribution");
if ("tar.gz".equals(key)) {
final String out = format.getValue();
final File output = out != null ? new File(out) : new File(base.getParentFile(), base.getName() + "." + key);
Files.mkdirs(output.getParentFile());
try (final TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(output)))) {
tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
for (final String entry : catalinaBase.list()) {
tarGz(tarGz, new File(catalinaBase, entry), prefix);
}
} catch (final IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
attach(key, output);
} else {
throw new MojoExecutionException(key + " format not supported");
}
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project camel by apache.
the class TarFileDataFormat method marshal.
@Override
public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
String filename = exchange.getIn().getHeader(FILE_NAME, String.class);
Long filelength = exchange.getIn().getHeader(FILE_LENGTH, Long.class);
if (filename == null) {
// generate the file name as the camel file component would do
filename = StringHelper.sanitize(exchange.getIn().getMessageId());
} else {
// remove any path elements
filename = Paths.get(filename).getFileName().toString();
}
TarArchiveOutputStream tos = new TarArchiveOutputStream(stream);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);
if (filelength == null) {
filelength = (long) is.available();
}
TarArchiveEntry entry = new TarArchiveEntry(filename);
entry.setSize(filelength);
tos.putArchiveEntry(entry);
try {
IOHelper.copy(is, tos);
} finally {
tos.closeArchiveEntry();
IOHelper.close(is, tos);
}
String newFilename = filename + ".tar";
exchange.getOut().setHeader(FILE_NAME, newFilename);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project camel by apache.
the class TarAggregationStrategy method addEntryToTar.
private void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException {
File tmpTar = File.createTempFile(source.getName(), null, parentDir);
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Cannot create 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);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = tin.getNextEntry()) != null) {
tos.putArchiveEntry(nextEntry);
IOUtils.copy(tin, tos);
tos.closeArchiveEntry();
}
// Create new entry
TarArchiveEntry entry = new TarArchiveEntry(entryName);
entry.setSize(length);
tos.putArchiveEntry(entry);
tos.write(buffer, 0, length);
tos.closeArchiveEntry();
IOHelper.close(fis, tin, tos);
LOG.trace("Deleting temporary file: {}", tmpTar);
FileUtil.deleteFile(tmpTar);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.
the class TarArchiver method compress.
@Override
public void compress(OutputStream tarOutput, VirtualFileFilter filter) throws IOException, ServerException {
try (TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(tarOutput)) {
tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
folder.accept(new VirtualFileVisitor() {
@Override
public void visit(VirtualFile visitedVirtualFile) throws ServerException {
if (filter.accept(visitedVirtualFile)) {
if (!visitedVirtualFile.equals(folder)) {
addTarEntry(visitedVirtualFile, tarOutputStream);
}
if (visitedVirtualFile.isFolder()) {
for (VirtualFile child : visitedVirtualFile.getChildren()) {
child.accept(this);
}
}
}
}
});
}
}
Aggregations