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:io.apiman.common.plugin.PluginClassLoader.java

/**
 * Searches the plugin artifact ZIP and all dependency ZIPs for a zip entry for
 * the given fully qualified class name.
 * @param className name of class/*from ww w .  j a  v  a2s . co m*/
 * @throws IOException if an I/O error has occurred
 */
protected InputStream findClassContent(String className) throws IOException {
    String primaryArtifactEntryName = "WEB-INF/classes/" + className.replace('.', '/') + ".class";
    String dependencyEntryName = className.replace('.', '/') + ".class";
    ZipEntry entry = this.pluginArtifactZip.getEntry(primaryArtifactEntryName);
    if (entry != null) {
        return this.pluginArtifactZip.getInputStream(entry);
    }
    for (ZipFile zipFile : this.dependencyZips) {
        entry = zipFile.getEntry(dependencyEntryName);
        if (entry != null) {
            return zipFile.getInputStream(entry);
        }
    }
    return null;
}

From source file:net.sf.zekr.engine.translation.TranslationData.java

/**
 * Verify the zip archive and close the zip file handle finally.
 * /*from  ww  w .j a  v a 2s.  c  o m*/
 * @return <code>true</code> if translation verified, <code>false</code> otherwise.
 * @throws IOException
 */
public boolean verify() throws IOException {
    ZipFile zf = new ZipFile(archiveFile);
    ZipEntry ze = zf.getEntry(file);
    if (ze == null) {
        logger.error("Load failed. No proper entry found in \"" + archiveFile.getName() + "\".");
        return false;
    }

    byte[] textBuf = new byte[(int) ze.getSize()];
    boolean result;
    result = verify(zf.getInputStream(ze), textBuf);
    zf.close();
    return result;
}

From source file:jp.co.cyberagent.jenkins.plugins.DeployStrategyAndroid.java

private String getStringFromManifest(final String name) {
    File tempApk = null;/*from  w w w  .j a v a  2s .com*/
    InputStream is = null;
    ZipFile zip = null;
    try {
        tempApk = File.createTempFile(getBuild().getId(), "nr-" + getBuild().getNumber());
        mApkFile.copyTo(new FileOutputStream(tempApk));
        zip = new ZipFile(tempApk);
        ZipEntry mft = zip.getEntry("AndroidManifest.xml");
        is = zip.getInputStream(mft);

        byte[] xml = new byte[is.available()];
        is.read(xml);

        String string = AndroidUtils.decompressXML(xml);
        int start = string.indexOf(name + "=\"") + name.length() + 2;
        int end = string.indexOf("\"", start);
        String version = string.substring(start, end);

        if (version.startsWith("resourceID 0x")) {
            int resId = Integer.parseInt(version.substring(13), 16);
            return getStringFromResource(tempApk, resId);
        } else {
            return version;
        }
    } catch (Exception e) {
        getLogger().println(TAG + "Error: " + e.getMessage());
    } finally {
        if (tempApk != null)
            tempApk.delete();
        if (zip != null)
            try {
                zip.close();
            } catch (IOException e) {
                getLogger().println(TAG + "Error: " + e.getMessage());
            }
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                getLogger().println(TAG + "Error: " + e.getMessage());
            }
    }
    return null;
}

From source file:com.ephesoft.dcma.util.FileUtils.java

/**
 * To get Input Stream from Zip.//from   ww  w . ja v a  2 s.c om
 * 
 * @param zipName {@link String}
 * @param fileName {@link String}
 * @return {@link InputStream}
 * @throws FileNotFoundException in case of error
 * @throws IOException in case of error
 */
public static InputStream getInputStreamFromZip(final String zipName, final String fileName)
        throws FileNotFoundException, IOException {
    ZipFile zipFile = new ZipFile(zipName + ZIP_FILE_EXT);
    // InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(fileName));
    return zipFile.getInputStream(zipFile.getEntry(fileName));
}

From source file:org.devproof.portal.core.module.theme.service.ThemeServiceImpl.java

@Override
public ValidationKey validateTheme(File themeArchive) {
    try {/*from  w ww.j  ava  2  s  .  c o m*/
        ZipFile zip = new ZipFile(themeArchive);
        ZipEntry entry = zip.getEntry("theme.properties");
        if (entry != null) {
            InputStream is = zip.getInputStream(entry);
            ThemeBean bean = getBeanFromInputStream("", is);
            if (StringUtils.isBlank(bean.getAuthor()) || StringUtils.isBlank(bean.getUrl())
                    || StringUtils.isBlank(bean.getPortalThemeVersion())
                    || StringUtils.isBlank(bean.getPortalVersion()) || StringUtils.isBlank(bean.getTheme())) {
                return ValidationKey.INVALID_DESCRIPTOR_FILE;
            } else {
                if (themeVersion.equals(bean.getPortalThemeVersion())) {
                    return ValidationKey.VALID;
                } else {
                    return ValidationKey.WRONG_VERSION;
                }
            }
        }
        return ValidationKey.MISSING_DESCRIPTOR_FILE;
    } catch (ZipException e) {
        logger.warn(themeArchive.toString() + " was not valid", e);
        return ValidationKey.NOT_A_JARFILE;
    } catch (IOException e) {
        logger.warn(themeArchive.toString() + " was not valid", e);
        return ValidationKey.NOT_A_JARFILE;
    }
}

From source file:pl.asie.modalyze.mcp.MCPDataManager.java

private void loadMappings(String version) throws IOException {
    File mappingClient = new File(MCP_DIR, version + "-client.map");
    File mappingServer = new File(MCP_DIR, version + "-server.map");
    if (mappingClient.exists() && mappingServer.exists()) {
        MAPPINGS.put(version + "-client", new HashSet<>(FileUtils.readLines(mappingClient, "UTF-8")));
        MAPPINGS.put(version + "-server", new HashSet<>(FileUtils.readLines(mappingServer, "UTF-8")));
    } else {/*from   w w w.j av a 2  s  .  c om*/
        File mcpFile = new File(MCP_DIR, MCP_VERSION_MAP.get(version).mcpFile);
        if (mcpFile.exists()) {
            ZipFile zipFile = new ZipFile(mcpFile);
            ZipEntry joinedSrgEntry = zipFile.getEntry("conf/joined.srg");
            if (joinedSrgEntry != null) {
                loadJoinedSrgMapping(version, zipFile, joinedSrgEntry);
            } else {
                ZipEntry clientSrgEntry = zipFile.getEntry("conf/client.srg");
                ZipEntry serverSrgEntry = zipFile.getEntry("conf/server.srg");
                if (clientSrgEntry != null && serverSrgEntry != null) {
                    loadSrgMapping(version + "-client", zipFile, clientSrgEntry);
                    loadSrgMapping(version + "-server", zipFile, serverSrgEntry);
                } else {
                    ZipEntry csvFields = zipFile.getEntry("conf/fields.csv");
                    ZipEntry csvMethods = zipFile.getEntry("conf/methods.csv");
                    if (csvFields != null && csvMethods != null) {
                        loadCsvMapping(version, zipFile, csvFields, csvMethods);
                    } else {
                        System.err.println("MCP file for Minecraft " + version + " (" + mcpFile.toString()
                                + ") stored in an unknown format!");
                        MAPPINGS.put(version + "-client", Collections.EMPTY_SET);
                        MAPPINGS.put(version + "-server", Collections.EMPTY_SET);
                    }
                }
            }

            if (MAPPINGS.get(version + "-client") != null) {
                FileUtils.writeLines(mappingClient, MAPPINGS.get(version + "-client"));
            }

            if (MAPPINGS.get(version + "-server") != null) {
                FileUtils.writeLines(mappingServer, MAPPINGS.get(version + "-server"));
            }
        } else {
            System.err
                    .println("MCP file for Minecraft " + version + " (" + mcpFile.toString() + ") not found!");
            MAPPINGS.put(version + "-client", Collections.EMPTY_SET);
            MAPPINGS.put(version + "-server", Collections.EMPTY_SET);
        }
    }
}

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

protected void extractCover(File f, Format format, String publicationID) {
    String destinationName = publicationID + "." + format.getName() + ".png";
    String entryName = format.getMetadatum("internalPathCover");

    if ((entryName == null) || (entryName.equals(""))) {
        format.addMetadatum("relativePathThumbnail", "");
        return;/*from  ww  w . java2  s  .co m*/
    }

    try {
        ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ);
        ZipEntry entry = zipFile.getEntry(entryName);
        File destFile = new File(this.thumbnailDirectoryPath, "orig-" + destinationName);
        String destinationPath = destFile.getAbsolutePath();

        BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
        int numberOfBytesRead;
        byte data[] = new byte[BUFFER_SIZE];

        FileOutputStream fos = new FileOutputStream(destFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);

        while ((numberOfBytesRead = is.read(data, 0, BUFFER_SIZE)) > -1) {
            dest.write(data, 0, numberOfBytesRead);
        }
        dest.flush();
        dest.close();
        is.close();
        fos.close();

        // create thumbnail
        FileInputStream fis = new FileInputStream(destinationPath);
        Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, this.thumbnailWidth, this.thumbnailHeight, false);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] imageData = baos.toByteArray();

        // write thumbnail to file 
        File destFile2 = new File(this.thumbnailDirectoryPath, destinationName);
        String destinationPath2 = destFile2.getAbsolutePath();
        FileOutputStream fos2 = new FileOutputStream(destFile2);
        fos2.write(imageData, 0, imageData.length);
        fos2.flush();
        fos2.close();
        baos.close();

        // close ZIP
        zipFile.close();

        // delete original cover
        destFile.delete();

        // set relativePathThumbnail
        format.addMetadatum("relativePathThumbnail", destinationName);

    } catch (Exception e) {
        // nop 
    }
}

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

private void verifyRequireJson(ZipFile zipInputStream, String artifactId, String version)
        throws IOException, ParseException {
    ZipEntry entry = zipInputStream.getEntry("META-INF/js/require.json");
    assertNotNull(entry);//from ww  w .jav  a 2  s  . c o m

    String jsonFile = IOUtils.toString(zipInputStream.getInputStream(entry), "UTF-8");

    JSONObject json = (JSONObject) parser.parse(jsonFile);

    assertTrue("dependency metadata exists", json.containsKey("requirejs-osgi-meta"));
    final JSONObject meta = (JSONObject) json.get("requirejs-osgi-meta");

    assertTrue("artifact info exists", meta.containsKey("artifacts"));
    final JSONObject artifactInfo = (JSONObject) meta.get("artifacts");

    assertTrue("artifact is " + artifactId, artifactInfo.containsKey(artifactId));
    final JSONObject versionInfo = (JSONObject) artifactInfo.get(artifactId);

    assertTrue("version is " + version, versionInfo.containsKey(version));
}

From source file:org.eclipse.tycho.nexus.internal.plugin.UnzipRepositoryPluginITCase.java

private String getTestData(String localArtifactPath, String testDataPrefix) throws ZipException, IOException {
    ZipFile zipFile = new ZipFile(testData().resolveFile(testDataPrefix + localArtifactPath));
    String expectedContent;/*from   w ww  . ja v a2  s  .  c  om*/
    try {
        ZipEntry entry = zipFile.getEntry(POM_PROPERTIES_PATH_IN_ZIP);
        expectedContent = IOUtils.toString(zipFile.getInputStream(entry));
    } finally {
        zipFile.close();
    }
    return expectedContent;
}

From source file:org.overlord.commons.osgi.vfs.VfsBundle.java

/**
 * Indexes the JAR file by getting a SHA1 hash of its MANIFEST.MF file.
 * @param entryFile// w  ww.  ja  va 2s.c o  m
 */
private void indexJar(File entryFile) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(entryFile);
        ZipEntry zipEntry = zipFile.getEntry("META-INF/MANIFEST.MF"); //$NON-NLS-1$
        if (zipEntry != null) {
            InputStream inputStream = zipFile.getInputStream(zipEntry);
            String hash = DigestUtils.shaHex(inputStream);
            index.put(hash, entryFile);
        }
    } catch (Exception e) {
        // Do nothing - invalid JAR file?
    } finally {
        try {
            if (zipFile != null)
                zipFile.close();
        } catch (IOException e) {
        }
    }
}