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:com.nike.cerberus.operation.dashboard.PublishDashboardOperation.java

private File extractArtifact(final URL artifactUrl) {
    final File extractionDirectory = Files.createTempDir();
    logger.debug("Extracting artifact contents to {}", extractionDirectory.getAbsolutePath());

    ArchiveEntry entry;/*from  www  .ja  va  2 s.c  om*/
    TarArchiveInputStream tarArchiveInputStream = null;
    try {
        tarArchiveInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(artifactUrl.openStream()));
        entry = tarArchiveInputStream.getNextEntry();
        while (entry != null) {
            String entryName = entry.getName();
            if (entry.getName().startsWith("./")) {
                entryName = entry.getName().substring(1);
            }
            final File destPath = new File(extractionDirectory, entryName);
            if (!entry.isDirectory()) {
                final File fileParentDir = new File(destPath.getParent());
                if (!fileParentDir.exists()) {
                    FileUtils.forceMkdir(fileParentDir);
                }
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream(destPath);
                    IOUtils.copy(tarArchiveInputStream, fileOutputStream);
                } finally {
                    IOUtils.closeQuietly(fileOutputStream);
                }
            }
            entry = tarArchiveInputStream.getNextEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(tarArchiveInputStream);
    }

    return extractionDirectory;
}

From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) {
    try {//from  w ww . j  a  va 2s  .  c  o  m
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        // read Tar entries
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (logging) {
                LOG.info("Extracting: " + outDir + File.separator + entry.getName());
            }
            if (entry.isDirectory()) { // create directory
                File f = new File(outDir + File.separator + entry.getName());
                f.mkdirs();
            } else { // decompress file
                int count;
                byte data[] = new byte[EXTRACT_BUFFER_SIZE];

                FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE);
                while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        // close input stream
        tarIn.close();

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    }
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

private static void untar(File tarFile, File targetDir) throws IOException {
    try (TarArchiveInputStream tis = new TarArchiveInputStream(new FileInputStream(tarFile))) {
        extractTar(tis, targetDir);// w  w  w. j  av  a2 s  .  co  m
    }
}

From source file:gobblin.util.io.StreamUtilsTest.java

@Test
public void testTarFile() throws IOException {
    FileSystem localFs = FileSystem.getLocal(new Configuration());

    // Set of expected Paths to be in the resulting tar file
    Set<Path> expectedPaths = Sets.newHashSet();

    // Create input file path
    Path testFile = new Path("testFile");
    expectedPaths.add(testFile);/*from  w  ww  .j  a  va2  s.c om*/

    // Create output file path
    Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");

    try {
        // Create the input file
        FSDataOutputStream testFileOut1 = localFs.create(testFile);
        testFileOut1.close();

        // tar the input file to the specific output file
        StreamUtils.tar(localFs, testFile, testOutFile);

        // Confirm the contents of the tar file are valid
        try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(localFs.open(testOutFile)))) {
            TarArchiveEntry tarArchiveEntry;

            while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
                MatcherAssert.assertThat(new Path(tarArchiveEntry.getName()), Matchers.isIn(expectedPaths));
            }
        }
    } finally {
        if (localFs.exists(testFile)) {
            localFs.delete(testFile, true);
        }
        if (localFs.exists(testOutFile)) {
            localFs.delete(testOutFile, true);
        }
    }
}

From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java

protected String getConfigPath(DeploymentArtifact artifact) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact())));
    File tmpDir = new File("user-conf", ArtifactHelpers.getServiceId(artifact));
    tmpDir.mkdirs();// www.ja  v  a  2  s.c  o m
    String configPath = tmpDir.getAbsolutePath();

    try {
        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            if (entry.getName().startsWith("config/")) {
                File tmpFile = new File(configPath,
                        Files.getNameWithoutExtension(entry.getName()) + ".properties");
                FileOutputStream fos = new FileOutputStream(tmpFile);
                try {
                    IOUtils.copy(tarIn, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
            }
            entry = tarIn.getNextTarEntry();
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }
    return configPath;
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

private static void untargz(File tarGzFile, File targetDir) throws IOException {
    try (TarArchiveInputStream tis = new TarArchiveInputStream(
            new GZIPInputStream(new FileInputStream(tarGzFile)))) {
        extractTar(tis, targetDir);//from  ww w  .ja v  a  2 s .  c  om
    }
}

From source file:io.takari.maven.testing.executor.junit.MavenVersionResolver.java

private void unarchive(File archive, File directory) throws IOException {
    try (TarArchiveInputStream ais = new TarArchiveInputStream(
            new GzipCompressorInputStream(new FileInputStream(archive)))) {
        TarArchiveEntry entry;/*from w ww  .  j a v  a2  s.c o  m*/
        while ((entry = ais.getNextTarEntry()) != null) {
            if (entry.isFile()) {
                String name = entry.getName();
                File file = new File(directory, name);
                file.getParentFile().mkdirs();
                try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
                    copy(ais, os);
                }
                int mode = entry.getMode();
                if (mode != -1 && (mode & 0100) != 0) {
                    try {
                        Path path = file.toPath();
                        Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(path);
                        permissions.add(PosixFilePermission.OWNER_EXECUTE);
                        Files.setPosixFilePermissions(path, permissions);
                    } catch (UnsupportedOperationException e) {
                        // must be windows, ignore
                    }
                }
            }
        }
    }
}

From source file:com.vmware.content.samples.ImportOva.java

private void uploadOva(String ovaPath, String sessionId) throws Exception {
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    IOUtil.print("Streaming OVF to update session " + sessionId);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(ovaPath))) {
        TarArchiveEntry entry;/*from  www .j  av  a  2  s . co  m*/
        while ((entry = tar.getNextTarEntry()) != null) {
            long bytes = entry.getSize();
            IOUtil.print("Uploading " + entry.getName() + " (" + entry.getSize() + " bytes)");
            URI uploadUri = generateUploadUri(sessionId, entry.getName());
            HttpPut request = new HttpPut(uploadUri);
            HttpEntity content = new TarBasedInputStreamEntity(tar, bytes);
            request.setEntity(content);
            HttpResponse response = httpclient.execute(request);
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

From source file:com.google.devtools.build.android.PackedResourceTarExpander.java

private void unTarPackedResources(final Path tarOut, final Path packedResources) throws IOException {
    LOGGER.fine(String.format("Found packed resources: %s", packedResources));
    try (InputStream inputStream = Files.newInputStream(packedResources);
            TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream)) {
        byte[] temp = new byte[4 * 1024];
        for (TarArchiveEntry entry = tarStream.getNextTarEntry(); entry != null; entry = tarStream
                .getNextTarEntry()) {/*from w  w w . jav a2 s . co m*/
            if (!entry.isFile()) {
                continue;
            }
            int read = tarStream.read(temp);
            // packed tars can start with a ./. This can cause issues, so remove it.
            final Path entryPath = tarOut.resolve(entry.getName().replace("^\\./", ""));
            Files.createDirectories(entryPath.getParent());
            final OutputStream entryOutStream = Files.newOutputStream(entryPath);
            while (read > -1) {
                entryOutStream.write(temp, 0, read);
                read = tarStream.read(temp);
            }
            entryOutStream.flush();
            entryOutStream.close();
        }
    }
}

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param archive/*from   ww  w .ja va  2 s  . c  o m*/
 * @param name
 * @return
 * @throws IOException
 */
private static final String getTarFile(String archive, String name) throws IOException {
    TarArchiveInputStream input = new TarArchiveInputStream(
            new BufferedInputStream(new FileInputStream(archive)));
    TarArchiveEntry entry;
    while ((entry = input.getNextTarEntry()) != null) {
        if (entry.getName().equals(name)) {
            byte[] content = new byte[(int) entry.getSize()];
            input.read(content, 0, content.length);
            input.close();
            return new String(content);
        }
    }
    input.close();
    return "";
}