Search in sources :

Example 76 with TarArchiveEntry

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

the class TarUtils method decompress.

public static void decompress(String in, File out) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(in);
    GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(fileInputStream);
    try (TarArchiveInputStream fin = new TarArchiveInputStream(gzipInputStream)) {
        TarArchiveEntry entry;
        while ((entry = fin.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            File curfile = new File(out, entry.getName());
            File parent = curfile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            IOUtils.copy(fin, new FileOutputStream(curfile));
        }
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 77 with TarArchiveEntry

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

the class CompressionUtils 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 inputFileName the input .tar file
 * @param outputDirName 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 String inputFileName, final String outputDirName, boolean flatten) throws FileNotFoundException, IOException, ArchiveException {
    File inputFile = new File(inputFileName);
    File outputDir = new File(outputDirName);
    final List<File> untaredFiles = new LinkedList<File>();
    InputStream is = null;
    try {
        if (inputFileName.endsWith(".gz")) {
            is = new GzipCompressorInputStream(new FileInputStream(inputFile));
        } else {
            is = new FileInputStream(inputFile);
        }
        final TarArchiveInputStream 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 (!outputFile.toPath().toAbsolutePath().normalize().startsWith(outputDir.toPath().toAbsolutePath().normalize())) {
                throw new IOException("Untarred file is not under the output directory");
            }
            if (entry.isDirectory()) {
                if (flatten) {
                    // no sub-directories
                    continue;
                }
                LOG.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOG.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 {
                final OutputStream outputFileStream;
                if (flatten) {
                    File flatOutputFile = new File(outputDir, outputFile.getName());
                    LOG.debug(String.format("Creating flat output file %s.", flatOutputFile.getAbsolutePath()));
                    outputFileStream = new FileOutputStream(flatOutputFile);
                } else if (!outputFile.getParentFile().exists()) {
                    LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getParentFile().getAbsoluteFile()));
                    if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getParentFile().getAbsolutePath()));
                    }
                    LOG.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                    outputFileStream = new FileOutputStream(outputFile);
                } else {
                    outputFileStream = new FileOutputStream(outputFile);
                }
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();
        return untaredFiles;
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) IOException(java.io.IOException) 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) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 78 with TarArchiveEntry

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

the class CompressionUtils method tar.

/**
 * Archive all the files in the inputFiles into outputFile
 *
 * @param inputFiles
 * @param outputFile
 * @throws IOException
 */
public static void tar(String parentDir, String[] inputFiles, String outputFile) throws IOException {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(new File(parentDir, outputFile));
        TarArchiveOutputStream tOut = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(out)));
        for (int i = 0; i < inputFiles.length; i++) {
            File f = new File(parentDir, inputFiles[i]);
            TarArchiveEntry tarEntry = new TarArchiveEntry(f, f.getName());
            tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            tOut.putArchiveEntry(tarEntry);
            FileInputStream input = new FileInputStream(f);
            try {
                // copy with 8K buffer, not close
                IOUtils.copy(input, tOut);
            } finally {
                input.close();
            }
            tOut.closeArchiveEntry();
        }
        // finishes inside
        tOut.close();
    } finally {
        // TarArchiveOutputStream seemed not to close files properly in error situation
        org.apache.hadoop.io.IOUtils.closeStream(out);
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 79 with TarArchiveEntry

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

the class ArchiveUtils method extractTarArchive.

/**
 * Extracts a TAR archive. If the current platform supports POSIX permissions, the archive entry permissions are applied to the create file or directory.
 * Symbolic and "hard" links are also support.
 *
 * @param inputStream        the archive input stream
 * @param extractToDirectory the directory to extract the archive into
 * @throws IOException if an IO error occurs
 */
private static void extractTarArchive(InputStream inputStream, Path extractToDirectory) throws IOException {
    final TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(inputStream);
    TarArchiveEntry entry;
    while ((entry = archiveInputStream.getNextTarEntry()) != null) {
        if (!archiveInputStream.canReadEntryData(entry)) {
            LOG.error("Failed to read archive entry " + entry);
            continue;
        }
        Path entryExtractPath = extractToDirectory.resolve(getEntryAsPath(entry));
        if (entry.isLink()) {
            Path linkTarget = Paths.get(entry.getLinkName());
            Files.deleteIfExists(entryExtractPath);
            Files.createLink(entryExtractPath, linkTarget);
        } else if (entry.isSymbolicLink()) {
            Path linkTarget = Paths.get(entry.getLinkName());
            Files.deleteIfExists(entryExtractPath);
            Files.createSymbolicLink(entryExtractPath, linkTarget);
        } else {
            if (entry.isDirectory()) {
                Files.createDirectories(entryExtractPath);
            } else {
                Files.createDirectories(entryExtractPath.getParent());
                Files.copy(archiveInputStream, entryExtractPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
        setLastModifiedTime(entryExtractPath, FileTime.fromMillis(entry.getLastModifiedDate().getTime()));
        Set<PosixFilePermission> permissions = getPosixFilePermissions(entry);
        setPosixPermissions(entryExtractPath, permissions);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 80 with TarArchiveEntry

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

the class BinlogDownloadQueue method saveFile.

private static void saveFile(File parentFile, String fileName, HttpResponse response) throws IOException {
    InputStream is = response.getEntity().getContent();
    long totalSize = Long.parseLong(response.getFirstHeader("Content-Length").getValue());
    if (response.getFirstHeader("Content-Disposition") != null) {
        fileName = response.getFirstHeader("Content-Disposition").getValue();
        fileName = StringUtils.substringAfter(fileName, "filename=");
    }
    boolean isTar = StringUtils.endsWith(fileName, ".tar");
    FileUtils.forceMkdir(parentFile);
    FileOutputStream fos = null;
    try {
        if (isTar) {
            TarArchiveInputStream tais = new TarArchiveInputStream(is);
            TarArchiveEntry tarArchiveEntry = null;
            while ((tarArchiveEntry = tais.getNextTarEntry()) != null) {
                String name = tarArchiveEntry.getName();
                File tarFile = new File(parentFile, name + ".tmp");
                logger.info("start to download file " + tarFile.getName());
                if (tarFile.exists()) {
                    tarFile.delete();
                }
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(new FileOutputStream(tarFile));
                    int read = -1;
                    byte[] buffer = new byte[1024];
                    while ((read = tais.read(buffer)) != -1) {
                        bos.write(buffer, 0, read);
                    }
                    logger.info("download file " + tarFile.getName() + " end!");
                    tarFile.renameTo(new File(parentFile, name));
                } finally {
                    IOUtils.closeQuietly(bos);
                }
            }
            tais.close();
        } else {
            File file = new File(parentFile, fileName + ".tmp");
            if (file.exists()) {
                file.delete();
            }
            if (!file.isFile()) {
                file.createNewFile();
            }
            try {
                fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len;
                long copySize = 0;
                long nextPrintProgress = 0;
                logger.info("start to download file " + file.getName());
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                    copySize += len;
                    long progress = copySize * 100 / totalSize;
                    if (progress >= nextPrintProgress) {
                        logger.info("download " + file.getName() + " progress : " + progress + "% , download size : " + copySize + ", total size : " + totalSize);
                        nextPrintProgress += 10;
                    }
                }
                logger.info("download file " + file.getName() + " end!");
                fos.flush();
            } finally {
                IOUtils.closeQuietly(fos);
            }
            file.renameTo(new File(parentFile, fileName));
        }
    } finally {
        IOUtils.closeQuietly(fos);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)213 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)102 File (java.io.File)91 FileInputStream (java.io.FileInputStream)59 IOException (java.io.IOException)59 FileOutputStream (java.io.FileOutputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)40 InputStream (java.io.InputStream)32 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)32 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Test (org.junit.Test)24 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 OutputStream (java.io.OutputStream)18 ArrayList (java.util.ArrayList)18 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)16 HashMap (java.util.HashMap)12 GZIPInputStream (java.util.zip.GZIPInputStream)12