java访问jar中的资源问题代码
package aquar.ja_access;
import java.io.*;
import java.util.*;
import java.util.zip.*;
//JarResources 类的重要数据域用来跟踪和存储指定 JAR 文件的内容
/**
* @author edison
*
*/
public final class JarResources {
public boolean debugOn = false;
private Hashtable htSizes = new Hashtable();
private Hashtable htJarContents = new Hashtable();
private String jarFileName;
// 类的实例化设置 JAR 文件的名称,然后转到 init() 方法完成全部实际工作
public JarResources(String jarFileName) {
this.jarFileName = jarFileName;
init();
}
/**
* Extracts a jar resource as a blob.
*
* @param name
* a resource name.
*/
public byte[] getResource(String name) {
return (byte[]) htJarContents.get(name);
}
// ctrl+/ 添加注释
// init() 方法只将指定 JAR 文件的整个内容加载到一个 hashtable(通过资源名访问)中
// ZipFile 类为我们提供了对 JAR/zip 档案头信息的基本访问方法。这类似于文件系统中
// 的目录信息。下面我们列出 ZipFile 中的所有条目,并用档案中每个资源的大小添充
// htSizes hashtable:
/**
* alt+shift+j 添加注释 initializes internal hash tables with Jar file
* resources.
*/
private void init() {
try {// extracts just sizes only.
ZipFile zf = new ZipFile(jarFileName);
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
if (debugOn) {
System.out.println(dumpZipEntry(ze));
}
htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
}
zf.close();
// ZipInputStream 类访问档案
// 我们从档案中读取组成每个资源的精确字节数,并将其存储在
// htJarContents hashtable 中,您可以通过资源名访问这些数据
// extract resources and put them into the hashtable.
FileInputStream fis = new FileInputStream(jarFileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
continue;
}
if (debugOn) {
System.out.println("ze.getName()=" + ze.getName() + ","
+ "getSize()=" + ze.getSize());
}
int size = (int) ze.getSize();
// -1 表示大小未知。
if (size == -1) {
size = ((Integer) htSizes.get(ze.getName())).intValue();
}
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = zis.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
// 添加到内部资源 hashtable 中
htJarContents.put(ze.getName(), b);
if (debugOn) {
System.out.println(ze.getName() + " rb=" + rb + ",size="
+ size + ",csize=" + ze.getCompressedSize());
}
}
} catch (NullPointerException e) {
System.out.println("done.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Dumps a zip entry into a string.
*
* @param ze
* a ZipEntry
*/
private String dumpZipEntry(ZipEntry ze) {
StringBuffer sb = new StringBuffer();
if (ze.isDirectory()) {
sb.append("d ");
} else {
sb.append("f ");
}
if (ze.getMethod() == ZipEntry.STORED) {
sb.append("stored ");
} else {
sb.append("defalted ");
}
sb.append(ze.getName());
sb.append("\t");
sb.append("" + ze.getSize());
if (ze.getMethod() == ZipEntry.DEFLATED) {
sb.append("/" + ze.getCompressedSize());
}
return (sb.toString());
}
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("user.dir"));
if (args.length != 2) {
System.err.println("usage: java JarResources ");
System.exit(1);
}
JarResources jr = new JarResources(args[0]);
byte[] buff = jr.getResource(args[1]);
if (buff == null) {
System.out.println("Could not find " + args[1] + ".");
} else {
// sysout+ alt+/ 代码模板 System.out.println();
System.out.println("Found " + args[1] + " (length="
+ buff.length + ").");
}
}
}
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。