【问题标题】:How to get the whole file structure as a tree when there are jar files in this jar file当这个jar文件中有jar文件时如何将整个文件结构获取为一棵树
【发布时间】:2013-10-16 13:40:00
【问题描述】:

我想将 jar 文件的整个文件结构作为树。我找到了很多解决方案。我将其部署为 zip 文件。我点击链接How to get names of classes inside a jar file?

代码如下:

    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());
        }
    }

}

D:\work\workspace\myjar\org.objectweb.asm_2.2.2.jar(It is not in classpath.) 打印的结果 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

如何进入这个jar文件中的jar文件?

【问题讨论】:

    标签: java file jar tree zip


    【解决方案1】:

    你可以试试:

    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();
    }
    

    【讨论】:

    • jarFileD:\work\workspace\myjar\org.objectweb.asm_2.2.2.jardestDir 是什么?
    • 你能给我更多的cmets或解释你的方法吗? @aviad 谢谢
    • 忽略 destDir(脏复制粘贴作业)。我尚未对其进行测试,但您可以了解如何通过递归迭代 jar 条目的枚举来扫描 jar 内容,而某些条目本身可以是 jar 文件。
    • 你的意思是 java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); 是没用?
    • 好吧,file 是 JarEntry 的类型,它没有 getAbsolutePath()。 @aviad
    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多