Example usage for java.util.zip ZipFile getEntry

List of usage examples for java.util.zip ZipFile getEntry

Introduction

In this page you can find the example usage for java.util.zip ZipFile getEntry.

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the zip file entry for the specified name, or null if not found.

Usage

From source file:org.mule.tools.maven.app.it.AbstractMavenIT.java

protected void assertZipDoesContain(File file, String... filenames) throws IOException {
    ZipFile zipFile = null;
    try {/*from  ww w . j  a  v  a  2 s .c  o m*/
        zipFile = new ZipFile(file);
        for (String name : filenames) {
            if (zipFile.getEntry(name) == null) {
                fail(file.getAbsolutePath() + " does not contain valid entry " + name);
            }
        }
    } finally {
        if (zipFile != null) {
            zipFile.close();
        }
    }
}

From source file:org.jboss.dashboard.ui.resources.GraphicElementPreview.java

protected void initDataStructures(File f) throws IOException {
    ZipFile zfile = new ZipFile(f);
    ZipEntry descriptorEntry = zfile.getEntry(getDescriptorFilename());
    if (descriptorEntry == null) {
        status = STATUS_MISSING_DESCRIPTOR;
        return;//from w  w  w  .  jav  a  2 s.  c  o m
    }
    Properties prop = new Properties();
    try {
        prop.load(zfile.getInputStream(descriptorEntry));
    } catch (IOException ioe) {
        log.warn("Error processing descriptor file. ", ioe);
        status = STATUS_DESCRIPTOR_CORRUPT;
        return;
    }
    description = new Properties();
    resources = new Properties();
    Enumeration properties = prop.propertyNames();
    while (properties.hasMoreElements()) {
        String propName = (String) properties.nextElement();
        if (propName.startsWith("name.")) {
            String lang = propName.substring(propName.lastIndexOf(".") + 1);
            setDescription(prop.getProperty(propName), lang);
            log.debug("Element preview name (" + lang + "): " + prop.getProperty(propName));
        } else if (propName.startsWith("resource.")) {
            String resourceName = propName.substring("resource.".length());
            String resourcePath = prop.getProperty(propName);
            resources.setProperty(resourceName, resourcePath);
        } else {
            log.warn("Unknown property in element " + propName);
        }
    }
    log.debug("Resources inside zip = " + resources);
    resourcesDeployed = new HashMap();
    for (Enumeration en = resources.propertyNames(); en.hasMoreElements();) {
        String resName = (String) en.nextElement();
        String resPath = resources.getProperty(resName);
        log.debug("Deploying property " + resName + "=" + resPath);
        ZipEntry resourceEntry = zfile.getEntry(resPath);
        if (resourceEntry != null) {
            resourcesDeployed.put(resName, toByteArray(zfile.getInputStream(resourceEntry)));
        }
    }
}

From source file:com.thoughtworks.go.util.ZipUtilTest.java

private void assertContent(File targetZipFile, String file, String expectedContent) throws IOException {
    ZipFile actualZip = new ZipFile(targetZipFile);
    ZipEntry entry = actualZip.getEntry(file);
    assertThat(entry).isNotNull();/*w ww  .  ja  v  a 2  s . c  om*/
    assertThat(IOUtils.toString(actualZip.getInputStream(entry), UTF_8)).isEqualTo(expectedContent);
}

From source file:it.readbeyond.minstrel.librarian.FormatHandlerCBZ.java

private List<ZipAsset> parseImagePlaylistEntry(String path, String playlistEntry) {
    List<ZipAsset> assets = new ArrayList<ZipAsset>();
    try {//from   www.ja  v a2  s .  c  o m
        ZipFile zipFile = new ZipFile(new File(path), ZipFile.OPEN_READ);
        ZipEntry ze = zipFile.getEntry(playlistEntry);
        String text = this.getZipEntryText(zipFile, ze);
        String[] lines = text.split("\n");

        // if we have lines 
        if (lines.length > 0) {
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i].trim();

                // do we have a file name? 
                if ((line.length() > 0) && (!line.startsWith("#"))) {
                    HashMap<String, String> meta = new HashMap<String, String>();

                    // duration
                    if (i + 1 < lines.length) {
                        String line2 = lines[i + 1].trim();
                        if ((line2.length() > 0) && (!line2.startsWith("#"))) {
                            i += 1;
                            meta.put("duration", line2);

                            // title 
                            if (i + 1 < lines.length) {
                                String line3 = lines[i + 1].trim();
                                if ((line3.length() > 0) && (!line3.startsWith("#"))) {
                                    i += 1;
                                    meta.put("title", line3);
                                }
                            }
                        }
                    }

                    // generate entry path
                    File w = new File(new File(playlistEntry).getParent(), line);
                    line = w.getAbsolutePath().substring(1);

                    // add asset
                    assets.add(new ZipAsset(line, meta));
                } else {
                    // either comment or blank line => continue
                }
            }
        }

        // close ZIP
        zipFile.close();
    } catch (Exception e) {
        // nothing
    }
    return assets;
}

From source file:org.mule.appkit.it.AbstractMavenIT.java

protected String contentsOfMuleConfigFromZipFile(File muleAppZipFile) throws Exception {
    ZipFile zipFile = null;
    InputStream muleConfigStream = null;
    try {//from   w ww.  j a v  a  2s.c om
        zipFile = new ZipFile(muleAppZipFile);

        ZipEntry muleConfigEntry = zipFile.getEntry("mule-config.xml");
        assertNotNull(muleConfigEntry);

        muleConfigStream = zipFile.getInputStream(muleConfigEntry);
        return IOUtil.toString(muleConfigStream);
    } finally {
        if (zipFile != null) {
            zipFile.close();
        }
        if (muleConfigStream != null) {
            muleConfigStream.close();
        }
    }
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

private void verifyNoBlueprint(ZipFile zipInputStream) throws IOException {
    ZipEntry entry = zipInputStream.getEntry("OSGI-INF/blueprint/blueprint.xml");
    assertNull(entry);/*from w  w w  .j  a  v a 2s . co m*/
}

From source file:org.apache.rave.provider.w3c.service.impl.WookieWidgetService.java

private Widget returnURLFromConfig(File wgtFile) {
    Widget widget = null;/*from   w w w  .  j a v a  2  s .  c  om*/
    try {
        final ZipFile zipFile = new ZipFile(wgtFile);
        ZipEntry entry = zipFile.getEntry("config.xml");
        InputStream input = zipFile.getInputStream(entry);
        BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
        try {
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.contains("id=")) {
                    String val = line.substring(line.indexOf("id=") + 4,
                            line.indexOf("\"", line.indexOf("id=") + 5));
                    widget = new W3CWidget();
                    widget.setUrl(val);
                    return widget;
                }
            }
        } finally {
            input.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    return widget;
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

private void verifyManifest(ZipFile zipInputStream) throws IOException {
    ZipEntry entry = zipInputStream.getEntry("META-INF/MANIFEST.MF");
    assertNotNull(entry);/*from  w  ww.  ja va 2 s .c  o  m*/
    Manifest manifest = new Manifest(zipInputStream.getInputStream(entry));
    assertTrue("Bundle-SymbolicName is not pentaho-webjars-",
            manifest.getMainAttributes().getValue("Bundle-SymbolicName").startsWith("pentaho-webjars-"));
}

From source file:com.google.appinventor.buildserver.util.AARLibrary.java

/**
 * Extracts the package name from the Android Archive without needing to unzip it to a location
 * in the file system/*from w w  w . j a  v  a2s . c o m*/
 *
 * @param zip the input stream reading from the Android Archive.
 * @return the package name declared in the archive's AndroidManifest.xml.
 * @throws IOException if reading the input stream fails.
 */
private String extractPackageName(ZipFile zip) throws IOException {
    ZipEntry entry = zip.getEntry("AndroidManifest.xml");
    if (entry == null) {
        throw new IllegalArgumentException(zip.getName() + " does not contain AndroidManifest.xml");
    }
    try {
        ZipEntryWrapper wrapper = new ZipEntryWrapper(zip.getInputStream(entry));
        // the following call will automatically close the input stream opened above
        return AndroidManifest.getPackage(wrapper);
    } catch (StreamException | XPathExpressionException e) {
        throw new IOException("Exception processing AndroidManifest.xml", e);
    }
}

From source file:com.thoughtworks.go.util.ZipUtilTest.java

@Test
void shouldPreserveFileTimestampWhileGeneratingTheZipFile() throws Exception {
    File file = temporaryFolder.newFile("foo.txt");
    file.setLastModified(1297989100000L); // Set this to any date in the past which is greater than the epoch
    File zip = zipUtil.zip(file, temporaryFolder.newFile("foo.zip"), Deflater.DEFAULT_COMPRESSION);

    ZipFile actualZip = new ZipFile(zip.getAbsolutePath());
    ZipEntry entry = actualZip.getEntry(file.getName());

    assertThat(entry.getTime()).isEqualTo(file.lastModified());
}