1

I want to get the whole file sturcture of a jar file as a tree. I find many solutions. I am deploy it as a zip file. I followed the link How to get names of classes inside a jar file?

Code looks as follow:

    public void getClassFromJar(String path) throws IOException {

    //ArrayList<String> classNames=new ArrayList<String>();
    ZipInputStream zip=new ZipInputStream(new FileInputStream(path));
    IntoEntry(zip);

    //return classNames;
}

public void IntoEntry(ZipInputStream zip) throws IOException {

    for(ZipEntry entry=zip.getNextEntry();entry!=null;entry=zip.getNextEntry()) {
        System.out.println("entry: "+entry.getName());
        if (entry.getName().endsWith(".jar")) {
            // How to do

        }

        if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
            // This ZipEntry represents a class. Now, what class does it represent?
            StringBuilder className=new StringBuilder();
            for(String part : entry.getName().split("/")) {
                if(className.length() != 0) {
                    className.append(".");
                }
                className.append(part);
                if(part.endsWith(".class")) {
                    className.setLength(className.length()-".class".length());
                }
            }
            classNames.add(className.toString());
        }
    }

}

The result from D:\work\workspace\myjar\org.objectweb.asm_2.2.2.jar(It is not in classpath.) print by System.out.println("entry: "+entry.getName());:

entry: output/
entry: output/dist/
entry: output/dist/lib/
entry: output/dist/lib/asm-2.2.2.jar
entry: output/dist/lib/asm-analysis-2.2.2.jar
entry: output/dist/lib/asm-attrs-2.2.2.jar
entry: output/dist/lib/asm-commons-2.2.2.jar
entry: output/dist/lib/asm-tree-2.2.2.jar
entry: output/dist/lib/asm-util-2.2.2.jar
entry: plugin.xml

How to go into the jar files in this jar file?

Community
  • 1
  • 1
Nick Dong
  • 3,638
  • 8
  • 47
  • 84

1 Answers1

0

You can try:

public static void printJarContent(File jarFile) {  
    java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
    java.util.Enumeration enum = jar.entries();
    while (enum.hasMoreElements()) {
        java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement();
        // temp directory where the jar will be extracted
        java.io.File f = new java.io.File(DEST_DIR + java.io.File.separator + file.getName());
        String ext = Files.probeContentType(f.toPath());
        if(ext.equalsIgnoreCase("jar")) {
            // extract current nested jar to a DEST_DIR and then
            printJarContent(f);    
        }
    java.io.InputStream is = jar.getInputStream(file); // get the input stream
    while (is.available() > 0) {  
        //print out the (is.read()) or do whatever you want with it
    }
    is.close();
}
aviad
  • 8,229
  • 9
  • 50
  • 98
  • `jarFile` is `D:\work\workspace\myjar\org.objectweb.asm_2.2.2.jar`. what is `destDir`? – Nick Dong Oct 09 '13 at 10:08
  • Can you give me more comments or explanation about your method? @aviad thanks – Nick Dong Oct 09 '13 at 11:04
  • Ignore the destDir (dirty copy-paste job). I haven't tested it but you can get the idea how to scan jar content by recursively iterating over enumeration of jar entries while some entries can be jar files themselves. – aviad Oct 09 '13 at 11:06
  • Did you mean **java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());** is useless? – Nick Dong Oct 09 '13 at 11:19
  • Well, `file` is type of JarEntry, which dont have `getAbsolutePath()`. @aviad – Nick Dong Oct 09 '13 at 11:35
  • right. see my edit. I think this was enough to get the idea :) – aviad Oct 09 '13 at 13:43
  • It not works. `probeContentType` cant get ext correctly from `f.topath()`. `ext` always got `null`. – Nick Dong Oct 15 '13 at 10:30
  • `f` in the next call of (`printJarContent(f);`) is not treated as a normal file. The program got error, which means could not find the specific file, in the next call of (`printJarContent(f);`). @aviad – Nick Dong Oct 15 '13 at 10:40