2013-05-30 3 views
3

Мне нужно прочитать содержимое /test/a.xml из файла test.jar (они оба переменные, конечно, не константы). Каков самый простой способ сделать это?Как читать файл из архива JAR?

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

ли ваше приложение также работает с 'test.jar'? – Pshemo

+0

Нет, мое приложение не работает из test.jar – yegor256

+0

Вы используете Java 7? –

ответ

4

Как об этом:

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

Используйте 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 
    } 
Смежные вопросы