如何从JAR存档中读取文件?

我需要从test.jar文件中读取/test/a.xml的内容(它们都是变量,当然不是常量)。 最简单的方法是什么?

 File file = new File("test.jar"); String path = "/test/a.xml"; String content = // ... how? 

这个怎么样:

 JarFile file = new JarFile(new File("test.jar")); JarEntry entry = file.getJarEntry("/test/a.xml"); String content = IOUtils.toString(file.getInputStream(entry)); 

使用ZipInputStream并搜索您请求的文件。

 FileInputStream fis = new FileInputStream(args[0]); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) if(ze.getName().equals("/test/a.xml") { //use zis to read the file's content }