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.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

public static void extractFiles(InputStream is, Path localPath) {
    ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
    try {//w w w  . j  a v a2s  .c  o  m
        Files.createDirectories(localPath);
    } catch (IOException e) {
        throw new ArchiveExtractionException(e);
    }
    try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(is);) {
        extractArchive(localPath, ais);
    } catch (ArchiveException e) {
        LOGGER.info("archiveFactory could not determine archive file type probably tar.gz");
        try (ArchiveInputStream ais = new TarArchiveInputStream(new GzipCompressorInputStream(is))) {
            extractArchive(localPath, ais);
        } catch (IOException e1) {
            throw new ArchiveExtractionException(e1);
        }
    } catch (IOException e) {
        throw new ArchiveExtractionException(e);
    }

}

From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java

private static void extractTarGz(final File source, final File destination) throws IOException {
    try (final ArchiveInputStream tarGzArchiveInputStream = new TarArchiveInputStream(
            new GzipCompressorInputStream(new FileInputStream(source)))) {
        extractArchive(destination, tarGzArchiveInputStream);
    }//from  w  w w .ja va2  s  .c  o  m
}

From source file:com.soeima.resources.tar.TarArchive.java

/**
 * @see  AbstractCompressArchive#newArchiveInputStream(InputStream)
 *//*  w  w w. j  ava2s  . co  m*/
@Override
protected ArchiveInputStream newArchiveInputStream(InputStream is) {
    return new TarArchiveInputStream(is);
}

From source file:it.geosolutions.tools.compress.file.reader.TarReader.java

/**
 * A method to read tar file:/*w ww.j  ava 2s . c o  m*/
 * extract its content to the 'destDir' file (which could be
 * a directory or a file depending on the srcF file content)
 * @throws CompressorException 
 */
public static void readTar(File srcF, File destDir) throws Exception {
    if (destDir == null)
        throw new IllegalArgumentException("Unable to extract to a null destination dir");
    if (!destDir.canWrite() && !destDir.mkdirs())
        throw new IllegalArgumentException("Unable to extract to a not writeable destination dir: " + destDir);

    FileInputStream fis = null;
    TarArchiveInputStream tis = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(srcF);
        bis = new BufferedInputStream(fis);
        tis = new TarArchiveInputStream(bis);

        TarArchiveEntry te = null;
        while ((te = tis.getNextTarEntry()) != null) {

            File curr_dest = new File(destDir, te.getName());
            if (te.isDirectory()) {
                // create destination folder
                if (!curr_dest.exists())
                    curr_dest.mkdirs();
            } else {
                writeFile(curr_dest, tis);
            }
        }
    } finally {
        if (tis != null) {
            try {
                tis.close();
            } catch (IOException ioe) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ioe) {
            }
        }

    }
}

From source file:cc.twittertools.corpus.data.TarJsonStatusCorpusReader.java

public TarJsonStatusCorpusReader(File file) throws IOException {
    Preconditions.checkNotNull(file);/*  www  .  ja  v a  2  s  .  co m*/

    if (!file.isFile()) {
        throw new IOException("Expecting " + file + " to be a file!");
    }

    tarInput = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(file)));
}

From source file:de.flapdoodle.embed.process.extract.TgzExtractor.java

protected ArchiveWrapper archiveStream(File source) throws IOException {
    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    return new TarArchiveWrapper(tarIn);
}

From source file:de.flapdoodle.embed.process.extract.Tbz2Extractor.java

@Override
protected ArchiveWrapper archiveStream(File source) throws IOException {
    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    BZip2CompressorInputStream gzIn = new BZip2CompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    return new TarArchiveWrapper(tarIn);
}

From source file:examples.utils.CifarReader.java

public static void downloadAndExtract() {

    if (new File("data", TEST_DATA_FILE).exists() == false) {
        try {//from   w  w  w . j av a 2 s .c  o  m
            if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) {
                URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE);
                FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE);
                fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE);
                fos.close();
            }
            TarArchiveInputStream tar = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE)));
            TarArchiveEntry entry = null;
            while ((entry = tar.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    new File("data", entry.getName()).mkdirs();
                } else {
                    byte data[] = new byte[2048];
                    int count;
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File("data/", entry.getName())), 2048);

                    while ((count = tar.read(data, 0, 2048)) != -1) {
                        bos.write(data, 0, count);
                    }
                    bos.close();
                }
            }
            tar.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:io.github.blindio.prospero.core.browserdrivers.phantomjs.TarBZip2UnArchiver.java

public void extract() {
    /** create a TarArchiveInputStream object. **/

    try {//w  w  w  . j  av  a2 s .co m
        FileInputStream fin = new FileInputStream(getSourceFile());
        BufferedInputStream in = new BufferedInputStream(fin);
        BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
        ArchiveInputStream arcIn = new TarArchiveInputStream(bzIn);

        extract(arcIn);
    } catch (IOException ioe) {
        throw new ProsperoIOException(ioe);
    }
}

From source file:de.dentrassi.pm.npm.aspect.NpmExtractor.java

private void perform(final Path file, final Map<String, String> metadata) throws IOException {
    try (final GZIPInputStream gis = new GZIPInputStream(new FileInputStream(file.toFile()));
            final TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
        TarArchiveEntry entry;//  w w  w.j  a va2  s. c  o m
        while ((entry = tis.getNextTarEntry()) != null) {
            if (entry.getName().equals("package/package.json")) {
                final byte[] data = new byte[(int) entry.getSize()];
                ByteStreams.read(tis, data, 0, data.length);

                final String str = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(data)).toString();

                try {
                    // test parse
                    new JsonParser().parse(str);
                    // store
                    metadata.put("package.json", str);
                } catch (final JsonParseException e) {
                    // ignore
                }

                break; // stop parsing the archive
            }
        }

    }
}