Example usage for java.util.jar JarFile getEntry

List of usage examples for java.util.jar JarFile getEntry

Introduction

In this page you can find the example usage for java.util.jar JarFile getEntry.

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the ZipEntry for the given base entry name or null if not found.

Usage

From source file:org.mousephenotype.dcc.utils.io.jar.JARUtils.java

public static InputStream getInputStreamFromJar(URL jarURL, String entryName) throws IOException {
    JarFile jarfile = new JarFile(jarURL.getFile());
    ZipEntry entry = jarfile.getEntry(entryName);
    return jarfile.getInputStream(entry);
}

From source file:org.mousephenotype.dcc.utils.io.jar.JARUtils.java

public static boolean fileExistsOnJar(URL jarURL, String entryName) throws IOException {
    JarFile jarfile = new JarFile(jarURL.getFile());
    ZipEntry entry = jarfile.getEntry(entryName);
    if (entry == null) {
        return false;
    } else {//from   w w w . j  ava 2 s.c o  m
        return true;
    }
}

From source file:org.superfuntime.chatty.Core.java

@SuppressWarnings("unchecked")
private static void loadBots() {
    logger.info("Loading available bots");
    final Collection<File> botJars = FileUtils.listFiles(botsDirecotry, new String[] { "jar" }, false);
    for (File botJar : botJars) {
        try {/*from  w w w  . j  a  v  a2 s .c  o  m*/
            JarFile jar = new JarFile(botJar);
            InputStream is = jar.getInputStream(jar.getEntry("bot.yml"));
            YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true);
            String mainClass = n.getString("mainClass");
            String type = n.getString("type");
            URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() },
                    Core.class.getClassLoader());
            BOT_INFO.put(type, new BotClassInfo(loader.loadClass(mainClass).asSubclass(Bot.class), n));
            logger.info("Loaded bot '{}'", type);
        } catch (Exception e) {
            logger.error("Failed to load bot from {}", botJar.getName());
            e.printStackTrace();
        }
    }
    logger.info("Loaded {} bot(s)", BOT_INFO.size());
}

From source file:org.superfuntime.chatty.Core.java

@SuppressWarnings("unchecked")
private static void startPlugins() {
    logger.info("Loading available plugins");
    final Collection<File> botJars = FileUtils.listFiles(pluginsDirecotry, new String[] { "jar" }, false);
    for (File botJar : botJars) {
        try {//from  ww  w . j  av a  2s . c  o  m
            JarFile jar = new JarFile(botJar);
            InputStream is = jar.getInputStream(jar.getEntry("plugin.yml"));
            YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true);
            String mainClass = n.getString("mainClass");
            String name = n.getString("name");
            URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() },
                    Core.class.getClassLoader());
            Plugin plugin = loader.loadClass(mainClass).asSubclass(Plugin.class).newInstance();
            plugin.start();
            PLUGIN_INFO.put(name, new PluginInfo(name, plugin, n));
            logger.info("Loaded plugin '{}'", name);
        } catch (Exception e) {
            logger.error("Failed to load plugin from {}", botJar.getName());
            e.printStackTrace();
        }
    }
    logger.info("Loaded {} plugin(s)", PLUGIN_INFO.size());
}

From source file:org.owasp.dependencycheck.xml.pom.PomUtils.java

/**
 * Retrieves the specified POM from a jar file and converts it to a Model.
 *
 * @param path the path to the pom.xml file within the jar file
 * @param jar the jar file to extract the pom from
 * @return returns an object representation of the POM
 * @throws AnalysisException is thrown if there is an exception extracting
 * or parsing the POM {@link Model} object
 *//*from  w  w  w .j  ava2  s.c o m*/
public static Model readPom(String path, JarFile jar) throws AnalysisException {
    final ZipEntry entry = jar.getEntry(path);
    Model model = null;
    if (entry != null) { //should never be null
        //noinspection CaughtExceptionImmediatelyRethrown
        try {
            final PomParser parser = new PomParser();
            model = parser.parse(jar.getInputStream(entry));
            if (model == null) {
                throw new AnalysisException(String.format("Unable to parse pom '%s/%s'", jar.getName(), path));
            }
        } catch (AnalysisException ex) {
            throw ex;
        } catch (SecurityException ex) {
            LOGGER.warn("Unable to parse pom '{}' in jar '{}'; invalid signature", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        } catch (IOException ex) {
            LOGGER.warn("Unable to parse pom '{}' in jar '{}' (IO Exception)", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        } catch (Throwable ex) {
            LOGGER.warn("Unexpected error during parsing of the pom '{}' in jar '{}'", path, jar.getName());
            LOGGER.debug("", ex);
            throw new AnalysisException(ex);
        }
    }
    return model;
}

From source file:org.ebayopensource.turmeric.eclipse.core.Activator.java

/**
 * Old type library jar(NOT in workspace) has the dir structure \types\<xsd>
 * and the new one has meta-src\types\<typeLibName>\<xsd>
 * //from   w ww. j  av a  2s . co  m
 * @return
 * @throws IOException
 * @throws URISyntaxException
 */
public static boolean isNewTypLibrary(URL jarURL, String projectName) throws IOException {
    File file = new File(jarURL.getPath());
    JarFile jarFile;
    jarFile = new JarFile(file);
    return jarFile.getEntry(TYPES_LOCATION_IN_JAR + WorkspaceUtil.PATH_SEPERATOR + projectName) != null;

}

From source file:org.sonar.updatecenter.deprecated.Plugin.java

public static Plugin extractMetadata(File file) throws IOException {
    JarFile jar = new JarFile(file);
    ZipEntry entry = jar.getEntry("META-INF/MANIFEST.MF");
    long timestamp = entry.getTime();
    Manifest manifest = jar.getManifest();
    jar.close();/*from w ww.  j a v  a 2 s  .  c o  m*/

    Attributes attributes = manifest.getMainAttributes();
    String pluginKey = attributes.getValue("Plugin-Key");
    Plugin plugin = new Plugin(pluginKey);
    plugin.setName(attributes.getValue("Plugin-Name"));
    plugin.setVersion(attributes.getValue("Plugin-Version"));
    plugin.setRequiredSonarVersion(attributes.getValue("Sonar-Version"));
    plugin.setHomepage(attributes.getValue("Plugin-Homepage"));
    plugin.setDate(timestamp);
    return plugin;
}

From source file:org.jboss.tools.central.internal.ImageUtil.java

/**
 * Creates an image from an {@link URL}.
 * If the iconUrl points at a jar file, the created image doesn't not leak file handle. 
 *//*from w w  w  .j a  va  2  s . c o  m*/
public static Image createImageFromUrl(Device device, URL iconUrl) {

    if (!iconUrl.getProtocol().equals("jar")) {
        ImageDescriptor descriptor = ImageDescriptor.createFromURL(iconUrl);
        return descriptor.createImage();
    }

    //Load from jar:
    Image image = null;
    try {
        String fileName = iconUrl.getFile();
        if (fileName.contains("!")) {
            String[] location = fileName.split("!");
            fileName = location[0];
            String imageName = URLDecoder.decode(location[1].substring(1), "utf-8");
            File file = new File(new URI(fileName));
            JarFile jarFile = null;
            InputStream inputStream = null;
            try {
                jarFile = new JarFile(file);
                ZipEntry imageEntry = jarFile.getEntry(imageName);
                if (imageEntry != null) {
                    inputStream = jarFile.getInputStream(imageEntry);
                    image = new Image(device, inputStream);
                }
            } finally {
                IOUtils.closeQuietly(inputStream);
                try {
                    if (jarFile != null) {
                        jarFile.close();
                    }
                } catch (Exception e) {
                    //ignore
                }
            }
        }
    } catch (Exception e) {
        JBossCentralActivator.log(e);
    }

    return image;
}

From source file:Main.java

static boolean isTheUpdateForMe(File path) {

    JarFile jar;
    try {//from   ww  w. j  a va2 s  . c o  m
        jar = new JarFile(path);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return false;
    }

    ZipEntry entry = jar.getEntry("system/build.prop");
    final String myDevice = "ro.product.device=" + Build.DEVICE;
    boolean finded = false;

    if (entry != null) {
        try {
            InputStreamReader bi = new InputStreamReader(jar.getInputStream(entry));

            BufferedReader br = new BufferedReader(bi);

            String line;
            Pattern p = Pattern.compile(myDevice);
            do {
                line = br.readLine();
                if (line == null) {
                    break;
                }

                Matcher m = p.matcher(line);
                if (m.find()) {
                    finded = true;
                    break;
                }
            } while (true);

            bi.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    try {
        jar.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return finded;
}

From source file:io.fabric8.vertx.maven.plugin.utils.WebJars.java

/**
 * Checks whether the given file is a WebJar or not (http://www.webjars.org/documentation).
 * The check is based on the presence of {@literal META-INF/resources/webjars/} directory in the jar file.
 *
 * @param file the file./*from  w w  w  . j a va2s .co m*/
 * @return {@literal true} if it's a bundle, {@literal false} otherwise.
 */
public static boolean isWebJar(Log log, File file) {
    if (file == null) {
        return false;
    }
    Set<String> found = new LinkedHashSet<>();
    if (file.isFile() && file.getName().endsWith(".jar")) {
        JarFile jar = null;
        try {
            jar = new JarFile(file);

            // Fast return if the base structure is not there
            if (jar.getEntry(WEBJAR_LOCATION) == null) {
                return false;
            }

            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                Matcher matcher = WEBJAR_REGEX.matcher(entry.getName());
                if (matcher.matches()) {
                    found.add(matcher.group(1) + "-" + matcher.group(2));
                }
            }
        } catch (IOException e) {
            log.error("Cannot check if the file " + file.getName() + " is a webjar, cannot open it", e);
            return false;
        } finally {
            final JarFile finalJar = jar;
            IOUtils.closeQuietly(() -> {
                if (finalJar != null) {
                    finalJar.close();
                }
            });
        }

        for (String lib : found) {
            log.info("Web Library found in " + file.getName() + " : " + lib);
        }

        return !found.isEmpty();
    }

    return false;
}