Search in sources :

Example 1 with TarArchiveEntry

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

the class TarGzCompressionUtils method unTarOneFile.

public static InputStream unTarOneFile(InputStream tarGzInputStream, final String filename) throws FileNotFoundException, IOException, ArchiveException {
    TarArchiveInputStream debInputStream = null;
    InputStream is = null;
    try {
        is = new GzipCompressorInputStream(tarGzInputStream);
        debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            if (entry.getName().contains(filename)) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                IOUtils.copy(debInputStream, byteArrayOutputStream);
                return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            }
        }
    } finally {
        IOUtils.closeQuietly(debInputStream);
        IOUtils.closeQuietly(is);
    }
    return null;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 2 with TarArchiveEntry

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

the class TarGzCompressionUtils method addFileToTarGz.

/**
   * Creates a tar entry for the path specified with a name built from the base
   * passed in and the file/directory name. If the path is a directory, a
   * recursive call is made such that the full directory is added to the tar.
   *
   * @param tOut
   *          The tar file's output stream
   * @param path
   *          The filesystem path of the file/directory being added
   * @param base
   *          The base prefix to for the name of the tar file entry
   *
   * @throws IOException
   *           If anything goes wrong
   */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tOut.putArchiveEntry(tarEntry);
    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 3 with TarArchiveEntry

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

the class TarGzCompressionUtils method unTar.

/** Untar an input file into an output file.

   * The output file is created in the output folder, having the same name
   * as the input file, minus the '.tar' extension.
   *
   * @param inputFile     the input .tar file
   * @param outputDir     the output directory file.
   * @throws IOException
   * @throws FileNotFoundException
   *
   * @return  The {@link List} of {@link File}s with the untared content.
   * @throws ArchiveException
   */
public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    LOGGER.debug(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
    TarArchiveInputStream debInputStream = null;
    InputStream is = null;
    final List<File> untaredFiles = new LinkedList<File>();
    try {
        is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
        debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                } else {
                    LOGGER.error("The directory already there. Deleting - " + outputFile.getAbsolutePath());
                    FileUtils.deleteDirectory(outputFile);
                }
            } else {
                LOGGER.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                File directory = outputFile.getParentFile();
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                OutputStream outputFileStream = null;
                try {
                    outputFileStream = new FileOutputStream(outputFile);
                    IOUtils.copy(debInputStream, outputFileStream);
                } finally {
                    IOUtils.closeQuietly(outputFileStream);
                }
            }
            untaredFiles.add(outputFile);
        }
    } finally {
        IOUtils.closeQuietly(debInputStream);
        IOUtils.closeQuietly(is);
    }
    return untaredFiles;
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 4 with TarArchiveEntry

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

the class TarUtils method addDirectoryEntry.

private static void addDirectoryEntry(TarArchiveOutputStream tarOut, String entryName, File directory, long modTime) throws IOException {
    final TarArchiveEntry tarEntry = new TarArchiveEntry(directory, entryName);
    if (modTime >= 0) {
        tarEntry.setModTime(modTime);
    }
    tarOut.putArchiveEntry(tarEntry);
    tarOut.closeArchiveEntry();
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 5 with TarArchiveEntry

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

the class TarUtils method untar.

public static void untar(InputStream in, File targetDir) throws IOException {
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    byte[] b = new byte[BUF_SIZE];
    TarArchiveEntry tarEntry;
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        final File file = new File(targetDir, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!file.mkdirs()) {
                throw new IOException("Unable to create folder " + file.getAbsolutePath());
            }
        } else {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent.getAbsolutePath());
                }
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = tarIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)210 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)101 File (java.io.File)89 FileInputStream (java.io.FileInputStream)58 IOException (java.io.IOException)56 FileOutputStream (java.io.FileOutputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)39 InputStream (java.io.InputStream)31 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)31 BufferedInputStream (java.io.BufferedInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Test (org.junit.Test)24 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 ArrayList (java.util.ArrayList)18 OutputStream (java.io.OutputStream)17 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)16 GZIPInputStream (java.util.zip.GZIPInputStream)12 HashMap (java.util.HashMap)11