ฉันต้องอ่านเนื้อหาของ /test/a.xml
จากไฟล์ test.jar
(แน่นอนว่าทั้งคู่เป็นตัวแปร ไม่ใช่ค่าคงที่) วิธีที่ง่ายที่สุดที่จะทำคืออะไร?
File file = new File("test.jar");
String path = "/test/a.xml";
String content = // ... how?
ฉันต้องอ่านเนื้อหาของ /test/a.xml
จากไฟล์ test.jar
(แน่นอนว่าทั้งคู่เป็นตัวแปร ไม่ใช่ค่าคงที่) วิธีที่ง่ายที่สุดที่จะทำคืออะไร?
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
}
fis
เหมือนปกติ InputStream
และมันจะให้เนื้อหาของไฟล์แก่คุณ
- person Jimmy T.; 15.10.2020
test.jar
ด้วยหรือไม่ - person Pshemo   schedule 30.05.2013