Maison  >  Questions et réponses  >  le corps du texte

java 如何列出jar包中jar包里的所有文件和目录?

如这样一个目录/Users/xxx/IdeaProjects/abc/web/target/web.jar!/BOOT-INF/lib/rest-0.0.1.jar!/com/tg/tiny
/Users/xxx/IdeaProjects/abc/web/target/web.jar这个jar包下的文件目录可以这样得到

JarFile localJarFile = new JarFile(new File("/Users/xxx/IdeaProjects/abc/web/target/web.jar"));
Enumeration<JarEntry> entries = localJarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            System.out.println(jarEntry.getName());
        }

那么这个web.jar里的rest-0.0.1.jar下的文件目录如何得到?

天蓬老师天蓬老师2743 Il y a quelques jours961

répondre à tous(2)je répondrai

  • 巴扎黑

    巴扎黑2017-04-18 10:54:55

            URL url = new URL("jar", null, 0, "file:/Users/xxx/IdeaProjects/web/target/web.jar!/BOOT-INF/lib/rest.jar");
            URLConnection con = url.openConnection();
            if (con instanceof JarURLConnection) {
                JarURLConnection result = (JarURLConnection) con;
                JarInputStream jarInputStream = new JarInputStream(result.getInputStream());
                JarEntry entry;
                while ((entry = jarInputStream.getNextJarEntry()) != null) {
                    System.out.println(entry.getName());
                }
            }

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-18 10:54:55

    Un package jar est un fichier. Pas un répertoire.
    Besoin de transmettre getResourceAsStream() du chargeur de classe.

    package edu.hxraid;  
    import java.io.*;  
    public class Resource {  
        public void getResource() throws IOException{  
            //返回读取指定资源的输入流  
            InputStream is=this.getClass().getResourceAsStream("/resource/res.txt");   
            BufferedReader br=new BufferedReader(new InputStreamReader(is));  
            String s="";  
            while((s=br.readLine())!=null)  
                System.out.println(s);  
        }  
    } 

    Utilisation détaillée :
    http://www.myexception.cn/pro...

    répondre
    0
  • Annulerrépondre