日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java访问jar中的资源问题代码

發布時間:2024/8/22 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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注冊。

轉載于:https://www.cnblogs.com/aquar/archive/2009/09/13/3451511.html

總結

以上是生活随笔為你收集整理的java访问jar中的资源问题代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。