Search in sources :

Example 96 with ZipInputStream

use of java.util.zip.ZipInputStream in project tika by apache.

the class EpubParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    // Because an EPub file is often made up of multiple XHTML files,
    //  we need explicit control over the start and end of the document
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    ContentHandler childHandler = new EmbeddedContentHandler(new BodyContentHandler(xhtml));
    ZipInputStream zip = new ZipInputStream(stream);
    ZipEntry entry = zip.getNextEntry();
    while (entry != null) {
        if (entry.getName().equals("mimetype")) {
            String type = IOUtils.toString(zip, UTF_8);
            //often has trailing new lines
            if (type != null) {
                type = type.trim();
            }
            metadata.set(Metadata.CONTENT_TYPE, type);
        } else if (entry.getName().equals("metadata.xml")) {
            meta.parse(zip, new DefaultHandler(), metadata, context);
        } else if (entry.getName().endsWith(".opf")) {
            meta.parse(zip, new DefaultHandler(), metadata, context);
        } else if (entry.getName().endsWith(".html") || entry.getName().endsWith(".xhtml")) {
            content.parse(zip, childHandler, metadata, context);
        }
        entry = zip.getNextEntry();
    }
    // Finish everything
    xhtml.endDocument();
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) ContentHandler(org.xml.sax.ContentHandler) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 97 with ZipInputStream

use of java.util.zip.ZipInputStream in project sling by apache.

the class PackageTransformer method checkForPackage.

/**
     * Check if the resource is a content package
     * @param resource The resource
     * @return {@code null} if not a content package, a result otherwise
     */
private TransformationResult[] checkForPackage(final RegisteredResource resource) {
    // first check if this is a zip archive
    try (final ZipInputStream zin = new ZipInputStream(new BufferedInputStream(resource.getInputStream()))) {
        if (zin.getNextEntry() == null) {
            return null;
        }
    } catch (final IOException ioe) {
        logger.debug("Unable to read resource.", ioe);
        return null;
    }
    Session session = null;
    JcrPackage pck = null;
    try {
        // create an admin session
        session = repository.loginAdministrative(null);
        final JcrPackageManager pckMgr = pkgSvc.getPackageManager(session);
        pck = pckMgr.upload(resource.getInputStream(), true, true);
        if (pck.isValid()) {
            final PackageId pid = pck.getDefinition().getId();
            final Map<String, Object> attrs = new HashMap<String, Object>();
            attrs.put(ATTR_PCK_ID, pid.toString());
            final TransformationResult tr = new TransformationResult();
            tr.setId(pid.getGroup() + ':' + pid.getName());
            tr.setResourceType(RESOURCE_TYPE);
            tr.setAttributes(attrs);
            // version
            final String version = pid.getVersionString();
            if (version.length() > 0) {
                tr.setVersion(new Version(cleanupVersion(version)));
            }
            return new TransformationResult[] { tr };
        }
    } catch (final Exception ioe) {
        logger.debug("Unable to check content package " + resource.getURL(), ioe);
    } finally {
        if (pck != null) {
            pck.close();
        }
        if (session != null) {
            session.logout();
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Version(org.osgi.framework.Version) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Session(javax.jcr.Session)

Example 98 with ZipInputStream

use of java.util.zip.ZipInputStream in project kotlin by JetBrains.

the class ClassEntry method addEntries.

/**
     * Given a classpath, add all the class files found within the directories and inside jar files
     */
private static void addEntries(@NonNull LintClient client, @NonNull List<ClassEntry> entries, @NonNull List<File> classPath) {
    for (File classPathEntry : classPath) {
        if (classPathEntry.getName().endsWith(DOT_JAR)) {
            //noinspection UnnecessaryLocalVariable
            File jarFile = classPathEntry;
            if (!jarFile.exists()) {
                continue;
            }
            ZipInputStream zis = null;
            try {
                FileInputStream fis = new FileInputStream(jarFile);
                try {
                    zis = new ZipInputStream(fis);
                    ZipEntry entry = zis.getNextEntry();
                    while (entry != null) {
                        String name = entry.getName();
                        if (name.endsWith(DOT_CLASS)) {
                            try {
                                byte[] bytes = ByteStreams.toByteArray(zis);
                                if (bytes != null) {
                                    File file = new File(entry.getName());
                                    entries.add(new ClassEntry(file, jarFile, jarFile, bytes));
                                }
                            } catch (Exception e) {
                                client.log(e, null);
                                continue;
                            }
                        }
                        entry = zis.getNextEntry();
                    }
                } finally {
                    Closeables.close(fis, true);
                }
            } catch (IOException e) {
                client.log(e, "Could not read jar file contents from %1$s", jarFile);
            } finally {
                try {
                    Closeables.close(zis, true);
                } catch (IOException e) {
                // cannot happen
                }
            }
        } else if (classPathEntry.isDirectory()) {
            //noinspection UnnecessaryLocalVariable
            File binDir = classPathEntry;
            List<File> classFiles = new ArrayList<File>();
            addClassFiles(binDir, classFiles);
            for (File file : classFiles) {
                try {
                    byte[] bytes = client.readBytes(file);
                    entries.add(new ClassEntry(file, null, /* jarFile*/
                    binDir, bytes));
                } catch (IOException e) {
                    client.log(e, null);
                }
            }
        } else {
            client.log(null, "Ignoring class path entry %1$s", classPathEntry);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 99 with ZipInputStream

use of java.util.zip.ZipInputStream in project android by JetBrains.

the class AndroidApkBuilder method collectDuplicateEntries.

@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
private static void collectDuplicateEntries(@NotNull String rootFile, @NotNull Set<String> entries, @NotNull Set<String> result) throws IOException {
    final JavaResourceFilter javaResourceFilter = new JavaResourceFilter();
    FileInputStream fis = null;
    ZipInputStream zis = null;
    try {
        fis = new FileInputStream(rootFile);
        zis = new ZipInputStream(fis);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String name = entry.getName();
                if (javaResourceFilter.checkEntry(name) && !entries.add(name)) {
                    result.add(name);
                }
                zis.closeEntry();
            }
        }
    } finally {
        if (zis != null) {
            zis.close();
        }
        if (fis != null) {
            fis.close();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) JavaResourceFilter(com.android.jarutils.JavaResourceFilter) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

Example 100 with ZipInputStream

use of java.util.zip.ZipInputStream in project graphhopper by graphhopper.

the class Unzipper method unzip.

/**
 * @param progressListener updates not in percentage but the number of bytes already read.
 */
public void unzip(InputStream fromIs, File toFolder, ProgressListener progressListener) throws IOException {
    if (!toFolder.exists())
        toFolder.mkdirs();
    long sumBytes = 0;
    ZipInputStream zis = new ZipInputStream(fromIs);
    try {
        ZipEntry ze = zis.getNextEntry();
        byte[] buffer = new byte[8 * 1024];
        while (ze != null) {
            if (ze.isDirectory()) {
                new File(toFolder, ze.getName()).mkdir();
            } else {
                double factor = 1;
                if (ze.getCompressedSize() > 0 && ze.getSize() > 0)
                    factor = (double) ze.getCompressedSize() / ze.getSize();
                File newFile = new File(toFolder, ze.getName());
                FileOutputStream fos = new FileOutputStream(newFile);
                try {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                        sumBytes += len * factor;
                        if (progressListener != null)
                            progressListener.update(sumBytes);
                    }
                } finally {
                    fos.close();
                }
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
    } finally {
        zis.close();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43