Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream.

Prototype

public TarArchiveInputStream(InputStream is) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:ezbake.deployer.publishers.ezcentos.EzCentosThriftRunnerPublisher.java

/**
 * Copies files from the artifact .tar file
 * that end in the suffix to the specified destination.
 *
 * @param artifact/*from  w  w w .ja v a 2 s.  c  om*/
 * @param destination
 * @param suffix
 * @return Paths that the files were copied to
 * @throws IOException
 */
private List<String> copyFiles(DeploymentArtifact artifact, File destination, String suffix)
        throws IOException {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact())));
    List<String> filePaths = new ArrayList<>();
    try {
        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            if (entry.getName().endsWith(suffix)) {
                String newFilePath = destination + File.separator + entry.getName();
                FileOutputStream fos = new FileOutputStream(newFilePath);
                try {
                    IOUtils.copy(tarIn, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
                filePaths.add(newFilePath);
            }
            entry = tarIn.getNextTarEntry();
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }

    return filePaths;
}

From source file:es.ucm.fdi.util.archive.TarFormat.java

public boolean extractOne(File source, String path, File dest) throws IOException {

    try (InputStream is = getTarInputStream(source);
            TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        byte[] b = new byte[512];
        for (TarArchiveEntry e; (e = tis.getNextTarEntry()) != null; /**/) {

            // backslash-protection: zip format expects only 'fw' slashes
            //System.err.println("Found entry: "+e.getName());
            String name = FileUtils.toCanonicalPath(e.getName());

            if (!name.equals(path) || e.isDirectory()) {
                continue;
            }/*from   w  w  w  . ja  v a2s.  c om*/

            //System.err.println("\tExtracting file "+name);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(dest)) {
                int len;
                while ((len = tis.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                return true;
            }
        }
    }
    return false;
}

From source file:co.cask.tigon.sql.internal.StreamBinaryGenerator.java

private void unzipFile(File libZip) throws IOException, ArchiveException {
    String path = libZip.toURI().getPath();
    String outDir = libZip.getParentFile().getPath();
    TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(
            new GZIPInputStream(new FileInputStream(path)));
    try {//from  w  ww . j  a  va  2  s  .c  om
        TarArchiveEntry entry = archiveInputStream.getNextTarEntry();
        while (entry != null) {
            File destFile = new File(outDir, entry.getName());
            destFile.getParentFile().mkdirs();
            if (!entry.isDirectory()) {
                ByteStreams.copy(archiveInputStream, Files.newOutputStreamSupplier(destFile));
                //TODO: Set executable permission based on entry.getMode()
                destFile.setExecutable(true, false);
            }
            entry = archiveInputStream.getNextTarEntry();
        }
    } finally {
        archiveInputStream.close();
    }
}

From source file:net.codestory.simplelenium.driver.Downloader.java

protected void untar(File tar, File toDir) throws IOException {
    try (FileInputStream fin = new FileInputStream(tar);
            BufferedInputStream bin = new BufferedInputStream(fin);
            TarArchiveInputStream tarInput = new TarArchiveInputStream(bin)) {
        ArchiveEntry entry;//from   w w  w . ja  v  a 2s .c  om
        while (null != (entry = tarInput.getNextTarEntry())) {
            if (entry.isDirectory()) {
                continue;
            }

            File to = new File(toDir, entry.getName());

            File parent = to.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent);
                }
            }

            Files.copy(tarInput, to.toPath(), REPLACE_EXISTING);
        }
    }
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void decompressFromStream2(InputStream inputStream) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(inputStream);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream);
    TarArchiveEntry entry;/*from  www. ja  v  a 2s.  com*/
    int bufferSize = 1024;
    while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
        String entryName = entry.getName();
        // strip out the leading directory like the --strip tar argument
        String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1);
        if (entryNameWithoutLeadingDir.isEmpty()) {
            continue;
        }
        if (entry.isDirectory()) {
            System.out.println("found dir " + entry.getName());
        } else {
            int count;
            byte data[] = new byte[bufferSize];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((count = tarInput.read(data, 0, bufferSize)) != -1) {
                baos.write(data, 0, count);
            }
            JarOutputStream jarOutputStream = new JarOutputStream(baos);
            System.out.println(new String(baos.toByteArray()));
        }
    }
    tarInput.close();
    gzipStream.close();
}

From source file:ezbake.deployer.utilities.ArtifactHelpers.java

public static void addFilesToArtifact(DeploymentArtifact artifact, Iterable<ArtifactDataEntry> injectFiles)
        throws DeploymentException {
    CompressorInputStream uncompressedInput = null;
    ArchiveInputStream input = null;//  w  w  w .ja v a2 s . com
    ByteArrayOutputStream bos = null;
    try {
        uncompressedInput = new GzipCompressorInputStream(new ByteArrayInputStream(artifact.getArtifact()));
        input = new TarArchiveInputStream(uncompressedInput);
        bos = new ByteArrayOutputStream(artifact.getArtifact().length);
        appendFilesInTarArchive(input, bos, injectFiles);
        artifact.setArtifact(bos.toByteArray());
    } catch (IOException ex) {
        log.error("IOException while attempting to add files to a deployment artifact.", ex);
        throw new DeploymentException(ex.getMessage());
    } finally {
        IOUtils.closeQuietly(uncompressedInput);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(bos);
    }
}

From source file:com.kalix.tools.kibana.KibanaController.java

/**
 * download kibana from remote server/* ww  w  .ja va  2s.  c o  m*/
 *
 * @throws Exception
 */
public void download() throws Exception {
    File target = new File(workingDirectory, KIBANA_FOLDER);
    if (target.exists()) {
        LOGGER.warn("Kibana folder already exists, download is skipped");
        return;
    }
    LOGGER.debug("Downloading Kibana from {}", KIBANA_LOCATION);
    if (isWindows()) {
        try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            ZipArchiveEntry entry;
            while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) {
                File file = new File(workingDirectory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    int read;
                    byte[] buffer = new byte[4096];
                    try (FileOutputStream outputStream = new FileOutputStream(file)) {
                        while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                            outputStream.write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    } else {
        try (GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            try (TarArchiveInputStream inputStream = new TarArchiveInputStream(gzInputStream)) {
                TarArchiveEntry entry;
                while ((entry = (TarArchiveEntry) inputStream.getNextEntry()) != null) {
                    File file = new File(workingDirectory, entry.getName());
                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        int read;
                        byte[] buffer = new byte[4096];
                        try (FileOutputStream outputStream = new FileOutputStream(file)) {
                            while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                                outputStream.write(buffer, 0, read);
                            }
                        }
                        file.setLastModified(entry.getLastModifiedDate().getTime());
                        if (entry instanceof TarArchiveEntry) {
                            int mode = ((TarArchiveEntry) entry).getMode();
                            if ((mode & 00100) > 0) {
                                file.setExecutable(true, (mode & 00001) == 0);
                            }
                        }
                    }
                }
            }
        }
    }
    overrideConfig();
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

/**
 * Uncompress the content of a tarball file to the specified directory.
 * @param srcFile - the tarball to be uncompressed
 * @param outDir - directory where the files (and directories) extracted from the tarball will be written
 * @return the list of files (and directories) extracted from the tarball
 * @throws IOException when an exception occurs on uncompressing the tarball
 *///  www  .ja va  2s  .  c  o m
public static List<String> unGzipUnTar(final String srcFile, final String outDir) throws IOException {
    final List<String> uncompressedFiles = newArrayList();
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    GzipCompressorInputStream gzipCompressorInputStream = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    BufferedInputStream bufferedInputStream2 = null;

    try {
        forceMkdir(new File(outDir));
        fileInputStream = new FileInputStream(srcFile);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        gzipCompressorInputStream = new GzipCompressorInputStream(fileInputStream);
        tarArchiveInputStream = new TarArchiveInputStream(gzipCompressorInputStream);

        TarArchiveEntry entry = null;
        while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
            final File outputFile = new File(outDir, entry.getName());
            if (entry.isDirectory()) {
                // attempting to write output directory
                if (!outputFile.exists()) {
                    // attempting to create output directory
                    if (!outputFile.mkdirs()) {
                        throw new IOException("Cannot create directory: " + outputFile.getCanonicalPath());
                    }
                }
            } else {
                // attempting to create output file
                final byte[] content = new byte[(int) entry.getSize()];
                tarArchiveInputStream.read(content, 0, content.length);
                final FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                write(content, outputFileStream);
                outputFileStream.close();
                uncompressedFiles.add(outputFile.getCanonicalPath());
            }
        }
    } finally {
        if (bufferedInputStream2 != null) {
            bufferedInputStream2.close();
        }
        if (tarArchiveInputStream != null) {
            tarArchiveInputStream.close();
        }
        if (gzipCompressorInputStream != null) {
            gzipCompressorInputStream.close();
        }
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
    return uncompressedFiles;
}

From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java

/**
 * Unpacks the contents of the given archive into the given folder
 * //from   w  ww  .  ja v a  2s.co m
 * @param srcFile The archive container file
 * @param destFolder The destination folder
 * @param uncompress Indicates if the archive file is compressed (tgz file) or not (tar file)
 * @throws Exception
 */
public void unarchiveFolder(File srcFile, File destFolder, boolean uncompress) throws Exception {

    FileInputStream fIn = new FileInputStream(srcFile);
    BufferedInputStream in = new BufferedInputStream(fIn);

    TarArchiveInputStream tIn = null;
    GzipCompressorInputStream gzIn = null;

    if (uncompress) {
        gzIn = new GzipCompressorInputStream(in);
        tIn = new TarArchiveInputStream(gzIn);
    } else
        tIn = new TarArchiveInputStream(fIn);

    TarArchiveEntry entry;
    do {
        entry = tIn.getNextTarEntry();

        if (entry == null)
            break;

        File entryFile = new File(destFolder.getAbsolutePath() + "/" + entry.getName());

        if (entry.isDirectory())
            entryFile.mkdirs();
        else {
            new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/')))
                    .mkdirs();

            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(tIn, out);
            out.close();
        }
    } while (true);

    tIn.close();
    if (gzIn != null)
        gzIn.close();
    in.close();
    fIn.close();
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void extractTar(Resource tarFile, Resource targetDir) throws IOException {
    if (!targetDir.exists() || !targetDir.isDirectory())
        throw new IOException(targetDir + " is not a existing directory");

    if (!tarFile.exists())
        throw new IOException(tarFile + " is not a existing file");

    if (tarFile.isDirectory()) {
        Resource[] files = tarFile.listResources(new ExtensionResourceFilter("tar"));
        if (files == null)
            throw new IOException("directory " + tarFile + " is empty");
        extract(FORMAT_TAR, files, targetDir);
        return;/* w w  w.j a  va  2  s. c  o m*/
    }

    //      read the zip file and build a query from its contents
    TarArchiveInputStream tis = null;
    try {
        tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream()));
        TarArchiveEntry entry;
        int mode;
        while ((entry = tis.getNextTarEntry()) != null) {
            //print.ln(entry);
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                IOUtil.copy(tis, target, false);
            }
            target.setLastModified(entry.getModTime().getTime());
            mode = entry.getMode();
            if (mode > 0)
                target.setMode(mode);
            //tis.closeEntry() ;
        }
    } finally {
        IOUtil.closeEL(tis);
    }
}