从jar文件中提取资源

数 java 程序员都非常清楚使用 jar 文件将组成 java 解决方案的各种资源(即 .class 文件、声音和图像)打包的优点。刚开始使用 jar 文件的人常问的一个问题是:“如何从 jar 文件中提取图像呢?”本文将回答这个问题,并会提供一个类,这个类使从 jar 文件中提取任何资源变得非常简单!

加载 gif 图像
假定我们有一个 jar 文件,其中包含我们的应用程序要使用的一组 .gif 图像。下面就是使用 JarResources 访问 jar 文件中的图像文件的方法:
JarResources JR=new JarResources("GifBundle.jar");

Image logo=Toolkit.getDefaultToolkit().createImage(JR.getResources("logo.gif"));

这段代码说明我们可以创建一个JarResources对象,并将其初始化为包含我们要使用的资源的 jar 文件 -- images.jar。随后我们使用JarResources的getResource()方法将来自 logo.gif 文件的原始数据提供给 awt Toolkit 的createImage()方法。
命名说明
JarResource 是一个非常简单的示例,它说明了如何使用 java 所提供的各种功能来处理 jar 和 zip 档案文件。

工作方式
JarReources类的重要数据域用来跟踪和存储指定 jar 文件的内容:
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();
}

现在,init()方法只将指定 jar 文件的整个内容加载到一个 hashtable(通过资源名访问)中。

这是一个相当有用的方法,下面我们对它作进一步的分析。ZipFile类为我们提供了对 jar/zip 档案头信息的基本访问方法。这类似于文件系统中的目录信息。下面我们列出ZipFile中的所有条目,并用档案中每个资源的大小添充 htsizes hashtable:
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类访问档案。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 means unknown size. 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; } // add to internal resource 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(); } }请注意,用来标识每个资源的名称是档案中资源的限定路径名,例如,不是包中的类名 -- 即 java.util.zip 包中的ZipEntry类将被命名为 "java/util/zip/ZipEntry",而不是 "java.util.zip.ZipEntry"。

其它方法:
/** * 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()); } /** * Extracts a jar resource as a blob. * @param name a resource name. */ public byte[] getResource(String name) { return (byte[])htJarContents.get(name); } 代码的最后一个重要部分是简单的测试驱动程序。该测试驱动程序是一个简单的应用程序,它接收 jar/zip 档案名和资源名。它试图发现档案中的资源文件,然后将成功或失败的消息报告出来:
public static void main(String[] args) throws IOException { if (args.length!=2) { System.err.println( "usage: java JarResources < jar file name> < resource name>" ); 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 { System.out.println("Found "+args[1]+ " (length="+buff.length+")."); } }} // End of JarResources class.您已了解了这个类。一个易于使用的类,它隐藏了使用打包在 jar 文件中的资源的全部棘手问题。
小结
如果您曾经渴望知道如何从 jar 文件中提取图像,那么您现在已学到了一种方法。有了本技巧提供的这个新类,您就不仅可以用 jar 文件处理图像,而且可以将提取魔术用于 jar 文件中的任何资源。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值