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:org.xwiki.webjars.internal.FilesystemResourceReferenceCopier.java

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  .  ja  v  a  2s.  c o  m*/

    String resourcePath = String.format(CONCAT_PATH_FORMAT, resourcePrefix, resourceName);
    JarFile jar = new JarFile(getJARFile(resourcePath));
    try {
        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();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:com.fengduo.bee.commons.core.utilities.ClassPathScanner.java

void findInJar(List<Class<?>> results, File file, String packageName) {
    JarFile jarFile = null;
    String packagePath = dotToPath(packageName) + "/";
    try {//from  ww w .  j  a va 2s  .  c o m
        jarFile = new JarFile(file);
        Enumeration<JarEntry> en = jarFile.entries();
        while (en.hasMoreElements()) {
            JarEntry je = en.nextElement();
            String name = je.getName();
            if (name.startsWith(packagePath) && name.endsWith(CLASS_FILE)) {
                String className = name.substring(0, name.length() - CLASS_FILE.length());
                add(results, pathToDot(className));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.apache.hadoop.chukwa.hicc.HiccWebServer.java

public List<String> getResourceListing(String path) throws URISyntaxException, IOException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URL dirURL = contextClassLoader.getResource(path);

    if (dirURL == null) {
        dirURL = contextClassLoader.getResource(path);
    }//w  w w.  j a  v a2 s . c  o m

    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(jarPath);
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        List<String> result = new ArrayList<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
                String entry = name.substring(path.length());
                int checkSubdir = entry.indexOf("/");
                if (checkSubdir == 0 && entry.length() > 1) {
                    // if it is a subdirectory, we just return the directory name
                    result.add(name);
                }
            }
        }
        return result;
    }

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

From source file:com.netease.hearttouch.hthotfix.HashTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    logger.info("HASHTRANSFORMJAR\t" + outputFile.getAbsolutePath());
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            //                new File(outFile, entry.getName()).mkdirs();
            continue;
        }/*from w w  w.  j a v  a2 s.c  om*/
        final InputStream is = jar.getInputStream(entry);

        try {
            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (!extension.getGeneratePatch()) {
                    hashFileGenerator.addClass(bytecode);
                    ClassReader cr = new ClassReader(bytecode);

                    outputJar.writeEntry(entry, hackInjector.inject(cr.getClassName(), bytecode));
                } else {
                    outputJar.writeEntry(entry, bytecode);
                }
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:es.jamisoft.comun.utils.compression.Jar.java

public void descomprimir(String lsDirDestino) {
    try {// w ww.j a v a  2s .c  om
        JarFile lzfFichero = new JarFile(isFicheroJar);
        Enumeration lenum = lzfFichero.entries();
        JarArchiveEntry entrada = null;
        InputStream linput;

        for (; lenum.hasMoreElements(); linput.close()) {
            entrada = (JarArchiveEntry) lenum.nextElement();
            linput = lzfFichero.getInputStream(entrada);

            byte labBytes[] = new byte[2048];
            int liLeido = -1;
            String lsRutaDestino = lsDirDestino + File.separator + entrada.getName();

            lsRutaDestino = lsRutaDestino.replace('\\', File.separatorChar);

            File lfRutaCompleta = new File(lsRutaDestino);
            String lsRuta = lfRutaCompleta.getAbsolutePath();
            int liPosSeparator = lsRuta.lastIndexOf(File.separatorChar);

            lsRuta = lsRuta.substring(0, liPosSeparator);

            File ldDir = new File(lsRuta);
            boolean lbCreado = ldDir.mkdirs();

            if (entrada.isDirectory()) {
                continue;
            }

            FileOutputStream loutput = new FileOutputStream(lfRutaCompleta);

            if (entrada.getSize() > 0L) {
                while ((liLeido = linput.read(labBytes, 0, 2048)) != -1) {
                    loutput.write(labBytes, 0, liLeido);
                }
            }

            loutput.flush();
            loutput.close();
        }

        lzfFichero.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.netease.hearttouch.hthotfix.patch.PatchRefGeneratorTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            continue;
        }/*from ww  w. j av  a  2  s .c om*/
        final InputStream is = jar.getInputStream(entry);
        try {

            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (extension.getGeneratePatch() && extension.getScanRef()) {
                    ClassReader cr = new ClassReader(bytecode);
                    String className = cr.getClassName();
                    if (extension.getGeneratePatch() && extension.getScanRef()) {
                        if (!refScanInstrument.isPatchClass(className.replace("/", "."))) {
                            boolean bPatchRef = refScanInstrument.hasReference(bytecode, project);
                            if (bPatchRef) {
                                patchJarHelper.writeClassToDirectory(className, bytecode);
                            }
                        }
                    }
                }
                outputJar.writeEntry(entry, bytecode);
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

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

private static void readJarEntries(URL location, String basePath, Map<String, URL> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;
    jarfile = conn.getJarFile();/*from w ww.  ja  va  2 s .  co  m*/

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

        if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) {
            continue;
        }

        name = name.substring(basePath.length());

        if (name.contains("/")) {
            continue;
        }

        URL resource = new URL(location, name);
        resources.put(name, resource);
    }
}

From source file:org.sonatype.maven.plugin.emma4it.InstrumentProjectArtifactMojo.java

private void append(JarOutputStream output, File jar, boolean excludeMetainf) throws IOException {
    JarFile ejar = new JarFile(jar);

    Enumeration<JarEntry> entries = ejar.entries();
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        if (jarEntry.isDirectory() || (excludeMetainf && jarEntry.getName().startsWith("META-INF"))) {
            continue;
        }//from w  ww.ja  v a 2s  .co  m

        output.putNextEntry(jarEntry);

        InputStream input = ejar.getInputStream(jarEntry);
        try {
            IOUtil.copy(input, output);
        } finally {
            IOUtil.close(input);
        }

        output.flush();
    }

}

From source file:com.netease.hearttouch.hthotfix.patch.PatchGeneratorTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            continue;
        }/*w  ww.java2s  .  c om*/
        final InputStream is = jar.getInputStream(entry);
        try {

            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (extension.getGeneratePatch()) {
                    ClassReader cr = new ClassReader(bytecode);
                    String className = cr.getClassName();
                    if ((hashFileParser != null) && (hashFileParser.isChanged(className, bytecode))) {
                        patchJarHelper.writeClassToDirectory(className,
                                hackInjector.inject(className, bytecode));
                        refScanInstrument.addPatchClassName(className, hackInjector.getLastSuperName());
                    }
                }
                outputJar.writeEntry(entry, bytecode);
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:javadepchecker.Main.java

public void processJar(JarFile jar) throws IOException {
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        String name = entry.getName();
        if (!entry.isDirectory() && name.endsWith(".class")) {
            this.current.add(name);
            InputStream stream = jar.getInputStream(entry);
            new ClassReader(stream).accept(this, 0);
        }//from w  w  w  . j av a 2s  . c  om
    }
}