如何解析jar中的文件以及查看jar中包含的内容
package com.hb;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.junit.Test;
public class JarFileDemo {
/**
* 遍历jar包所有的文件
*/
@Test
public void getAllFilesName(){
String path = "d:/hb.jar";
try {
File file = new File(path);
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> jarEntries = jarFile.entries();
while(jarEntries.hasMoreElements()){
JarEntry entry = jarEntries.nextElement();
System.out.println(entry.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取jar包中的某个文件
*/
@Test
public void readFileInJar(){
String path = "d:/hb.jar";
try {
File file = new File(path);
JarFile jarFile = new JarFile(file);
//指定文件路径的位置
// ZipEntry zipEntry = jarFile.getEntry("AppProperty.xml");
ZipEntry zipEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
InputStream is = jarFile.getInputStream(zipEntry);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String temp = null;
while((temp = br.readLine())!=null){
System.out.println(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}