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:hudson.plugins.simpleupdatesite.HPI.java

public static HPI loadHPI(File file) throws IOException {
    JarFile jarFile = new JarFile(file);
    try {/*from  ww  w  .  java 2  s  .c  o m*/
        ZipEntry entry = jarFile.getEntry("META-INF/MANIFEST.MF");
        HPI hpi = new HPI();
        hpi.init(jarFile.getManifest().getMainAttributes(), entry.getTime());
        return hpi;
    } finally {
        jarFile.close();
    }
}

From source file:org.nuxeo.osgi.JarBundleFile.java

public static void extractNestedJar(JarFile file, String path, File dest) throws IOException {
    InputStream in = null;//from   www. ja  v  a  2s. com
    ZipEntry entry = file.getEntry(path);
    try {
        in = file.getInputStream(entry);
        FileUtils.copyToFile(in, dest);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:org.wisdom.maven.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   www.j a v  a  2 s .com
 * @return {@literal true} if it's a bundle, {@literal false} otherwise.
 */
public static boolean isWebJar(File file) {
    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) {
            LoggerFactory.getLogger(DependencyCopy.class)
                    .error("Cannot check if the file {} is a webjar, " + "cannot open it", file.getName(), e);
            return false;
        } finally {
            final JarFile finalJar = jar;
            IOUtils.closeQuietly(new Closeable() {
                @Override
                public void close() throws IOException {
                    if (finalJar != null) {
                        finalJar.close();
                    }
                }
            });
        }

        for (String lib : found) {
            LoggerFactory.getLogger(DependencyCopy.class).info("Web Library found in {} : {}", file.getName(),
                    lib);
        }

        return !found.isEmpty();
    }

    return false;
}

From source file:net.fabricmc.loader.FabricLoader.java

protected static ModInfo[] getJarMods(File f) {
    try {//  w ww  . ja va2s.co m
        JarFile jar = new JarFile(f);
        ZipEntry entry = jar.getEntry("mod.json");
        if (entry != null) {
            try (InputStream in = jar.getInputStream(entry)) {
                return getMods(in);
            }
        }

    } catch (Exception e) {
        LOGGER.error("Unable to load mod from %s", f.getName());
        e.printStackTrace();
    }

    return new ModInfo[0];
}

From source file:org.trianacode.taskgraph.util.ExtensionFinder.java

public static Map<Class, Set<Object>> getProviders(List<Class> providers, File file) {
    log.debug("searching for providers:" + file.getAbsolutePath());
    //        System.out.println("*** Looking for extensions in : " + file.getAbsolutePath());
    Map<Class, Set<Object>> ret = new HashMap<Class, Set<Object>>();
    if (file.isDirectory()) {
        File meta = new File(file, "META-INF");
        if (meta.exists()) {
            File services = new File(meta, "services");
            if (services.exists()) {
                for (Class provider : providers) {
                    File prov = new File(services, provider.getName());
                    if (prov.exists()) {

                        List<Object> impls = null;
                        try {
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(new FileInputStream(prov)));
                            String line;
                            impls = new ArrayList<Object>();
                            List<String> done = new ArrayList<String>();
                            while ((line = reader.readLine()) != null) {
                                log.debug("got next service provider:" + line);
                                log.debug("*** Found : " + line);
                                try {
                                    if (!done.contains(line)) {
                                        Class cls = ClassLoaders.forName(line);
                                        if (provider.isAssignableFrom(cls)) {
                                            Object p = cls.newInstance();
                                            impls.add(p);
                                        }
                                        done.add(line);
                                    }/*  w w w  .j  a v  a  2 s.  com*/
                                } catch (Exception e1) {
                                    log.debug("Exception thrown trying to load service provider class " + line
                                            + ":" + FileUtils.formatThrowable(e1));
                                }
                            }
                        } catch (IOException e) {
                            log.debug("error thrown while reading file:" + e.getMessage());
                        }
                        if (impls.size() > 0) {
                            Set<Object> exist = ret.get(provider);
                            if (exist == null) {
                                exist = new HashSet<Object>();
                            }
                            exist.addAll(impls);
                            ret.put(provider, exist);
                        }
                    }
                }
            }
        }
        File[] children = file.listFiles(new FilenameFilter() {
            public boolean accept(File file, String s) {
                if (s.endsWith(".jar")) {
                    return true;
                }
                return false;
            }
        });
        if (children != null) {
            for (File child : children) {
                Map<Class, Set<Object>> map = getProviders(providers, child);
                for (Class aClass : map.keySet()) {
                    Set<Object> objs = ret.get(aClass);
                    if (objs == null) {
                        ret.put(aClass, map.get(aClass));
                    } else {
                        objs.addAll(map.get(aClass));
                        ret.put(aClass, objs);
                    }
                }
            }
        }
    } else {
        if (file.getName().endsWith(".jar")) {
            try {
                JarFile jf = new JarFile(file);
                ZipEntry entry = jf.getEntry("META-INF/services/");
                if (entry != null) {
                    for (Class provider : providers) {
                        ZipEntry e = jf.getEntry("META-INF/services/" + provider.getName());
                        if (e != null) {
                            InputStream zin = jf.getInputStream(e);
                            BufferedReader reader = new BufferedReader(new InputStreamReader(zin));
                            String line;
                            List<Object> impls = new ArrayList<Object>();
                            List<String> done = new ArrayList<String>();
                            while ((line = reader.readLine()) != null) {
                                log.debug("got next service provider:" + line);
                                // check if the class is in this jar
                                ZipEntry sp = jf.getEntry(line.replace(".", "/") + ".class");
                                if (sp != null) {
                                    try {
                                        if (!done.contains(line)) {
                                            Class cls = ClassLoaders.forName(line);
                                            if (provider.isAssignableFrom(cls)) {
                                                Object prov = cls.newInstance();
                                                impls.add(prov);
                                            }
                                            done.add(line);
                                        }
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                        log.debug("Exception thrown trying to load service provider class "
                                                + line, e1);
                                    }
                                }
                            }
                            if (impls.size() > 0) {
                                Set<Object> exist = ret.get(provider);
                                if (exist == null) {
                                    exist = new HashSet<Object>();
                                }
                                exist.addAll(impls);
                                ret.put(provider, exist);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                log.debug("Exception thrown trying to load service providers from file " + file, e);
            }
        }

    }
    return ret;
}

From source file:org.apache.openejb.config.Deploy.java

private static boolean shouldUnpack(final File file) {
    final String name = file.getName();
    if (name.endsWith(".ear") || name.endsWith(".rar") || name.endsWith(".rar")) {
        return true;
    }/*from   w  ww  .  j  a v  a  2  s . c om*/

    JarFile jarFile = null;
    try {
        jarFile = new JarFile(file);

        if (jarFile.getEntry("META-INF/application.xml") != null) {
            return true;
        }
        if (jarFile.getEntry("META-INF/ra.xml") != null) {
            return true;
        }
        if (jarFile.getEntry("WEB-INF/web.xml") != null) {
            return true;
        }
    } catch (final IOException e) {
        // no-op
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (final IOException ignored) {
                // no-op
            }
        }
    }

    return false;
}

From source file:org.wisdom.resources.WebJarDeployer.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  va2 s  .c  o m*/
 * @return the set of libraries found in the file, {@code null} if none.
 */
public static Set<DetectedWebJar> isWebJar(File file) {
    Set<DetectedWebJar> 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(WebJarController.WEBJAR_LOCATION) == null) {
                return null;
            }

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

        for (DetectedWebJar lib : found) {
            LOGGER.info("Web Library found in {} : {}", file.getName(), lib.id);
        }

        return found;
    }

    return null;
}

From source file:ClassFileUtilities.java

private static void computeClassDependencies(InputStream is, Set classpath, Set done, Set result, boolean rec)
        throws IOException {

    Iterator it = getClassDependencies(is).iterator();
    while (it.hasNext()) {
        String s = (String) it.next();
        if (!done.contains(s)) {
            done.add(s);//from www. j  ava 2 s  .com

            Iterator cpit = classpath.iterator();
            while (cpit.hasNext()) {
                InputStream depis = null;
                String path = null;
                Object cpEntry = cpit.next();
                if (cpEntry instanceof JarFile) {
                    JarFile jarFile = (JarFile) cpEntry;
                    String classFileName = s + ".class";
                    ZipEntry ze = jarFile.getEntry(classFileName);
                    if (ze != null) {
                        path = jarFile.getName() + '!' + classFileName;
                        depis = jarFile.getInputStream(ze);
                    }
                } else {
                    path = ((String) cpEntry) + '/' + s + ".class";
                    File f = new File(path);
                    if (f.isFile()) {
                        depis = new FileInputStream(f);
                    }
                }

                if (depis != null) {
                    result.add(path);

                    if (rec) {
                        computeClassDependencies(depis, classpath, done, result, rec);
                    }
                }
            }
        }
    }
}

From source file:org.colombbus.tangara.Main.java

public static void copyFilesInTempDirectory() {
    try {//w  ww.j  av  a2s  .com
        // Creation of a temp directory
        tempDirectory = FileUtils.createTempDirectory();
        Configuration conf = Configuration.instance();
        StringTokenizer resources = new StringTokenizer(conf.getProperty("program.resources"), ",");
        String resource = null;

        JarFile jarFile = new JarFile(conf.getTangaraPath());

        while (resources.hasMoreTokens()) {
            resource = resources.nextToken();
            ZipEntry entry = jarFile.getEntry(RESOURCES_DIRECTORY + resource);
            if (entry == null) {
                jarFile.close();
                throw new Exception("Resource '" + resource + "' not found");
            }
            BufferedInputStream input = new BufferedInputStream(jarFile.getInputStream(entry));
            File destinationFile = new File(tempDirectory, resource);
            destinationFile.createNewFile();
            FileUtils.copyFile(input, destinationFile);
        }
    } catch (Exception e) {
        LOG.error("error while copying program files: ", e);
    }
}

From source file:itdelatrisu.opsu.Utils.java

public static void unpackFromJar(@NotNull JarFile jarfile, @NotNull File unpackedFile, @NotNull String filename)
        throws IOException {
    InputStream in = jarfile.getInputStream(jarfile.getEntry(filename));
    OutputStream out = new FileOutputStream(unpackedFile);

    byte[] buffer = new byte[65536];
    int bufferSize;
    while ((bufferSize = in.read(buffer, 0, buffer.length)) != -1) {
        out.write(buffer, 0, bufferSize);
    }//from  w  w  w  .  j a  v  a 2s .  co  m

    in.close();
    out.close();
}