Example usage for java.util.jar JarFile entries

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

Introduction

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

Prototype

public Enumeration<JarEntry> entries() 

Source Link

Document

Returns an enumeration of the jar file entries.

Usage

From source file:jp.co.tis.gsp.tools.dba.mojo.ImportSchemaMojo.java

/**
 * Jar?????/*from w ww  .  j  av a  2 s  . c  om*/
 * 
 * @param jar
 * @param destDir
 * @throws IOException
 */
private void extractJarAll(JarFile jar, String destDir) throws IOException {
    Enumeration<JarEntry> enumEntries = jar.entries();
    while (enumEntries.hasMoreElements()) {
        java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
        java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
        if (file.isDirectory()) {
            f.mkdir();
            continue;
        }
        java.io.InputStream is = jar.getInputStream(file);
        java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
        while (is.available() > 0) {
            fos.write(is.read());
        }
        fos.close();
        is.close();
    }
}

From source file:st.redline.classloader.SmalltalkSourceFinder.java

private List<Source> findSourceInInJar(String packagePath, String classPath) {
    List<Source> sources = new ArrayList<>();
    JarFile jarFile = tryCreateJarFile(classPath);
    for (Enumeration em1 = jarFile.entries(); em1.hasMoreElements();) {
        String entry = em1.nextElement().toString();
        int lastSlash = entry.lastIndexOf('/');
        int pathLength = packagePath.length();
        if (entry.startsWith(packagePath) && pathLength == lastSlash && entry.endsWith(SOURCE_EXTENSION))
            sources.add(sourceFactory.createFromJar(entry, classPath));
    }//from  w w  w . j a  va 2  s  .  c om
    return sources;
}

From source file:org.xwiki.webjars.internal.FilesystemResourceReferenceSerializer.java

private void copyResourceFromJAR(String resourcePrefix, String resourceName, String targetPrefix,
        FilesystemExportContext exportContext) throws IOException {
    // Note that we cannot use ClassLoader.getResource() to access the resource since the resourcePath may point
    // to a directory inside the JAR, and in this case we need to copy all resources inside that directory in the
    // JAR!/*from   ww  w. j a  va  2  s.  c o m*/

    String resourcePath = String.format("%s/%s", resourcePrefix, resourceName);
    JarFile jar = new JarFile(getJARFile(resourcePath));
    for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        if (entry.getName().startsWith(resourcePath) && !entry.isDirectory()) {
            // Copy the resource!
            // TODO: Won't this cause collisions if the same resource is available on several subwikis for example?
            String targetPath = targetPrefix + entry.getName().substring(resourcePrefix.length());
            File targetLocation = new File(exportContext.getExportDir(), targetPath);
            if (!targetLocation.exists()) {
                targetLocation.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(targetLocation);
                InputStream is = jar.getInputStream(entry);
                IOUtils.copy(is, fos);
                fos.close();
                is.close();
            }
        }
    }
    jar.close();
}

From source file:us.mn.state.health.lims.plugin.PluginLoader.java

private void loadPlugin(File pluginFile) {

    try {/*from  www.ja  v  a 2s  .  c o m*/
        JarFile jar = new JarFile(pluginFile);

        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (entry.getName().contains(".xml")) {
                boolean valid = loadFromXML(jar, entry);
                if (valid) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.xwiki.webjars.internal.FilesystemResourceReferenceCopier.java

void processCSS(String resourcePrefix, String resourceName, String targetPrefix,
        FilesystemExportContext exportContext) throws Exception {
    String resourcePath = String.format(CONCAT_PATH_FORMAT, resourcePrefix, resourceName);
    JarFile jar = new JarFile(getJARFile(resourcePath));
    try {/*from   www . j a v  a 2 s  .c o  m*/
        for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements();) {
            JarEntry entry = enumeration.nextElement();
            if (entry.getName().equals(resourcePath)) {
                // Read the CSS and look for url() entries...
                processCSSfile(resourcePrefix, targetPrefix, entry, jar, exportContext);
                break;
            }
        }
    } finally {
        jar.close();
    }
}

From source file:com.tobedevoured.solrsail.SolrConfig.java

/**
 * Install Solr Config t the local file system by extracting from the
 * SolrSail jar/*from   w  w  w  . j a  v a 2 s.c  o m*/
 * 
 * @param jar File
 * @throws IOException
 */
public void installFromJar(File jar) throws IOException {
    logger.info("Installing config from Jar to {}", this.getSolrHome());
    logger.debug("Opening Jar {}", jar.toString());

    JarFile jarFile = new JarFile(jar);

    for (Enumeration<JarEntry> enumeration = jarFile.entries(); enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();

        if (!entry.getName().equals("solr/") && entry.getName().startsWith("solr/")) {
            StringBuilder dest = new StringBuilder(getSolrHome()).append(File.separator)
                    .append(entry.getName().replaceFirst("solr/", ""));

            File file = new File(dest.toString());

            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                if (file.getParentFile() != null) {
                    file.getParentFile().mkdirs();
                }

                logger.debug("Copying {} to {}", entry.getName(), dest.toString());

                InputStream input = jarFile.getInputStream(entry);
                Writer writer = new FileWriter(file.getAbsoluteFile());
                IOUtils.copy(input, writer);

                input.close();

                writer.close();
            }
        }
    }
}

From source file:org.jahia.services.htmlvalidator.WAIValidatorTest.java

private String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException {
    URL dirURL = clazz.getResource(path);
    if (dirURL != null && dirURL.getProtocol().equals("file")) {
        /* A file path: easy enough */
        return new File(dirURL.toURI()).list();
    }//from   www .  ja  v a  2 s  .  c om

    if (dirURL == null) {
        /*
        * In case of a jar file, we can't actually find a directory.
        * Have to assume the same jar as clazz.
        */
        String me = clazz.getName().replace(".", "/") + ".class";
        dirURL = clazz.getClassLoader().getResource(me);
    }

    if (dirURL.getProtocol().equals("jar")) {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
        JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            if (name.startsWith(path)) { //filter according to the path
                result.add(name);
            }
        }
        return result.toArray(new String[result.size()]);
    }

    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}

From source file:net.erdfelt.android.sdkfido.local.JarListing.java

private final void loadJar(File jarfile) throws IOException {
    JarFile jar = null;

    try {//from  www  .  ja v  a 2 s . co  m
        String name;
        jar = new JarFile(jarfile);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            name = entry.getName();
            add(name);

            if (!entry.isDirectory() && name.endsWith(".class")) {
                classlist.add(name);
            }
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:com.plugin.excel.xsd.node.store.impl.XsdNodeParserManagerImpl.java

private List<String> getXsdPaths(String directoryLocation) throws IOException {

    List<String> files = new ArrayList<String>();

    Enumeration<URL> e = this.getClass().getClassLoader().getResources(directoryLocation);
    while (e.hasMoreElements()) {
        java.net.URL url = e.nextElement();
        java.net.JarURLConnection urlcon = (java.net.JarURLConnection) (url.openConnection());
        java.util.jar.JarFile jar = urlcon.getJarFile();
        {//from w  w  w. ja v  a 2  s. c o  m
            java.util.Enumeration<java.util.jar.JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String entry = entries.nextElement().getName();
                if (entry.startsWith(directoryLocation)) {
                    files.add(entry);
                }
            }
        }
    }

    return files;
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private static void readJarDirectoryEntries(URL location, String basePath, Set<String> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;
    jarfile = conn.getJarFile();//from  w  ww .ja  v  a  2  s.  c om

    Enumeration<JarEntry> entries = jarfile.entries();
    while (entries != null && entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();

        if (entry.isDirectory() && StringUtils.startsWith(name, basePath)) {
            resources.add(name);
        }
    }
}