Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当这个jar文件中有jar文件时,如何将整个文件结构作为一个树来获取_Java_File_Jar_Tree_Zip - Fatal编程技术网

Java 当这个jar文件中有jar文件时,如何将整个文件结构作为一个树来获取

Java 当这个jar文件中有jar文件时,如何将整个文件结构作为一个树来获取,java,file,jar,tree,zip,Java,File,Jar,Tree,Zip,我想把jar文件的整个文件结构作为一棵树。我找到了很多解决办法。我正在将其部署为zip文件。我跟踪了链接 代码如下所示: public void getClassFromJar(String path) throws IOException { //ArrayList<String> classNames=new ArrayList<String>(); ZipInputStream zip=new ZipInputStream(new File

我想把jar文件的整个文件结构作为一棵树。我找到了很多解决办法。我正在将其部署为zip文件。我跟踪了链接

代码如下所示:

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

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

您可以尝试:

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

jarFile
D:\work\workspace\myjar\org.objectweb.asm_2.2.2.jar
。什么是
destDir
?你能给我更多关于你的方法的评论或解释吗@aviad感谢指定destDir(脏拷贝粘贴作业)。我还没有测试过它,但是您可以通过递归迭代jar条目的枚举来了解如何扫描jar内容,而有些条目本身可以是jar文件。您是指java.io.File f=new java.io.File(destDir+java.io.File.separator+File.getName());是无用的吗?嗯,
文件
是JarEntry的类型,它没有
getAbsolutePath()
@阿维德
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();
}