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:org.soulwing.credo.service.archive.TarGzipArchiveBuilderTest.java

@Test
public void testBuildArchiveEntryWithMultipleContent() throws Exception {
    byte[] archive = builder.beginEntry("file.txt", "UTF-8").addContent(new StringReader("content1"))
            .addContent(new StringReader("content2")).endEntry().build();
    TarArchiveInputStream tis = new TarArchiveInputStream(
            new GzipCompressorInputStream(new ByteArrayInputStream(archive)));
    TarArchiveEntry entry = tis.getNextTarEntry();
    assertThat(entry.getName(), is(equalTo("file.txt")));
    assertThat(readContent(tis), is(equalTo("content1content2")));
    assertThat(tis.getNextEntry(), is(nullValue()));
}

From source file:org.springframework.boot.gradle.plugin.ApplicationPluginActionIntegrationTests.java

private List<String> tarEntryNames(File distribution) throws IOException {
    List<String> entryNames = new ArrayList<>();
    try (TarArchiveInputStream input = new TarArchiveInputStream(new FileInputStream(distribution))) {
        TarArchiveEntry entry;//w w  w .ja va  2s  . c  o m
        while ((entry = input.getNextTarEntry()) != null) {
            entryNames.add(entry.getName());
        }
    }
    return entryNames;
}

From source file:org.springframework.boot.gradle.plugin.ApplicationPluginActionIntegrationTests.java

private void tarEntries(File distribution, Consumer<TarArchiveEntry> consumer) throws IOException {
    try (TarArchiveInputStream input = new TarArchiveInputStream(new FileInputStream(distribution))) {
        TarArchiveEntry entry;//from w w w. ja v a 2s. c o  m
        while ((entry = input.getNextTarEntry()) != null) {
            consumer.accept(entry);
        }
    }
}

From source file:org.torproject.collector.bridgedescs.BridgeSnapshotReader.java

/**
 * Reads the half-hourly snapshots of bridge descriptors from Bifroest.
 *///from www  . j a  v  a  2s  .c  om
public BridgeSnapshotReader(BridgeDescriptorParser bdp, File bridgeDirectoriesDir, File statsDirectory)
        throws ConfigurationException {

    if (bdp == null || bridgeDirectoriesDir == null || statsDirectory == null) {
        throw new IllegalArgumentException();
    }

    SortedSet<String> parsed = new TreeSet<String>();
    File bdDir = bridgeDirectoriesDir;
    File pbdFile = new File(statsDirectory, "parsed-bridge-directories");
    boolean modified = false;
    if (bdDir.exists()) {
        if (pbdFile.exists()) {
            logger.debug("Reading file " + pbdFile.getAbsolutePath() + "...");
            try {
                BufferedReader br = new BufferedReader(new FileReader(pbdFile));
                String line = null;
                while ((line = br.readLine()) != null) {
                    parsed.add(line);
                }
                br.close();
                logger.debug("Finished reading file " + pbdFile.getAbsolutePath() + ".");
            } catch (IOException e) {
                logger.warn("Failed reading file " + pbdFile.getAbsolutePath() + "!", e);
                return;
            }
        }
        logger.debug("Importing files in directory " + bridgeDirectoriesDir + "/...");
        Set<String> descriptorImportHistory = new HashSet<String>();
        int parsedFiles = 0;
        int skippedFiles = 0;
        int parsedStatuses = 0;
        int parsedServerDescriptors = 0;
        int skippedServerDescriptors = 0;
        int parsedExtraInfoDescriptors = 0;
        int skippedExtraInfoDescriptors = 0;
        Stack<File> filesInInputDir = new Stack<File>();
        filesInInputDir.add(bdDir);
        while (!filesInInputDir.isEmpty()) {
            File pop = filesInInputDir.pop();
            if (pop.isDirectory()) {
                for (File f : pop.listFiles()) {
                    filesInInputDir.add(f);
                }
            } else if (!parsed.contains(pop.getName())) {
                try {
                    FileInputStream in = new FileInputStream(pop);
                    if (in.available() > 0) {
                        TarArchiveInputStream tais = null;
                        if (pop.getName().endsWith(".tar.gz")) {
                            GzipCompressorInputStream gcis = new GzipCompressorInputStream(in);
                            tais = new TarArchiveInputStream(gcis);
                        } else if (pop.getName().endsWith(".tar")) {
                            tais = new TarArchiveInputStream(in);
                        } else {
                            continue;
                        }
                        BufferedInputStream bis = new BufferedInputStream(tais);
                        String fn = pop.getName();
                        String[] fnParts = fn.split("-");
                        if (fnParts.length != 5) {
                            logger.warn("Invalid bridge descriptor tarball file name: " + fn + ".  Skipping.");
                            continue;
                        }
                        String authorityPart = String.format("%s-%s-", fnParts[0], fnParts[1]);
                        String datePart = String.format("%s-%s-%s", fnParts[2], fnParts[3], fnParts[4]);
                        String authorityFingerprint;
                        switch (authorityPart) {
                        case "from-tonga-":
                            authorityFingerprint = "4A0CCD2DDC7995083D73F5D667100C8A5831F16D";
                            break;
                        case "from-bifroest-":
                            authorityFingerprint = "1D8F3A91C37C5D1C4C19B1AD1D0CFBE8BF72D8E1";
                            break;
                        default:
                            logger.warn("Did not recognize the bridge authority that " + "generated " + fn
                                    + ".  Skipping.");
                            continue;
                        }
                        String dateTime = datePart.substring(0, 10) + " " + datePart.substring(11, 13) + ":"
                                + datePart.substring(13, 15) + ":" + datePart.substring(15, 17);
                        while ((tais.getNextTarEntry()) != null) {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            int len;
                            byte[] data = new byte[1024];
                            while ((len = bis.read(data, 0, 1024)) >= 0) {
                                baos.write(data, 0, len);
                            }
                            byte[] allData = baos.toByteArray();
                            if (allData.length == 0) {
                                continue;
                            }
                            String fileDigest = Hex.encodeHexString(DigestUtils.sha(allData));
                            String ascii = new String(allData, "US-ASCII");
                            BufferedReader br3 = new BufferedReader(new StringReader(ascii));
                            String firstLine = null;
                            while ((firstLine = br3.readLine()) != null) {
                                if (firstLine.startsWith("@")) {
                                    continue;
                                } else {
                                    break;
                                }
                            }
                            if (firstLine == null) {
                                continue;
                            }
                            if (firstLine.startsWith("published ") || firstLine.startsWith("flag-thresholds ")
                                    || firstLine.startsWith("r ")) {
                                bdp.parse(allData, dateTime, authorityFingerprint);
                                parsedStatuses++;
                            } else if (descriptorImportHistory.contains(fileDigest)) {
                                /* Skip server descriptors or extra-info descriptors if
                                 * we parsed them before. */
                                skippedFiles++;
                                continue;
                            } else {
                                int start = -1;
                                int sig = -1;
                                int end = -1;
                                String startToken = firstLine.startsWith("router ") ? "router " : "extra-info ";
                                String sigToken = "\nrouter-signature\n";
                                String endToken = "\n-----END SIGNATURE-----\n";
                                while (end < ascii.length()) {
                                    start = ascii.indexOf(startToken, end);
                                    if (start < 0) {
                                        break;
                                    }
                                    sig = ascii.indexOf(sigToken, start);
                                    if (sig < 0) {
                                        break;
                                    }
                                    sig += sigToken.length();
                                    end = ascii.indexOf(endToken, sig);
                                    if (end < 0) {
                                        break;
                                    }
                                    end += endToken.length();
                                    byte[] descBytes = new byte[end - start];
                                    System.arraycopy(allData, start, descBytes, 0, end - start);
                                    String descriptorDigest = Hex.encodeHexString(DigestUtils.sha(descBytes));
                                    if (!descriptorImportHistory.contains(descriptorDigest)) {
                                        bdp.parse(descBytes, dateTime, authorityFingerprint);
                                        descriptorImportHistory.add(descriptorDigest);
                                        if (firstLine.startsWith("router ")) {
                                            parsedServerDescriptors++;
                                        } else {
                                            parsedExtraInfoDescriptors++;
                                        }
                                    } else {
                                        if (firstLine.startsWith("router ")) {
                                            skippedServerDescriptors++;
                                        } else {
                                            skippedExtraInfoDescriptors++;
                                        }
                                    }
                                }
                            }
                            descriptorImportHistory.add(fileDigest);
                            parsedFiles++;
                        }
                        bis.close();
                    }
                    in.close();

                    /* Let's give some memory back, or we'll run out of it. */
                    System.gc();

                    parsed.add(pop.getName());
                    modified = true;
                } catch (IOException e) {
                    logger.warn("Could not parse bridge snapshot " + pop.getName() + "!", e);
                    continue;
                }
            }
        }
        logger.debug("Finished importing files in directory " + bridgeDirectoriesDir
                + "/.  In total, we parsed " + parsedFiles + " files (skipped " + skippedFiles + ") containing "
                + parsedStatuses + " statuses, " + parsedServerDescriptors + " server descriptors (skipped "
                + skippedServerDescriptors + "), and " + parsedExtraInfoDescriptors + " extra-info descriptors "
                + "(skipped " + skippedExtraInfoDescriptors + ").");
        if (!parsed.isEmpty() && modified) {
            logger.debug("Writing file " + pbdFile.getAbsolutePath() + "...");
            pbdFile.getParentFile().mkdirs();
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(pbdFile))) {
                for (String f : parsed) {
                    bw.append(f + "\n");
                }
                logger.debug("Finished writing file " + pbdFile.getAbsolutePath() + ".");
            } catch (IOException e) {
                logger.warn("Failed writing file " + pbdFile.getAbsolutePath() + "!", e);
            }
        }
    }
}

From source file:org.torproject.ernie.db.BridgeSnapshotReader.java

public BridgeSnapshotReader(BridgeDescriptorParser bdp, String bridgeDirectoriesDir) {
    Logger logger = Logger.getLogger(BridgeSnapshotReader.class.getName());
    SortedSet<String> parsed = new TreeSet<String>();
    File bdDir = new File(bridgeDirectoriesDir);
    File pbdFile = new File("stats/parsed-bridge-directories");
    boolean modified = false;
    if (bdDir.exists()) {
        if (pbdFile.exists()) {
            logger.fine("Reading file " + pbdFile.getAbsolutePath() + "...");
            try {
                BufferedReader br = new BufferedReader(new FileReader(pbdFile));
                String line = null;
                while ((line = br.readLine()) != null) {
                    parsed.add(line);/*from www. jav  a2s .  c o  m*/
                }
                br.close();
                logger.fine("Finished reading file " + pbdFile.getAbsolutePath() + ".");
            } catch (IOException e) {
                logger.log(Level.WARNING, "Failed reading file " + pbdFile.getAbsolutePath() + "!", e);
                return;
            }
        }
        logger.fine("Importing files in directory " + bridgeDirectoriesDir + "/...");
        Stack<File> filesInInputDir = new Stack<File>();
        filesInInputDir.add(bdDir);
        while (!filesInInputDir.isEmpty()) {
            File pop = filesInInputDir.pop();
            if (pop.isDirectory()) {
                for (File f : pop.listFiles()) {
                    filesInInputDir.add(f);
                }
            } else if (!parsed.contains(pop.getName())) {
                try {
                    FileInputStream in = new FileInputStream(pop);
                    if (in.available() > 0) {
                        GzipCompressorInputStream gcis = new GzipCompressorInputStream(in);
                        TarArchiveInputStream tais = new TarArchiveInputStream(gcis);
                        BufferedInputStream bis = new BufferedInputStream(tais);
                        String fn = pop.getName();
                        String dateTime = fn.substring(11, 21) + " " + fn.substring(22, 24) + ":"
                                + fn.substring(24, 26) + ":" + fn.substring(26, 28);
                        while ((tais.getNextTarEntry()) != null) {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            int len;
                            byte[] data = new byte[1024];
                            while ((len = bis.read(data, 0, 1024)) >= 0) {
                                baos.write(data, 0, len);
                            }
                            byte[] allData = baos.toByteArray();
                            if (allData.length == 0) {
                                continue;
                            }
                            String ascii = new String(allData, "US-ASCII");
                            BufferedReader br3 = new BufferedReader(new StringReader(ascii));
                            String firstLine = null;
                            while ((firstLine = br3.readLine()) != null) {
                                if (firstLine.startsWith("@")) {
                                    continue;
                                } else {
                                    break;
                                }
                            }
                            if (firstLine.startsWith("r ")) {
                                bdp.parse(allData, dateTime, false);
                            } else {
                                int start = -1, sig = -1, end = -1;
                                String startToken = firstLine.startsWith("router ") ? "router " : "extra-info ";
                                String sigToken = "\nrouter-signature\n";
                                String endToken = "\n-----END SIGNATURE-----\n";
                                while (end < ascii.length()) {
                                    start = ascii.indexOf(startToken, end);
                                    if (start < 0) {
                                        break;
                                    }
                                    sig = ascii.indexOf(sigToken, start);
                                    if (sig < 0) {
                                        break;
                                    }
                                    sig += sigToken.length();
                                    end = ascii.indexOf(endToken, sig);
                                    if (end < 0) {
                                        break;
                                    }
                                    end += endToken.length();
                                    byte[] descBytes = new byte[end - start];
                                    System.arraycopy(allData, start, descBytes, 0, end - start);
                                    bdp.parse(descBytes, dateTime, false);
                                }
                            }
                        }
                    }
                    in.close();

                    /* Let's give some memory back, or we'll run out of it. */
                    System.gc();

                    parsed.add(pop.getName());
                    modified = true;
                } catch (IOException e) {
                    logger.log(Level.WARNING, "Could not parse bridge snapshot " + pop.getName() + "!", e);
                    continue;
                }
            }
        }
        logger.fine("Finished importing files in directory " + bridgeDirectoriesDir + "/.");
        if (!parsed.isEmpty() && modified) {
            logger.fine("Writing file " + pbdFile.getAbsolutePath() + "...");
            try {
                pbdFile.getParentFile().mkdirs();
                BufferedWriter bw = new BufferedWriter(new FileWriter(pbdFile));
                for (String f : parsed) {
                    bw.append(f + "\n");
                }
                bw.close();
                logger.fine("Finished writing file " + pbdFile.getAbsolutePath() + ".");
            } catch (IOException e) {
                logger.log(Level.WARNING, "Failed writing file " + pbdFile.getAbsolutePath() + "!", e);
            }
        }
    }
}

From source file:org.trustedanalytics.servicebroker.gearpump.service.file.ArchiverService.java

public void untar(InputStream inputStream) throws IOException {
    this.shouldOverrideFiles = true;

    TarArchiveInputStream tarInput = null;
    try {// w ww .j  a v a 2s. c  om
        tarInput = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(inputStream)));
        unpack(tarInput);
    } finally {
        if (tarInput != null) {
            tarInput.close();
        }
    }

    LOGGER.debug("untar completed successfully!!");
}

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

/**
 * Decompresses/unarchives the file given by 'f' to the destination path
 * given by dest, and returns a list of all the files contained within the archive.
 * If the input file is compressed, but not archived, the destination filename will be 
 * "dest/"+stripExtension(f.getName()).  If the input file is 
 * not compressed or archived, the input file will be moved to the new location 
 * given by dest, and returned in a one-element list.
 * // w w w . jav  a 2  s  .c o  m
 * Note the file given by f will be deleted.
 *
 * @return A list of files that were decompressed from the input file f, or
 * a list containing the input file in its new location if the input file was not compressed/archived.
 * @throws IOException 
 * @see stripExtension
 */
public static List<File> decompressAndDelete(File f, File dest) throws IOException {
    String lcfn = f.getName().toLowerCase();
    InputStream is = new BufferedInputStream(new FileInputStream(f));
    List<File> files;

    //Detect compression type.
    if (lcfn.endsWith(".gz") || lcfn.endsWith(".tgz")) {
        is = new GzipCompressorInputStream(is, true);
    } else if (lcfn.endsWith(".bz2")) {
        is = new BZip2CompressorInputStream(is, true);
    }

    //Detect archive type.
    if (lcfn.endsWith(".tgz") || lcfn.endsWith(".tar.gz") || lcfn.endsWith(".tar")
            || lcfn.endsWith(".tar.bz2")) {
        is = new TarArchiveInputStream(is);
        files = unArchive(dest, (ArchiveInputStream) is);
    } else if (lcfn.endsWith(".zip")) {
        is = new ZipArchiveInputStream(is);
        files = unArchive(dest, (ArchiveInputStream) is);
    } else {
        String filename = f.getName();
        if (!(is instanceof BufferedInputStream)) {
            filename = stripExtension(f);
            if (filename == null) {
                filename = f.getName() + ".decompressed";
            }
        }

        File outputFile = new File(dest, filename);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile));
        if (!(is instanceof BufferedInputStream)) {
            IOUtils.copyStream(is, bos);
        } else {
            if (!moveFile(f, outputFile)) {
                throw new IOException(
                        "Couldn't move file " + f.getAbsolutePath() + " to " + outputFile.getAbsolutePath());
            }
        }

        bos.close();
        files = new ArrayList<File>(1);
        files.add(outputFile);
    }
    is.close();
    if (f.exists()) {
        f.delete();
    }
    return files;
}

From source file:org.vafer.jdeb.ArchiveWalker.java

public static boolean walkEmbedded(File deb, final String name, final ArchiveVisitor<TarArchiveEntry> visitor,
        final Compression compression) throws IOException {
    final AtomicBoolean found = new AtomicBoolean(false);
    ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(deb));
    ArchiveWalker.walk(in, new ArchiveVisitor<ArArchiveEntry>() {
        public void visit(ArArchiveEntry entry, byte[] content) throws IOException {
            if (entry.getName().equals(name + compression.getExtension())) {
                InputStream in = new ByteArrayInputStream(content);
                if (compression == Compression.GZIP) {
                    in = new GZIPInputStream(in);
                } else if (compression == Compression.XZ) {
                    in = new XZCompressorInputStream(in);
                } else if (compression == Compression.BZIP2) {
                    in = new BZip2CompressorInputStream(in);
                }//from   w ww .ja  va  2 s. c  om

                ArchiveWalker.walk(new TarArchiveInputStream(in), new ArchiveVisitor<TarArchiveEntry>() {
                    public void visit(TarArchiveEntry entry, byte[] content) throws IOException {
                        found.set(true);
                        visitor.visit(entry, content);
                    }
                });
            }
        }
    });
    return found.get();
}

From source file:org.vafer.jdeb.DataBuilderTestCase.java

public void testCreateParentDirectories() throws Exception {
    File archive = new File("target/data.tar");
    if (archive.exists()) {
        archive.delete();/* ww  w. j  a v  a 2  s  . c o  m*/
    }

    DataBuilder builder = new DataBuilder(new NullConsole());

    DataProducer producer = new DataProducerFile(new File("pom.xml"), "/usr/share/myapp/pom.xml", null, null,
            null);

    builder.buildData(Arrays.asList(producer), archive, new StringBuilder(), Compression.NONE);

    int count = 0;
    TarArchiveInputStream in = null;
    try {
        in = new TarArchiveInputStream(new FileInputStream(archive));
        while (in.getNextTarEntry() != null) {
            count++;
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }

    assertEquals("entries", 4, count);
}

From source file:org.waarp.common.tar.TarUtility.java

/**
 * Extract all files from Tar into the specified directory
 * /*from www.  j  av a 2 s .c  o  m*/
 * @param tarFile
 * @param directory
 * @return the list of extracted filenames
 * @throws IOException
 */
public static List<String> unTar(File tarFile, File directory) throws IOException {
    List<String> result = new ArrayList<String>();
    InputStream inputStream = new FileInputStream(tarFile);
    TarArchiveInputStream in = new TarArchiveInputStream(inputStream);
    TarArchiveEntry entry = in.getNextTarEntry();
    while (entry != null) {
        if (entry.isDirectory()) {
            entry = in.getNextTarEntry();
            continue;
        }
        File curfile = new File(directory, entry.getName());
        File parent = curfile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(curfile);
        IOUtils.copy(in, out);
        out.close();
        result.add(entry.getName());
        entry = in.getNextTarEntry();
    }
    in.close();
    return result;
}