How to read a file from a JAR archive?

I need to read the contents of /test/a.xml from the test.jar file (they are both variables, of course, not constant). What is the easiest way to do this?

 File file = new File("test.jar"); String path = "/test/a.xml"; String content = // ... how? 
+3
java jar
May 30 '13 at 17:21
source share
2 answers

How about this:

 JarFile file = new JarFile(new File("test.jar")); JarEntry entry = file.getJarEntry("/test/a.xml"); String content = IOUtils.toString(file.getInputStream(entry)); 
+4
May 30 '13 at 17:34
source share

Use ZipInputStream and search for the desired file.

 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 content } 
+2
May 30 '13 at 17:29
source share



All Articles