Search in sources :

Example 11 with TarArchiveOutputStream

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 12 with TarArchiveOutputStream

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");
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) Map(java.util.Map)

Example 13 with TarArchiveOutputStream

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 14 with TarArchiveOutputStream

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);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 15 with TarArchiveOutputStream

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);
                        }
                    }
                }
            }
        });
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)22 FileOutputStream (java.io.FileOutputStream)13 File (java.io.File)12 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)12 FileInputStream (java.io.FileInputStream)7 IOException (java.io.IOException)7 BufferedOutputStream (java.io.BufferedOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)4 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)4 OutputStream (java.io.OutputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)3 InputStream (java.io.InputStream)2 Map (java.util.Map)2 WrappedFile (org.apache.camel.WrappedFile)2 GenericFile (org.apache.camel.component.file.GenericFile)2 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)2 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)2 CompressorStreamFactory (org.apache.commons.compress.compressors.CompressorStreamFactory)2