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:drawnzer.anurag.tarHelper.TarManager.java

public TarManager(File file, String pathToShow) throws IOException {
    // TODO Auto-generated constructor stub
    if (file.getName().endsWith(".tar.gz"))
        tar = new TarArchiveInputStream(
                new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))));
    else if (file.getName().endsWith(".tar.bz2") || file.getName().endsWith(".TAR.BZ2"))
        tar = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file))));
    else//  w w  w  .j av  a  2 s.  c o  m
        tar = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(file)));
    path = pathToShow;
}

From source file:com.foreignreader.util.WordNetExtractor.java

@Override
protected Void doInBackground(InputStream... streams) {
    assert streams.length == 1;

    try {/*from   w  w  w . j  av a 2  s.  c om*/

        BufferedInputStream in = new BufferedInputStream(streams[0]);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

        TarArchiveInputStream tar = new TarArchiveInputStream(gzIn);

        TarArchiveEntry tarEntry;

        File dest = LongTranslationHelper.getWordNetDict().getParentFile();

        while ((tarEntry = tar.getNextTarEntry()) != null) {
            File destPath = new File(dest, tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                byte[] btoRead = new byte[1024 * 10];

                BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tar.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();

            }
        }

        tar.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.wenzani.maven.mongodb.TarUtils.java

public String untargz(File archive, File outputDir) {
    String absolutePath = archive.getAbsolutePath();
    String root = null;//from w w  w .ja  v  a  2  s  .  com
    boolean first = true;

    while (absolutePath.contains("tar") || absolutePath.contains("gz") || absolutePath.contains("tgz")) {
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("."));
    }

    absolutePath = absolutePath + ".tar";

    try {
        GZIPInputStream input = new GZIPInputStream(new FileInputStream(archive));
        FileOutputStream fos = new FileOutputStream(new File(absolutePath));

        IOUtils.copy(input, fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fos);

        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                new FileInputStream(absolutePath));

        for (TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry(); entry != null;) {
            unpackEntries(tarArchiveInputStream, entry, outputDir);

            if (first && entry.isDirectory()) {
                root = outputDir + File.separator + entry.getName();
            }

            entry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return root;
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction./* w w w.  j  a v a  2 s .  c  o  m*/
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}

From source file:com.spotify.docker.client.CompressedDirectoryTest.java

@Test
public void testFile() throws Exception {
    // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
    final URL dockerDirectory = Resources.getResource("dockerDirectory");
    try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
            BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
            GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;//from   w  w  w.ja  v a  2 s .  c  o m
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, containsInAnyOrder("Dockerfile", "bin/date.sh", "innerDir/innerDockerfile"));
    }
}

From source file:com.facebook.buck.testutil.integration.TarInspector.java

private static TarArchiveInputStream getArchiveInputStream(Optional<String> compressorType, Path tar)
        throws IOException, CompressorException {
    BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(tar));
    if (compressorType.isPresent()) {
        return new TarArchiveInputStream(
                new CompressorStreamFactory().createCompressorInputStream(compressorType.get(), inputStream));
    }//from   w  ww . j a  v  a  2 s.c  o m
    return new TarArchiveInputStream(inputStream);
}

From source file:com.pinterest.deployservice.common.TarUtils.java

/**
 * Unbundle the given tar bar as a map, with key as file name and value as content.
 *///from  w  w  w.  java 2  s.c  om
public static Map<String, String> untar(InputStream is) throws Exception {
    TarArchiveInputStream tais = new TarArchiveInputStream(new GZIPInputStream(is));
    Map<String, String> data = new HashMap<String, String>();
    TarArchiveEntry entry;
    while ((entry = tais.getNextTarEntry()) != null) {
        String name = entry.getName();
        byte[] content = new byte[(int) entry.getSize()];
        tais.read(content, 0, content.length);
        data.put(name, new String(content, "UTF8"));
    }
    tais.close();
    return data;
}

From source file:de.flapdoodle.embedmongo.extract.TgzExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {

    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    try {//from  w  w w .  ja va2s .co m
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (tarIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(tarIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        tarIn.close();
        gzIn.close();
    }
}

From source file:io.wcm.maven.plugins.nodejs.installation.TarUnArchiver.java

/**
 * Unarchives the arvive into the pBaseDir
 * @param baseDir//from   www  . j  a v a 2s  . c o  m
 * @throws MojoExecutionException
 */
public void unarchive(String baseDir) throws MojoExecutionException {
    try {
        FileInputStream fis = new FileInputStream(archive);

        // TarArchiveInputStream can be constructed with a normal FileInputStream if
        // we ever need to extract regular '.tar' files.
        final TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));

        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        while (tarEntry != null) {
            // Create a file for this tarEntry
            final File destPath = new File(baseDir + File.separator + tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                destPath.setExecutable(true);
                //byte [] btoRead = new byte[(int)tarEntry.getSize()];
                byte[] btoRead = new byte[8024];
                final BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tarIn.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();
            }
            tarEntry = tarIn.getNextTarEntry();
        }
        tarIn.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not extract archive: '" + archive.getAbsolutePath() + "'", e);
    }

}

From source file:com.streamsets.datacollector.restapi.TarEdgeArchiveBuilder.java

@Override
public void finish() throws IOException {
    try (TarArchiveOutputStream tarArchiveOutput = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(outputStream));
            TarArchiveInputStream tarArchiveInput = new TarArchiveInputStream(
                    new GzipCompressorInputStream(new FileInputStream(edgeArchive)))) {
        tarArchiveOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        TarArchiveEntry entry = tarArchiveInput.getNextTarEntry();

        while (entry != null) {
            tarArchiveOutput.putArchiveEntry(entry);
            IOUtils.copy(tarArchiveInput, tarArchiveOutput);
            tarArchiveOutput.closeArchiveEntry();
            entry = tarArchiveInput.getNextTarEntry();
        }/*from w  w  w.  j av  a 2  s . c o m*/

        for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) {
            addArchiveEntry(tarArchiveOutput, pipelineConfiguration, pipelineConfiguration.getPipelineId(),
                    PIPELINE_JSON_FILE);
            addArchiveEntry(tarArchiveOutput, pipelineConfiguration.getInfo(),
                    pipelineConfiguration.getPipelineId(), PIPELINE_INFO_FILE);
        }

        tarArchiveOutput.finish();
    }
}