Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get this entry's name.

Usage

From source file:org.eclipse.che.api.vfs.TarArchiverTest.java

private Map<String, String> readArchiveEntries(InputStream archive) throws Exception {
    Map<String, String> entries = newHashMap();
    try (TarArchiveInputStream tarIn = new TarArchiveInputStream(archive)) {
        TarArchiveEntry tarArchiveEntry;
        while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) {
            String name = tarArchiveEntry.getName();
            String content = tarArchiveEntry.isDirectory() ? "<none>"
                    : new String(ByteStreams.toByteArray(tarIn));
            entries.put(name, content);//from  ww w  . ja  v a 2s  .co m
        }
    }
    return entries;
}

From source file:org.eclipse.che.api.vfs.TarArchiverTest.java

private void assertThatTarArchiveContainsAllEntries(InputStream in, Map<String, String> entries)
        throws Exception {
    try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) {
        TarArchiveEntry tarArchiveEntry;
        while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) {
            String name = tarArchiveEntry.getName();
            assertTrue(String.format("Unexpected entry %s in TAR", name), entries.containsKey(name));
            if (!tarArchiveEntry.isDirectory()) {
                String content = new String(ByteStreams.toByteArray(tarIn));
                assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
            }//  w w w.j  ava 2s. com
            entries.remove(name);
        }
    }
    assertTrue(String.format("Expected but were not found in TAR %s", entries), entries.isEmpty());
}

From source file:org.eclipse.che.commons.lang.TarUtils.java

public static void untar(InputStream in, File targetDir) throws IOException {
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    byte[] b = new byte[BUF_SIZE];
    TarArchiveEntry tarEntry;
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        final File file = new File(targetDir, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!file.mkdirs()) {
                throw new IOException("Unable to create folder " + file.getAbsolutePath());
            }/*from  w ww .ja  va 2 s . c  o  m*/
        } else {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent.getAbsolutePath());
                }
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = tarIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        }
    }
}

From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarLeveledStructureProvider.java

/**
 * Creates a new tar file entry with the specified name.
 * @param entry the entry to create the file for
 *//*from   w ww .jav  a  2s .  c  o m*/
protected void createFile(TarArchiveEntry entry) {
    IPath pathname = new Path(entry.getName());
    TarArchiveEntry parent;
    if (pathname.segmentCount() == 1) {
        parent = root;
    } else {
        parent = directoryEntryCache.get(pathname.removeLastSegments(1));
    }

    List<TarArchiveEntry> childList = children.get(parent);
    NonNullUtils.checkNotNull(childList).add(entry);
}

From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarLeveledStructureProvider.java

/**
 * Initializes this object's children table based on the contents of the
 * specified source file./*ww  w. j  ava2 s  .  c o  m*/
 */
protected void initialize() {
    children = new HashMap<>(1000);

    children.put(root, new ArrayList<>());
    Enumeration<TarArchiveEntry> entries = tarFile.entries();
    while (entries.hasMoreElements()) {
        TarArchiveEntry entry = entries.nextElement();
        IPath path = new Path(entry.getName()).addTrailingSeparator();

        if (entry.isDirectory()) {
            createContainer(path);
        } else {
            // Ensure the container structure for all levels above this is initialized
            // Once we hit a higher-level container that's already added we need go no further
            int pathSegmentCount = path.segmentCount();
            if (pathSegmentCount > 1) {
                createContainer(path.uptoSegment(pathSegmentCount - 1));
            }
            createFile(entry);
        }
    }
}

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

private Map<String, TarArchiveEntry> getTarEntries() throws IOException, FileNotFoundException {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(
            new GzipCompressorInputStream(new FileInputStream(tarGzArchive)));
    Map<String, TarArchiveEntry> entries = new HashMap<String, TarArchiveEntry>();
    try {/*  w ww .ja  va2s. c om*/
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarStream.getNextTarEntry()) != null) {
            entries.put(tarEntry.getName(), tarEntry);
        }
    } finally {
        tarStream.close();
    }
    return entries;
}

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 {/* w  w  w.  ja va  2  s .  co m*/
        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.efaps.esjp.admin.update.UpdatePack.java

/**
 * Check revisions.//from   w  w  w  .  j a  v a 2  s . c om
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @return the return
 * @throws EFapsException on error
 * @throws InstallationException on error
 */
public Return execute(final Parameter _parameter) throws EFapsException, InstallationException {
    final Context context = Context.getThreadContext();
    final Context.FileParameter fileItem = context.getFileParameters().get("pack");
    final boolean compress = GzipUtils.isCompressedFilename(fileItem.getName());

    try (final TarArchiveInputStream tarInput = new TarArchiveInputStream(
            compress ? new GzipCompressorInputStream(fileItem.getInputStream()) : fileItem.getInputStream());) {

        File tmpfld = AppConfigHandler.get().getTempFolder();
        if (tmpfld == null) {
            final File temp = File.createTempFile("eFaps", ".tmp");
            tmpfld = temp.getParentFile();
            temp.delete();
        }
        final File updateFolder = new File(tmpfld, Update.TMPFOLDERNAME);
        if (!updateFolder.exists()) {
            updateFolder.mkdirs();
        }
        final File dateFolder = new File(updateFolder, ((Long) new Date().getTime()).toString());
        dateFolder.mkdirs();

        final Map<String, URL> files = new HashMap<>();
        TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
        while (currentEntry != null) {
            final byte[] bytess = new byte[(int) currentEntry.getSize()];
            tarInput.read(bytess);
            final File file = new File(dateFolder.getAbsolutePath() + "/" + currentEntry.getName());
            file.getParentFile().mkdirs();
            final FileOutputStream output = new FileOutputStream(file);
            output.write(bytess);
            output.close();
            files.put(currentEntry.getName(), file.toURI().toURL());
            currentEntry = tarInput.getNextTarEntry();
        }

        final Map<RevItem, InstallFile> installFiles = new HashMap<>();

        final URL json = files.get("revisions.json");
        final ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());
        final List<RevItem> items = mapper.readValue(new File(json.toURI()),
                mapper.getTypeFactory().constructCollectionType(List.class, RevItem.class));
        final List<RevItem> allItems = new ArrayList<>();
        allItems.addAll(items);

        installFiles.putAll(getInstallFiles(files, items, CIAdmin.Abstract));
        installFiles.putAll(getInstallFiles(files, items, CIAdminUser.Abstract));
        installFiles.putAll(getInstallFiles(files, items, CIAdminAccess.AccessSet));
        installFiles.putAll(getInstallFiles(files, items, CICommon.DBPropertiesBundle));

        final Iterator<RevItem> iter = items.iterator();
        int i = 0;
        while (iter.hasNext()) {
            final RevItem item = iter.next();
            LOG.info("Adding unfound Item {} / {}: {}", i, items.size(), item.getIdentifier());
            final InstallFile installFile = new InstallFile().setName(item.getName4InstallFile())
                    .setURL(item.getURL(files)).setType(item.getFileType().getType())
                    .setRevision(item.getRevision()).setDate(item.getDate());
            installFiles.put(item, installFile);
            i++;
        }

        final List<InstallFile> installFileList = new ArrayList<>(installFiles.values());
        Collections.sort(installFileList, new Comparator<InstallFile>() {
            @Override
            public int compare(final InstallFile _installFile0, final InstallFile _installFile1) {
                return _installFile0.getName().compareTo(_installFile1.getName());
            }
        });

        final List<InstallFile> dependendFileList = new ArrayList<>();
        // check if a object that depends on another object must be added to the update
        final Map<String, String> depenMap = getDependendMap();
        final Set<String> tobeAdded = new HashSet<>();
        for (final RevItem item : installFiles.keySet()) {
            if (depenMap.containsKey(item.getIdentifier())) {
                tobeAdded.add(depenMap.get(item.getIdentifier()));
            }
        }
        if (!tobeAdded.isEmpty()) {
            // check if the object to be added is already part ot the list
            for (final RevItem item : installFiles.keySet()) {
                final Iterator<String> tobeiter = tobeAdded.iterator();
                while (tobeiter.hasNext()) {
                    final String ident = tobeiter.next();
                    if (item.getIdentifier().equals(ident)) {
                        tobeiter.remove();
                    }
                }
            }
        }
        if (!tobeAdded.isEmpty()) {
            i = 1;
            // add the objects to the list taht are missing
            for (final RevItem item : allItems) {
                if (tobeAdded.contains(item.getIdentifier())) {
                    LOG.info("Adding releated Item {} / {}: {}", i, tobeAdded.size(), item);
                    final InstallFile installFile = new InstallFile().setName(item.getName4InstallFile())
                            .setURL(item.getURL(files)).setType(item.getFileType().getType())
                            .setRevision(item.getRevision()).setDate(item.getDate());
                    dependendFileList.add(installFile);
                    i++;
                }
            }
        }

        if (!installFileList.isEmpty()) {
            final Install install = new Install(true);
            for (final InstallFile installFile : installFileList) {
                LOG.info("...Adding to Update: '{}' ", installFile.getName());
                install.addFile(installFile);
            }
            install.updateLatest(null);
        }
        if (!dependendFileList.isEmpty()) {
            LOG.info("Update for related Items");
            final Install install = new Install(true);
            for (final InstallFile installFile : dependendFileList) {
                LOG.info("...Adding to Update: '{}' ", installFile.getName());
                install.addFile(installFile);
            }
            install.updateLatest(null);
        }
        LOG.info("Terminated update.");
    } catch (final IOException e) {
        LOG.error("Catched", e);
    } catch (final URISyntaxException e) {
        LOG.error("Catched", e);
    }
    return new Return();
}

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

@Override
protected Sequence processCompressedData(BinaryValue compressedData) throws XPathException, XMLDBException {
    TarArchiveInputStream tis = null;// ww  w .ja v  a  2 s .co  m
    try {
        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;/*from  w  ww  .  j a  va2s .  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);
    }
}