Search in sources :

Example 41 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.

the class ZipWriter method zipStoreBuffer.

private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException {
    ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
    zipEntry.setMethod(ZipOutputStream.STORED);
    zipEntry.setSize(dataBuffer.length);
    CRC32 crc32 = new CRC32();
    crc32.update(dataBuffer);
    zipEntry.setCrc(crc32.getValue());
    try {
        zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
    } catch (ZipException ex) {
        if (name != null) {
            zipStoreBuffer(zip, "x-" + name, dataBuffer);
            return;
        }
    }
    zip.write(dataBuffer);
    zip.closeArchiveEntry();
}
Also used : CRC32(java.util.zip.CRC32) ZipEntry(java.util.zip.ZipEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipException(java.util.zip.ZipException)

Example 42 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tomee by apache.

the class BuildTomEEMojo method zip.

private void zip(final ZipArchiveOutputStream zip, final File f, final String prefix) throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final ZipArchiveEntry archiveEntry = new ZipArchiveEntry(f, path);
    if (isSh(path)) {
        archiveEntry.setUnixMode(0755);
    }
    zip.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        zip.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                zip(zip, child, prefix);
            }
        }
    } else {
        IO.copy(f, zip);
        zip.closeArchiveEntry();
    }
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) File(java.io.File)

Example 43 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project stanbol by apache.

the class ResourceLoader method loadResource.

/**
     * Loads a resource from a file
     * @param file the file resource
     */
private void loadResource(String file) {
    synchronized (files) {
        //sync to files to avoid two threads loading the same file
        ResourceState state = files.get(file);
        if (state == null || state != ResourceState.REGISTERED) {
            log.info("Do not load File {} because of its state {} (null means removed from list)", file, state);
            //someone removed it in between
            return;
        } else {
            //set to loading
            setResourceState(file, ResourceState.LOADING, null);
        }
    }
    long startFile = System.currentTimeMillis();
    log.info(" > loading '{}' ...", file);
    String extension = FilenameUtils.getExtension(file);
    if (loadEntriesWithinZipArchives && ("zip".equalsIgnoreCase(extension) || "jar".equalsIgnoreCase(extension))) {
        log.info("  - processing {}-archive entries:", extension);
        ZipFile zipArchive;
        try {
            zipArchive = new ZipFile(file);
        } catch (IOException e) {
            zipArchive = null;
            setResourceState(file, ResourceState.ERROR, e);
        }
        if (zipArchive != null) {
            boolean isError = false;
            Enumeration<ZipArchiveEntry> entries = zipArchive.getEntries();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    String entryName = entry.getName();
                    log.info("     o loading entry '{}'", entryName);
                    try {
                        ResourceState state = resourceImporter.importResource(zipArchive.getInputStream(entry), FilenameUtils.getName(entryName));
                        if (state == ResourceState.ERROR) {
                            isError = true;
                        }
                    } catch (IOException e) {
                        isError = true;
                    }
                }
            }
            //set the state for the Archive as a whole
            setResourceState(file, isError ? ResourceState.ERROR : ResourceState.LOADED, null);
        }
    } else {
        InputStream is;
        try {
            is = new FileInputStream(file);
            ResourceState state = resourceImporter.importResource(is, FilenameUtils.getName(file));
            setResourceState(file, state, null);
        } catch (FileNotFoundException e) {
            //during init it is checked that files exists and are files 
            //and there is read access so this can only happen if
            //someone deletes the file in between
            log.warn("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        } catch (IOException e) {
            log.error("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        } catch (Exception e) {
            log.error("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        }
    }
    log.info("   - completed in {} seconds", (System.currentTimeMillis() - startFile) / 1000);
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 44 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.

the class PackageParser method parseEntry.

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);
        // 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);
        }
    }
}
Also used : EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaException(org.apache.tika.exception.TikaException) Metadata(org.apache.tika.metadata.Metadata) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 45 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.

the class ZipContainerDetector method detectIpa.

@SuppressWarnings("unchecked")
private static MediaType detectIpa(ZipFile zip) {
    // Note - consider generalising this logic, if another format needs many regexp matching
    Set<Pattern> tmpPatterns = (Set<Pattern>) ipaEntryPatterns.clone();
    Enumeration<ZipArchiveEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        String name = entry.getName();
        Iterator<Pattern> ip = tmpPatterns.iterator();
        while (ip.hasNext()) {
            if (ip.next().matcher(name).matches()) {
                ip.remove();
            }
        }
        if (tmpPatterns.isEmpty()) {
            // We've found everything we need to find
            return MediaType.application("x-itunes-ipa");
        }
    }
    // If we get here, not all required entries were found
    return null;
}
Also used : Pattern(java.util.regex.Pattern) HashSet(java.util.HashSet) Set(java.util.Set) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Aggregations

ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)46 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)21 IOException (java.io.IOException)13 File (java.io.File)12 FileInputStream (java.io.FileInputStream)10 InputStream (java.io.InputStream)10 Path (java.nio.file.Path)10 Test (org.junit.Test)8 BufferedInputStream (java.io.BufferedInputStream)7 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)7 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)6 FileOutputStream (java.io.FileOutputStream)5 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 ImageInfo (com.github.hmdev.info.ImageInfo)4 SectionInfo (com.github.hmdev.info.SectionInfo)4 BufferedWriter (java.io.BufferedWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)4