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

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

Introduction

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

Prototype

public TarArchiveEntry getNextTarEntry() throws IOException 

Source Link

Document

Get the next entry in this tar archive.

Usage

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

private byte[] getTarEntry(String name) throws IOException {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(
            new GzipCompressorInputStream(new FileInputStream(tarGzArchive)));
    try {/* ww w .  java  2  s .  com*/
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarStream.getNextTarEntry()) != null) {
            if (name.equals(tarEntry.getName())) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(tarStream, baos);
                return baos.toByteArray();
            }
        }
    } finally {
        tarStream.close();
    }
    throw new IOException(name + " not found in " + tarGzArchive);
}

From source file:org.exist.xquery.modules.compression.UnTarFunction.java

@Override
protected Sequence processCompressedData(BinaryValue compressedData) throws XPathException, XMLDBException {
    TarArchiveInputStream tis = null;
    try {/*from ww  w .j  av a2 s  .c o m*/
        tis = new TarArchiveInputStream(compressedData.getInputStream());
        TarArchiveEntry entry = null;

        Sequence results = new ValueSequence();

        while ((entry = tis.getNextTarEntry()) != null) {
            Sequence processCompressedEntryResults = processCompressedEntry(entry.getName(),
                    entry.isDirectory(), tis, filterParam, storeParam);

            results.addAll(processCompressedEntryResults);
        }

        return results;
    } catch (IOException ioe) {
        LOG.error(ioe.getMessage(), ioe);
        throw new XPathException(this, ioe.getMessage(), ioe);
    } finally {
        if (tis != null) {
            try {
                tis.close();
            } catch (IOException ioe) {
                LOG.warn(ioe.getMessage(), ioe);
            }
        }
    }
}

From source file:org.fabrician.maven.plugins.CompressUtils.java

public static void copyTargzToArchiveOutputStream(File targzSrc, FilenamePatternFilter filter,
        ArchiveOutputStream out, String alternateBaseDir) throws IOException {
    FileInputStream fin = null;/*  w w  w .j a va2 s  . c om*/
    CompressorInputStream zipIn = null;
    TarArchiveInputStream tarIn = null;
    try {
        fin = new FileInputStream(targzSrc);
        zipIn = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, fin);
        tarIn = new TarArchiveInputStream(zipIn);
        TarArchiveEntry entry = tarIn.getNextTarEntry();
        while (entry != null) {
            if (filter != null && !filter.accept(entry.getName())) {
                System.out.println("Excluding " + entry.getName());
            } else {
                out.putArchiveEntry(createArchiveEntry(entry, out, alternateBaseDir));
                IOUtils.copy(tarIn, out);
                out.closeArchiveEntry();
            }
            entry = tarIn.getNextTarEntry();
        }
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        close(zipIn);
        close(tarIn);
        close(fin);
    }
}

From source file:org.fabrician.maven.plugins.CompressUtils.java

public static boolean entryExistsInTargz(File targz, String entryName) throws IOException {
    FileInputStream fin = null;/*from  w  w  w.j  av a2 s .  c  om*/
    CompressorInputStream zipIn = null;
    TarArchiveInputStream tarIn = null;
    boolean exists = false;
    try {
        fin = new FileInputStream(targz);
        zipIn = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, fin);
        tarIn = new TarArchiveInputStream(zipIn);

        TarArchiveEntry entry = tarIn.getNextTarEntry();
        while (entry != null) {
            if (entry.getName().equals(entryName)) {
                exists = true;
                break;
            }
            entry = tarIn.getNextTarEntry();
        }
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        close(zipIn);
        close(tarIn);
        close(fin);
    }
    return exists;
}

From source file:org.finra.herd.service.helper.TarHelper.java

/**
 * Logs contents of a TAR file.//from  w ww .ja  v  a 2 s.  co  m
 *
 * @param tarFile the TAR file
 *
 * @throws IOException on error
 */
public void logTarFileContents(File tarFile) throws IOException {
    TarArchiveInputStream tarArchiveInputStream = null;

    try {
        tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(tarFile));
        TarArchiveEntry entry;

        LOGGER.info(String.format("Listing the contents of \"%s\" TAR archive file:", tarFile.getPath()));

        while (null != (entry = tarArchiveInputStream.getNextTarEntry())) {
            LOGGER.info(String.format("    %s", entry.getName()));
        }
    } finally {
        if (tarArchiveInputStream != null) {
            tarArchiveInputStream.close();
        }
    }
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

private UnpackResult unpack(CacheableEntity entity, TarArchiveInputStream tarInput,
        OriginReader readOriginAction) throws IOException {
    ImmutableMap.Builder<String, CacheableTree> treesBuilder = ImmutableMap.builder();
    entity.visitTrees((name, type, root) -> {
        if (root != null) {
            treesBuilder.put(name, new CacheableTree(type, root));
        }//from ww  w  .  j a v a 2  s  .  co  m
    });
    ImmutableMap<String, CacheableTree> treesByName = treesBuilder.build();

    TarArchiveEntry tarEntry;
    OriginMetadata originMetadata = null;
    Map<String, FileSystemLocationSnapshot> snapshots = new HashMap<String, FileSystemLocationSnapshot>();

    tarEntry = tarInput.getNextTarEntry();
    MutableLong entries = new MutableLong();
    while (tarEntry != null) {
        entries.increment(1);
        String path = tarEntry.getName();

        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
            tarEntry = tarInput.getNextTarEntry();
        } else {
            // handle tree
            Matcher matcher = TREE_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached entry format error, invalid contents: " + path);
            }

            String treeName = unescape(matcher.group(2));
            CacheableTree tree = treesByName.get(treeName);
            if (tree == null) {
                throw new IllegalStateException(String.format("No tree '%s' registered", treeName));
            }

            boolean missing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            tarEntry = unpackTree(treeName, tree.getType(), tree.getRoot(), tarInput, tarEntry, childPath,
                    missing, snapshots, entries);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }

    return new UnpackResult(originMetadata, entries.get(), snapshots);
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

@Nullable
private TarArchiveEntry unpackTree(String treeName, TreeType treeType, File treeRoot,
        TarArchiveInputStream input, TarArchiveEntry rootEntry, String childPath, boolean missing,
        Map<String, FileSystemLocationSnapshot> snapshots, MutableLong entries) throws IOException {
    boolean isDirEntry = rootEntry.isDirectory();
    boolean root = Strings.isNullOrEmpty(childPath);
    if (!root) {//from  w  w w . j  ava  2s. c om
        throw new IllegalStateException("Root needs to be the first entry in a tree");
    }
    // We are handling the root of the tree here
    if (missing) {
        unpackMissingFile(treeRoot);
        return input.getNextTarEntry();
    }

    ensureDirectoryForTree(treeType, treeRoot);
    if (treeType == TreeType.FILE) {
        if (isDirEntry) {
            throw new IllegalStateException("Should be a file: " + treeName);
        }
        RegularFileSnapshot fileSnapshot = unpackFile(input, rootEntry, treeRoot, treeRoot.getName());
        snapshots.put(treeName, fileSnapshot);
        return input.getNextTarEntry();
    }

    if (!isDirEntry) {
        throw new IllegalStateException("Should be a directory: " + treeName);
    }
    chmodUnpackedFile(rootEntry, treeRoot);

    return unpackDirectoryTree(input, rootEntry, snapshots, entries, treeRoot, treeName);
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

@Nullable
private TarArchiveEntry unpackDirectoryTree(TarArchiveInputStream input, TarArchiveEntry rootEntry,
        Map<String, FileSystemLocationSnapshot> snapshots, MutableLong entries, File treeRoot, String treeName)
        throws IOException {
    RelativePathParser parser = new RelativePathParser();
    parser.rootPath(rootEntry.getName());

    MerkleDirectorySnapshotBuilder builder = MerkleDirectorySnapshotBuilder.noSortingRequired();
    String rootPath = stringInterner.intern(treeRoot.getAbsolutePath());
    String rootDirName = stringInterner.intern(treeRoot.getName());
    builder.preVisitDirectory(rootPath, rootDirName);

    TarArchiveEntry entry;/*from w  w w  .ja  v a 2  s . c om*/

    while ((entry = input.getNextTarEntry()) != null) {
        entries.increment(1);
        boolean isDir = entry.isDirectory();
        int directoriesLeft = parser.nextPath(entry.getName(), isDir);
        for (int i = 0; i < directoriesLeft; i++) {
            builder.postVisitDirectory();
        }
        if (parser.getDepth() == 0) {
            break;
        }

        File file = new File(treeRoot, parser.getRelativePath());
        if (isDir) {
            FileUtils.forceMkdir(file);
            chmodUnpackedFile(entry, file);
            String internedAbsolutePath = stringInterner.intern(file.getAbsolutePath());
            String indernedDirName = stringInterner.intern(parser.getName());
            builder.preVisitDirectory(internedAbsolutePath, indernedDirName);
        } else {
            RegularFileSnapshot fileSnapshot = unpackFile(input, entry, file, parser.getName());
            builder.visit(fileSnapshot);
        }
    }

    for (int i = 0; i < parser.getDepth(); i++) {
        builder.postVisitDirectory();
    }

    snapshots.put(treeName, builder.getResult());
    return entry;
}

From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java

@Override
public void unpack(DataSource input, DataTargetFactory targetFactory) throws IOException {
    TarArchiveInputStream tarInput = new TarArchiveInputStream(input.openInput());
    while (true) {
        TarArchiveEntry entry = tarInput.getNextTarEntry();
        if (entry == null) {
            break;
        }// w w w. j a v a  2  s  .c o m
        PackerUtils.unpackEntry(entry.getName(), tarInput, buffer, targetFactory);
    }
    tarInput.close();
}

From source file:org.hyperledger.fabric.sdk.testutils.TestUtils.java

public static ArrayList tarBytesToEntryArrayList(byte[] bytes) throws Exception {

    ArrayList<String> ret = new ArrayList<>();

    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(bytes)));

    for (TarArchiveEntry ta = tarArchiveInputStream.getNextTarEntry(); null != ta; ta = tarArchiveInputStream
            .getNextTarEntry()) {//w ww. jav a 2 s  . c o  m

        Assert.assertTrue(format("Tar entry %s is not a file.", ta.getName()), ta.isFile()); //we only expect files.
        ret.add(ta.getName());

    }

    return ret;

}