Search in sources :

Example 51 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project POL-POM-5 by PlayOnLinux.

the class Tar method uncompress.

/**
     * Uncompress a tar
     *
     * @param countingInputStream
     *            to count the number of byte extracted
     * @param outputDir
     *            The directory where files should be extracted
     * @return A list of extracted files
     * @throws ArchiveException
     *             if the process fails
     */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream)) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(), entry.getMode()));
                if (entry.isSymbolicLink()) {
                    Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()), Paths.get(entry.getLinkName()));
                } else {
                    try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                        IOUtils.copy(debInputStream, outputFileStream);
                        Files.setPosixFilePermissions(Paths.get(outputFile.getPath()), fileUtilities.octToPosixFilePermission(entry.getMode()));
                    }
                }
            }
            uncompressedFiles.add(outputFile);
            stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}
Also used : LinkedList(java.util.LinkedList) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream)

Example 52 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project tomee by apache.

the class BuildTomEEMojo method tarGz.

private void tarGz(final TarArchiveOutputStream tarGz, final File f, final String prefix) throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final TarArchiveEntry archiveEntry = new TarArchiveEntry(f, path);
    if (isSh(path)) {
        archiveEntry.setMode(0755);
    }
    tarGz.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        tarGz.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                tarGz(tarGz, child, prefix);
            }
        }
    } else {
        IO.copy(f, tarGz);
        tarGz.closeArchiveEntry();
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 53 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project tika by apache.

the class TarWriter method tarStoreBuffer.

private static void tarStoreBuffer(TarArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(dataBuffer.length);
    zip.putArchiveEntry(entry);
    zip.write(dataBuffer);
    zip.closeArchiveEntry();
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 54 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project cdap by caskdata.

the class LocalizationUtils method extractTar.

private static void extractTar(final TarArchiveInputStream tis, File targetDir) throws IOException {
    TarArchiveEntry entry = tis.getNextTarEntry();
    while (entry != null) {
        File output = new File(targetDir, new File(entry.getName()).getName());
        if (entry.isDirectory()) {
            //noinspection ResultOfMethodCallIgnored
            output.mkdirs();
        } else {
            //noinspection ResultOfMethodCallIgnored
            output.getParentFile().mkdirs();
            ByteStreams.copy(tis, Files.newOutputStreamSupplier(output));
        }
        entry = tis.getNextTarEntry();
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 55 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project cdap by caskdata.

the class LocalizationUtilsTest method addFilesToTar.

private void addFilesToTar(TarArchiveOutputStream tos, File... filesToAdd) throws IOException {
    for (File file : filesToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tos.putArchiveEntry(tarEntry);
        if (file.isFile()) {
            com.google.common.io.Files.copy(file, tos);
        }
        tos.closeArchiveEntry();
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)55 File (java.io.File)29 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)27 FileInputStream (java.io.FileInputStream)24 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)17 InputStream (java.io.InputStream)14 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)14 BufferedInputStream (java.io.BufferedInputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 BufferedOutputStream (java.io.BufferedOutputStream)8 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 OutputStream (java.io.OutputStream)6 GZIPInputStream (java.util.zip.GZIPInputStream)5 ZipFile (java.util.zip.ZipFile)4 Test (org.junit.Test)4 LinkedList (java.util.LinkedList)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3