一、前言
关于jdiary源码包中定义com.l2fprod.util.ZipResourceLoader资源包加载器,实现获取资源包访问路径生成、资源包中文件数据获取等。
二、源码说明
package com.l2fprod.util;@b@@b@import java.io.BufferedInputStream;@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.PrintStream;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import java.net.URLStreamHandler;@b@import java.util.Enumeration;@b@import java.util.Hashtable;@b@import java.util.zip.ZipEntry;@b@import java.util.zip.ZipInputStream;@b@@b@public class ZipResourceLoader@b@{@b@ Hashtable resources;@b@ URLStreamHandler handler;@b@@b@ public ZipResourceLoader(URL p_JarUrl)@b@ throws Exception@b@ {@b@ this(p_JarUrl.openStream());@b@ }@b@@b@ public ZipResourceLoader(InputStream p_JarStream)@b@ throws Exception@b@ {@b@ this.resources = new Hashtable();@b@@b@ this.handler = new URLStreamHandler(this) {@b@ private final ZipResourceLoader this$0;@b@@b@ protected URLConnection openConnection() { return new ZipResourceLoader.ZipURLConnection(this.this$0, u);@b@ }@b@@b@ protected void parseURL(, String spec, int start, int limit) {@b@ String file = u.getFile();@b@ int index = file.indexOf("/");@b@ if (index != -1) {@b@ String path = file.substring(0, index);@b@ setURL(u, u.getProtocol(), u.getHost(), u.getPort(), path + "/" + spec, u.getRef());@b@ }@b@ else@b@ {@b@ setURL(u, u.getProtocol(), u.getHost(), u.getPort(), spec, u.getRef());@b@ }@b@ }@b@@b@ protected String toExternalForm()@b@ {@b@ return "ziploader://" + u.getFile();@b@ }@b@@b@ };@b@ BufferedInputStream bis = new BufferedInputStream(p_JarStream);@b@ ZipInputStream input = new ZipInputStream(bis);@b@@b@ ZipEntry entry = null;@b@ while (true) { while (true) { if ((entry = input.getNextEntry()) == null) return;@b@ if (!(entry.isDirectory()))@b@ break;@b@ }@b@@b@ ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();@b@@b@ while ((read = input.read()) != -1) {@b@ int read;@b@ outBuffer.write(read);@b@ }@b@ this.resources.put(entry.getName(), outBuffer.toByteArray());@b@ }@b@ }@b@@b@ public InputStream getResourceAsStream(String name)@b@ throws MalformedURLException, IOException@b@ {@b@ return getResource(name).openStream();@b@ }@b@@b@ public URL getResource(String name)@b@ throws MalformedURLException@b@ {@b@ if (this.resources.get(name) == null)@b@ return null;@b@@b@ return new URL("ziploader", null, -1, name, this.handler);@b@ }@b@@b@ public ZipResource getZipResource(String name)@b@ {@b@ return new ZipResource(this, name);@b@ }@b@@b@ public ZipResource getZipResource(URL name)@b@ {@b@ return getZipResource(name.getFile());@b@ }@b@@b@ public Enumeration entries()@b@ {@b@ return this.resources.keys();@b@ }@b@@b@ public void dump()@b@ {@b@ for (Enumeration e = this.resources.keys(); e.hasMoreElements(); ) {@b@ String name = (String)e.nextElement();@b@ System.out.println(name + " size = " + ((byte[])this.resources.get(name)).length);@b@ }@b@ }@b@@b@ public void release()@b@ {@b@ this.resources.clear();@b@ this.resources = null;@b@ }@b@@b@ private byte[] getURLContent(String name)@b@ {@b@ byte[] data = (byte[])this.resources.get(name);@b@ if ((data == null) && (name.startsWith("/")))@b@ data = (byte[])this.resources.get(name.substring(1));@b@@b@ return data;@b@ }@b@@b@ public static void main(String[] args)@b@ throws Exception@b@ {@b@ ZipResourceLoader loader = new ZipResourceLoader(new File(args[0]).toURL());@b@ for (Enumeration e = loader.entries(); e.hasMoreElements(); ) {@b@ String name = (String)e.nextElement();@b@ System.out.println(name);@b@ URL url = loader.getResource(name);@b@ System.out.println(url);@b@ System.out.println(url.openStream());@b@ System.out.println(loader.getZipResource(name).getInputStream());@b@ }@b@ }@b@@b@ static byte[] access$000(ZipResourceLoader x0, String x1)@b@ {@b@ return x0.getURLContent(x1);@b@ }@b@@b@ class ZipURLConnection extends URLConnection@b@ {@b@ private final ZipResourceLoader this$0;@b@@b@ public ZipURLConnection(, URL p_Url)@b@ {@b@ super(p_Url);@b@@b@ this.this$0 = this$0;@b@ }@b@@b@ public InputStream getInputStream()@b@ {@b@ ByteArrayInputStream input = new ByteArrayInputStream((byte[])this.this$0.resources.get(getURL().getFile()));@b@ return input;@b@ }@b@@b@ public void connect()@b@ throws IOException@b@ {@b@ if ((this.this$0.resources == null) || (this.this$0.resources.get(getURL().getFile()) == null))@b@ throw new IOException("No data for " + getURL());@b@ }@b@ }@b@@b@ public class ZipResource@b@ {@b@ String m_Name;@b@ private final ZipResourceLoader this$0;@b@@b@ public ZipResource(, String p_Name)@b@ {@b@ this.this$0 = this$0;@b@ this.m_Name = p_Name;@b@ }@b@@b@ public URL getURL()@b@ throws MalformedURLException@b@ {@b@ return new URL("http", null, -1, this.m_Name);@b@ }@b@@b@ public InputStream getInputStream()@b@ {@b@ ByteArrayInputStream input = new ByteArrayInputStream(ZipResourceLoader.access$000(this.this$0, this.m_Name));@b@ return input;@b@ }@b@@b@ public byte[] getURLContent()@b@ {@b@ return ZipResourceLoader.access$000(this.this$0, this.m_Name);@b@ }@b@ }@b@}