Example usage for org.apache.commons.compress.archivers ArchiveInputStream canReadEntryData

List of usage examples for org.apache.commons.compress.archivers ArchiveInputStream canReadEntryData

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveInputStream canReadEntryData.

Prototype

public boolean canReadEntryData(ArchiveEntry ae) 

Source Link

Document

Whether this stream is able to read the given entry.

Usage

From source file:mj.ocraptor.extraction.tika.parser.pkg.PackageParser.java

private void parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedDocumentExtractor extractor,
        XHTMLContentHandler xhtml) throws SAXException, IOException, TikaException {
    String name = entry.getName();
    if (archive.canReadEntryData(entry)) {
        Metadata entrydata = new Metadata();
        if (name != null && name.length() > 0) {
            entrydata.set(Metadata.RESOURCE_NAME_KEY, name);
            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
            attributes.addAttribute("", "id", "id", "CDATA", name);
            xhtml.startElement("div", attributes);
            xhtml.endElement("div");

            entrydata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, name);
        }//  w ww. jav a 2s.  c  o  m
        if (extractor.shouldParseEmbedded(entrydata)) {
            // For detectors to work, we need a mark/reset supporting
            // InputStream, which ArchiveInputStream isn't, so wrap
            TemporaryResources tmp = new TemporaryResources();
            try {
                TikaInputStream tis = TikaInputStream.get(archive, tmp);
                extractor.parseEmbedded(tis, xhtml, entrydata, true);
            } finally {
                tmp.dispose();
            }
        }
    } else if (name != null && name.length() > 0) {
        xhtml.element("p", name);
    }
}

From source file:com.qwazr.tools.ArchiverTool.java

public void extract(File sourceFile, File destDir) throws IOException, ArchiveException {
    final InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
    try {/*from w ww  .j  a  va  2 s.  co  m*/
        ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(is);
        try {
            ArchiveEntry entry;
            while ((entry = in.getNextEntry()) != null) {
                if (!in.canReadEntryData(entry))
                    continue;
                if (entry.isDirectory()) {
                    new File(destDir, entry.getName()).mkdir();
                    continue;
                }
                if (entry instanceof ZipArchiveEntry)
                    if (((ZipArchiveEntry) entry).isUnixSymlink())
                        continue;
                File destFile = new File(destDir, entry.getName());
                File parentDir = destFile.getParentFile();
                if (!parentDir.exists())
                    parentDir.mkdirs();
                IOUtils.copy(in, destFile);
            }
        } catch (IOException e) {
            throw new IOException("Unable to extract the archive: " + sourceFile.getPath(), e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (ArchiveException e) {
        throw new ArchiveException("Unable to extract the archive: " + sourceFile.getPath(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.igormaznitsa.mvngolang.utils.UnpackUtils.java

public static int unpackFileToFolder(@Nonnull final Log logger, @Nullable final String folder,
        @Nonnull final File archiveFile, @Nonnull final File destinationFolder, final boolean makeAllExecutable)
        throws IOException {
    final String normalizedName = archiveFile.getName().toLowerCase(Locale.ENGLISH);

    final ArchEntryGetter entryGetter;

    boolean modeZipFile = false;

    final ZipFile theZipFile;
    final ArchiveInputStream archInputStream;
    if (normalizedName.endsWith(".zip")) {
        logger.debug("Detected ZIP archive");

        modeZipFile = true;//from   w w  w . j  av  a 2  s.  c om

        theZipFile = new ZipFile(archiveFile);
        archInputStream = null;
        entryGetter = new ArchEntryGetter() {
            private final Enumeration<ZipArchiveEntry> iterator = theZipFile.getEntries();

            @Override
            @Nullable
            public ArchiveEntry getNextEntry() throws IOException {
                ArchiveEntry result = null;
                if (this.iterator.hasMoreElements()) {
                    result = this.iterator.nextElement();
                }
                return result;
            }
        };
    } else {
        theZipFile = null;
        final InputStream in = new BufferedInputStream(new FileInputStream(archiveFile));
        try {
            if (normalizedName.endsWith(".tar.gz")) {
                logger.debug("Detected TAR.GZ archive");
                archInputStream = new TarArchiveInputStream(new GZIPInputStream(in));

                entryGetter = new ArchEntryGetter() {
                    @Override
                    @Nullable
                    public ArchiveEntry getNextEntry() throws IOException {
                        return ((TarArchiveInputStream) archInputStream).getNextTarEntry();
                    }
                };

            } else {
                logger.debug("Detected OTHER archive");
                archInputStream = ARCHIVE_STREAM_FACTORY.createArchiveInputStream(in);
                logger.debug("Created archive stream : " + archInputStream.getClass().getName());

                entryGetter = new ArchEntryGetter() {
                    @Override
                    @Nullable
                    public ArchiveEntry getNextEntry() throws IOException {
                        return archInputStream.getNextEntry();
                    }
                };
            }

        } catch (ArchiveException ex) {
            IOUtils.closeQuietly(in);
            throw new IOException("Can't recognize or read archive file : " + archiveFile, ex);
        } catch (CantReadArchiveEntryException ex) {
            IOUtils.closeQuietly(in);
            throw new IOException("Can't read entry from archive file : " + archiveFile, ex);
        }
    }

    try {

        final String normalizedFolder = folder == null ? null : FilenameUtils.normalize(folder, true) + '/';

        int unpackedFilesCounter = 0;
        while (true) {
            final ArchiveEntry entry = entryGetter.getNextEntry();
            if (entry == null) {
                break;
            }
            final String normalizedPath = FilenameUtils.normalize(entry.getName(), true);

            logger.debug("Detected archive entry : " + normalizedPath);

            if (normalizedFolder == null || normalizedPath.startsWith(normalizedFolder)) {
                final File targetFile = new File(destinationFolder, normalizedFolder == null ? normalizedPath
                        : normalizedPath.substring(normalizedFolder.length()));
                if (entry.isDirectory()) {
                    logger.debug("Folder : " + normalizedPath);
                    if (!targetFile.exists() && !targetFile.mkdirs()) {
                        throw new IOException("Can't create folder " + targetFile);
                    }
                } else {
                    final File parent = targetFile.getParentFile();

                    if (parent != null && !parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("Can't create folder : " + parent);
                    }

                    final FileOutputStream fos = new FileOutputStream(targetFile);

                    try {
                        if (modeZipFile) {
                            logger.debug("Unpacking ZIP entry : " + normalizedPath);

                            final InputStream zipEntryInStream = theZipFile
                                    .getInputStream((ZipArchiveEntry) entry);
                            try {
                                if (IOUtils.copy(zipEntryInStream, fos) != entry.getSize()) {
                                    throw new IOException(
                                            "Can't unpack file, illegal unpacked length : " + entry.getName());
                                }
                            } finally {
                                IOUtils.closeQuietly(zipEntryInStream);
                            }
                        } else {
                            logger.debug("Unpacking archive entry : " + normalizedPath);

                            if (!archInputStream.canReadEntryData(entry)) {
                                throw new IOException("Can't read archive entry data : " + normalizedPath);
                            }
                            if (IOUtils.copy(archInputStream, fos) != entry.getSize()) {
                                throw new IOException(
                                        "Can't unpack file, illegal unpacked length : " + entry.getName());
                            }
                        }
                    } finally {
                        fos.close();
                    }

                    if (makeAllExecutable) {
                        try {
                            targetFile.setExecutable(true, true);
                        } catch (SecurityException ex) {
                            throw new IOException("Can't make file executable : " + targetFile, ex);
                        }
                    }
                    unpackedFilesCounter++;
                }
            } else {
                logger.debug("Archive entry " + normalizedPath + " ignored");
            }
        }
        return unpackedFilesCounter;
    } finally {
        IOUtils.closeQuietly(theZipFile);
        IOUtils.closeQuietly(archInputStream);
    }
}

From source file:org.apache.ant.compress.resources.CommonsCompressArchiveScanner.java

/**
 * Fills the file and directory maps with resources read from the
 * archive./*from   www.j a va2  s. c o  m*/
 *
 * @param src the archive to scan.
 * @param encoding encoding used to encode file names inside the archive.
 * @param fileEntries Map (name to resource) of non-directory
 * resources found inside the archive.
 * @param matchFileEntries Map (name to resource) of non-directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 * @param dirEntries Map (name to resource) of directory
 * resources found inside the archive.
 * @param matchDirEntries Map (name to resource) of directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 */
protected void fillMapsFromArchive(Resource src, String encoding, Map fileEntries, Map matchFileEntries,
        Map dirEntries, Map matchDirEntries) {
    ArchiveEntry entry = null;
    ArchiveInputStream ai = null;

    try {
        try {
            ai = StreamHelper.getInputStream(factory, src, encoding);
            if (ai == null) {
                ai = factory.getArchiveStream(new BufferedInputStream(src.getInputStream()), encoding);
            }
        } catch (IOException ex) {
            throw new BuildException("problem opening " + src, ex);
        }
        while ((entry = ai.getNextEntry()) != null) {
            if (skipUnreadable && !ai.canReadEntryData(entry)) {
                log(Messages.skippedIsUnreadable(entry));
                continue;
            }
            Resource r = builder.buildResource(src, encoding, entry);
            String name = entry.getName();
            if (entry.isDirectory()) {
                name = trimSeparator(name);
                dirEntries.put(name, r);
                if (match(name)) {
                    matchDirEntries.put(name, r);
                }
            } else {
                fileEntries.put(name, r);
                if (match(name)) {
                    matchFileEntries.put(name, r);
                }
            }
        }
    } catch (IOException ex) {
        throw new BuildException("problem reading " + src, ex);
    } finally {
        FileUtils.close(ai);
    }
}

From source file:org.apache.ant.compress.taskdefs.ExpandBase.java

private void expandArchiveStream(String name, ArchiveInputStream is, File dir) throws IOException {
    FileNameMapper mapper = getMapper();
    log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
    boolean empty = true;
    ArchiveEntry ent = null;/*  w w  w.jav  a2 s.c o m*/
    while ((ent = is.getNextEntry()) != null) {
        if (skipUnreadable && !is.canReadEntryData(ent)) {
            log(Messages.skippedIsUnreadable(ent));
            continue;
        }
        empty = false;
        log("extracting " + ent.getName(), Project.MSG_DEBUG);
        extractFile(FileUtils.getFileUtils(), null, dir, is, ent.getName(), ent.getLastModifiedDate(),
                ent.isDirectory(), mapper);
    }
    if (empty && getFailOnEmptyArchive()) {
        throw new BuildException("archive '" + name + "' is empty");
    }
    log("expand complete", Project.MSG_VERBOSE);
}

From source file:org.apache.tika.parser.pkg.PackageExtractor.java

/**
 * Parses the given stream as a package of multiple underlying files.
 * The package entries are parsed using the delegate parser instance.
 * It is not an error if the entry can not be parsed, in that case
 * just the entry name (if given) is emitted.
 *
 * @param archive package stream/* w w w.j  a va 2 s . c o  m*/
 * @param xhtml content handler
 * @throws IOException if an IO error occurs
 * @throws SAXException if a SAX error occurs
 */
public void unpack(ArchiveInputStream archive, XHTMLContentHandler xhtml) throws IOException, SAXException {
    try {
        ArchiveEntry entry = archive.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {
                String name = entry.getName();

                if (archive.canReadEntryData(entry)) {
                    Metadata entrydata = new Metadata();
                    if (name != null && name.length() > 0) {
                        entrydata.set(Metadata.RESOURCE_NAME_KEY, name);
                    }
                    if (extractor.shouldParseEmbedded(entrydata)) {
                        extractor.parseEmbedded(archive, xhtml, entrydata, true);
                    }
                } else if (name != null && name.length() > 0) {
                    xhtml.element("p", name);
                }
            }
            entry = archive.getNextEntry();
        }
    } finally {
        archive.close();
    }
}

From source file:org.apache.tika.parser.pkg.PackageParser.java

private void parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedDocumentExtractor extractor,
        Metadata parentMetadata, XHTMLContentHandler xhtml) throws SAXException, IOException, TikaException {
    String name = entry.getName();
    if (archive.canReadEntryData(entry)) {
        // Fetch the metadata on the entry contained in the archive
        Metadata entrydata = handleEntryMetadata(name, null, entry.getLastModifiedDate(), entry.getSize(),
                xhtml);/*from w  ww. j av  a2  s  .c  o m*/

        // Recurse into the entry if desired
        if (extractor.shouldParseEmbedded(entrydata)) {
            // For detectors to work, we need a mark/reset supporting
            // InputStream, which ArchiveInputStream isn't, so wrap
            TemporaryResources tmp = new TemporaryResources();
            try {
                TikaInputStream tis = TikaInputStream.get(archive, tmp);
                extractor.parseEmbedded(tis, xhtml, entrydata, true);
            } finally {
                tmp.dispose();
            }
        }
    } else {
        name = (name == null) ? "" : name;
        if (entry instanceof ZipArchiveEntry) {
            boolean usesEncryption = ((ZipArchiveEntry) entry).getGeneralPurposeBit().usesEncryption();
            if (usesEncryption) {
                EmbeddedDocumentUtil.recordEmbeddedStreamException(
                        new EncryptedDocumentException("stream (" + name + ") is encrypted"), parentMetadata);
            }
        } else {
            EmbeddedDocumentUtil.recordEmbeddedStreamException(
                    new TikaException("Can't read archive stream (" + name + ")"), parentMetadata);
        }
        if (name.length() > 0) {
            xhtml.element("p", name);
        }
    }
}

From source file:org.springframework.cloud.stream.app.tensorflow.util.ModelExtractor.java

/**
 * Traverses the Archive to find either an entry that matches the modelFileNameInArchive name (if not empty) or
 * and entry that ends in .pb if the modelFileNameInArchive is empty.
 *
 * @param modelFileNameInArchive Optional name of the archive entry that represents the frozen model file. If empty
 *                               the archive will be searched for the first entry that ends in .pb
 * @param archive Archive stream to be traversed
 * @return//from  w w w.  ja  v  a  2s.  com
 * @throws IOException
 */
private byte[] findInArchiveStream(String modelFileNameInArchive, ArchiveInputStream archive)
        throws IOException {
    ArchiveEntry entry;
    while ((entry = archive.getNextEntry()) != null) {
        //System.out.println(entry.getName() + " : " + entry.isDirectory());

        if (archive.canReadEntryData(entry) && !entry.isDirectory()) {
            if ((StringUtils.hasText(modelFileNameInArchive)
                    && entry.getName().endsWith(modelFileNameInArchive))
                    || (!StringUtils.hasText(modelFileNameInArchive)
                            && entry.getName().endsWith(this.frozenGraphFileExtension))) {
                return StreamUtils.copyToByteArray(archive);
            }
        }
    }
    throw new IllegalArgumentException("No model is found in the archive");
}