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:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * retrieves an entry from the package file (ZIP file)
 *
 * @param packageFile the sound package file
 * @param entryName the name of the entry in the ZIP file
 * @throws com.dreikraft.infactory.sound.SoundPackageException encapsulates
 * all low level (IO) exceptions//ww  w  .  j  av a 2  s  .co  m
 * @return the entry data as stream
 */
public static InputStream getPackageEntryStream(File packageFile, String entryName)
        throws SoundPackageException {
    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    InputStream keyIn = null;
    try {
        ZipFile packageZip = new ZipFile(packageFile);

        // get key from package
        ZipEntry keyEntry = packageZip.getEntry(LICENSE_KEY_ENTRY);
        keyIn = packageZip.getInputStream(keyEntry);
        Key key = CryptoUtil.unwrapKey(keyIn, PUBLIC_KEY_FILE);

        // read entry
        ZipEntry entry = packageZip.getEntry(entryName);
        return new ZipClosingInputStream(packageZip,
                CryptoUtil.decryptInput(packageZip.getInputStream(entry), key));
    } catch (ZipException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } catch (CryptoException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (keyIn != null) {
                keyIn.close();
            }
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

From source file:com.google.gdt.eclipse.designer.webkit.WebKitSupportWin32.java

private static boolean deployNeeded(ZipFile zipFile) {
    if (WEBKIT_DIR.exists()) {
        // check if changed
        try {/* w w w . j av  a  2s  . co m*/
            File versionFile = new File(WEBKIT_DIR, WEBKIT_VERSION_NAME);
            if (!versionFile.exists()) {
                return true;
            }
            String currentVersion = readString(new FileInputStream(versionFile));
            // new version
            String newVersion = readString(zipFile.getInputStream(zipFile.getEntry(WEBKIT_VERSION_NAME)));
            return !currentVersion.equals(newVersion);
        } catch (Throwable e) {
            // ignore, means deploy needed
        }
    }
    return true;
}

From source file:org.n52.geoar.newdata.PluginLoader.java

/**
 * Extracts and parses the geoar-plugin.xml plugin-descriptor to create and
 * fill a {@link PluginInfo} instance.// w w  w .j av a 2 s .c  o  m
 * 
 * @param pluginFile
 * @return
 */
private static PluginInfo readPluginInfoFromPlugin(File pluginFile) {
    try {
        ZipFile zipFile = new ZipFile(pluginFile);
        ZipEntry pluginDescriptorEntry = zipFile.getEntry("geoar-plugin.xml");
        if (pluginDescriptorEntry == null) {
            return null;
        }

        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(zipFile.getInputStream(pluginDescriptorEntry));
        // Find name
        String name = null;
        NodeList nodeList = document.getElementsByTagName("name");
        if (nodeList != null && nodeList.getLength() >= 1) {
            name = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a name");
        }

        // Find publisher
        String publisher = null;
        nodeList = document.getElementsByTagName("publisher");
        if (nodeList != null && nodeList.getLength() >= 1) {
            publisher = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a publisher");
        }

        // Find description
        String description = null;
        nodeList = document.getElementsByTagName("description");
        if (nodeList != null && nodeList.getLength() >= 1) {
            description = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a description");
        }

        // Find identifier
        String identifier = null;
        nodeList = document.getElementsByTagName("identifier");
        if (nodeList != null && nodeList.getLength() >= 1) {
            identifier = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify an identifier");
        }

        // Find version
        Long version = null;
        nodeList = document.getElementsByTagName("version");
        if (nodeList != null && nodeList.getLength() >= 1) {
            String versionString = "-" + nodeList.item(0).getTextContent();

            Matcher matcher = pluginVersionPattern.matcher(versionString);
            if (matcher.find() && matcher.group(1) != null) {
                try {
                    version = parseVersionNumber(matcher.group(1));
                } catch (NumberFormatException e) {
                    LOG.error("Plugin filename version invalid: " + matcher.group(1));
                }
            }
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a version");
        }

        if (identifier == null) {
            identifier = name;
        }

        return new PluginInfo(pluginFile, name, description, version, identifier, publisher);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.n52.geoar.codebase.resources.InfoResource.java

/**
 * Reads {@link PluginDescriptor} from {@link File} representing a GeoAR
 * plugin APK. Uses a {@link ZipInputStream} to find and read plugin
 * descriptor file.//from w w w  . j  a  v  a2s .c o  m
 * 
 * 
 * @param pluginFile
 * @return
 * @throws IOException
 */
private static PluginDescriptor readPluginDescriptorFromPluginFile(File pluginFile) throws IOException {
    ZipFile zipFile = new ZipFile(pluginFile);
    ZipEntry pluginDescriptorEntry = zipFile.getEntry(GEOAR_PLUGIN_XML_NAME);
    if (pluginDescriptorEntry == null) {
        return null;
    }
    return readPluginInfoFromDescriptorInputStream(zipFile.getInputStream(pluginDescriptorEntry));
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

private static ZipEntry getZipEntry(URL file, ZipFile zipFile) {
    String fileString = file.getFile();
    int exclamation = fileString.indexOf('!');
    String jarLocation = fileString.substring(exclamation + 2); // remove jar separator !/ 
    return zipFile.getEntry(jarLocation);
}

From source file:com.serotonin.m2m2.Main.java

private static Properties getProperties(ZipFile zip) throws IOException, Main.ModulePropertiesException {
    ZipEntry propFile = zip.getEntry("module.signed");
    if (propFile != null) {
        return getProperties(zip.getInputStream(propFile), true);
    }//  ww w.  j a v a  2s.  com
    propFile = zip.getEntry("module.properties");
    if (propFile == null) {
        throw new RuntimeException("module.properties not found in module zip file");
    }
    return getProperties(zip.getInputStream(propFile), false);
}

From source file:fll.web.admin.UploadSubjectiveData.java

/**
 * Save the data stored in file to the database and update the subjective
 * score totals.//from   w  ww . ja  v a 2 s.  co m
 * 
 * @param file the file to read the data from
 * @param connection the database connection to write to
 * @throws SAXException if there is an error parsing the document
 */
public static void saveSubjectiveData(final File file, final int currentTournament,
        final ChallengeDescription challengeDescription, final Connection connection,
        final ServletContext application) throws SQLException, IOException, ParseException, SAXException {
    if (LOGGER.isDebugEnabled()) {
        try {
            LOGGER.debug("Saving uploaded file to ");
            final String baseFilename = "subjective-upload_" + DATE_TIME_FORMAT.get().format(new Date());
            final String filename = application.getRealPath("/WEB-INF/" + baseFilename);

            final File copy = new File(filename);
            FileOutputStream output = null;
            FileInputStream input = null;
            try {
                input = new FileInputStream(file);
                output = new FileOutputStream(copy);
                IOUtils.copy(input, output);
            } finally {
                IOUtils.closeQuietly(input);
                IOUtils.closeQuietly(output);
            }
        } catch (final IOException e) {
            LOGGER.debug("Error creating copy of subjective datafile", e);
        }
    }
    ZipFile zipfile = null;
    Document scoreDocument = null;
    try {
        try {
            zipfile = new ZipFile(file);

            // read in score data
            final ZipEntry scoreZipEntry = zipfile.getEntry("score.xml");
            if (null == scoreZipEntry) {
                throw new RuntimeException("Zipfile does not contain score.xml as expected");
            }
            final InputStream scoreStream = zipfile.getInputStream(scoreZipEntry);
            scoreDocument = XMLUtils.parseXMLDocument(scoreStream);
            scoreStream.close();
            zipfile.close();

        } catch (final ZipException ze) {
            LOGGER.info("Subjective upload is not a zip file, trying as an XML file");

            // not a zip file, parse as just the XML file
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                scoreDocument = XMLUtils.parseXMLDocument(fis);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }

        if (null == scoreDocument) {
            throw new FLLRuntimeException(
                    "Cannot parse input as a compressed subjective data file or an uncompressed XML file");
        }

        saveSubjectiveData(scoreDocument, currentTournament, challengeDescription, connection);
    } finally {
        if (null != zipfile) {
            zipfile.close();
        }
    }
}

From source file:com.openAtlas.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {/*w  w w  .  j a  va  2  s  .  co  m*/
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = FileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.acdd.ext.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {//from  w ww . jav a 2  s  . c  o  m
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = ACDDFileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:password.pwm.http.servlet.resource.ResourceFileServlet.java

static FileResource resolveRequestedFile(final ServletContext servletContext, final String resourcePathUri,
        final ResourceServletConfiguration resourceServletConfiguration) throws PwmUnrecoverableException {
    // URL-decode the file name (might contain spaces and on) and prepare file object.
    String filename = StringUtil.urlDecode(resourcePathUri);

    // parse out the session key...
    if (filename.contains(";")) {
        filename = filename.substring(0, filename.indexOf(";"));
    }/*from w ww .j a  va  2 s  .  c  o m*/

    if (!filename.startsWith(RESOURCE_PATH)) {
        LOGGER.warn("illegal url request to " + filename);
        throw new PwmUnrecoverableException(
                new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "illegal url request"));
    }

    {
        final FileResource resource = handleWebjarURIs(servletContext, resourcePathUri);
        if (resource != null) {
            return resource;
        }
    }

    {// check files system zip files.
        final Map<String, ZipFile> zipResources = resourceServletConfiguration.getZipResources();
        for (final String path : zipResources.keySet()) {
            if (filename.startsWith(path)) {
                final String zipSubPath = filename.substring(path.length() + 1, filename.length());
                final ZipFile zipFile = zipResources.get(path);
                final ZipEntry zipEntry = zipFile.getEntry(zipSubPath);
                if (zipEntry != null) {
                    return new ZipFileResource(zipFile, zipEntry);
                }
            }
            if (filename.startsWith(zipResources.get(path).getName())) {
                LOGGER.warn("illegal url request to " + filename + " zip resource");
                throw new PwmUnrecoverableException(
                        new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "illegal url request"));
            }
        }
    }

    // convert to file.
    final String filePath = servletContext.getRealPath(filename);
    final File file = new File(filePath);

    // figure top-most path allowed by request
    final String parentDirectoryPath = servletContext.getRealPath(RESOURCE_PATH);
    final File parentDirectory = new File(parentDirectoryPath);

    FileResource fileSystemResource = null;
    { //verify the requested page is a child of the servlet resource path.
        int recursions = 0;
        File recurseFile = file.getParentFile();
        while (recurseFile != null && recursions < 100) {
            if (parentDirectory.equals(recurseFile)) {
                fileSystemResource = new RealFileResource(file);
                break;
            }
            recurseFile = recurseFile.getParentFile();
            recursions++;
        }
    }

    if (fileSystemResource == null) {
        LOGGER.warn("attempt to access file outside of servlet path " + file.getAbsolutePath());
        throw new PwmUnrecoverableException(
                new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "illegal file path request"));
    }

    if (!fileSystemResource.exists()) { // check custom (configuration defined) zip file bundles
        final Map<String, FileResource> customResources = resourceServletConfiguration.getCustomFileBundle();
        for (final String customFileName : customResources.keySet()) {
            final String testName = RESOURCE_PATH + "/" + customFileName;
            if (testName.equals(resourcePathUri)) {
                return customResources.get(customFileName);
            }
        }
    }

    return fileSystemResource;
}