KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > util > JarResources


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.util;
11
12 import java.io.*;
13 import java.util.*;
14 import java.util.zip.*;
15
16 /**
17  * JarResources: JarResources maps all resources included in a
18  * Zip or Jar file. Additionaly, it provides a method to extract one
19  * as a blob.
20  *
21  * @author Unknown
22  * @author Klaus Meffert (integrated into JGAP)
23  * @since 3.2
24  */

25 public final class JarResources {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
28
29   // external debug flag
30
public boolean debugOn = false;
31
32   // jar resource mapping tables
33
private Hashtable htSizes = new Hashtable();
34
35   private Hashtable htJarContents = new Hashtable();
36
37   // a jar file
38
private String JavaDoc jarFileName;
39
40   /**
41    * creates a JarResources. It extracts all resources from a Jar
42    * into an internal hashtable, keyed by resource names.
43    * @param jarFileName a jar or zip file
44    */

45   public JarResources(String JavaDoc jarFileName) {
46     this.jarFileName = jarFileName;
47     init();
48   }
49
50   /**
51    * Extracts a jar resource as a blob.
52    * @param name a resource name.
53    */

54   public byte[] getResource(String JavaDoc name) {
55     return (byte[]) htJarContents.get(name);
56   }
57
58   /**
59    * initializes internal hash tables with Jar file resources.
60    */

61   private void init() {
62     try {
63       // extracts just sizes only.
64
ZipFile zf = new ZipFile(jarFileName);
65       Enumeration e = zf.entries();
66       while (e.hasMoreElements()) {
67         ZipEntry ze = (ZipEntry) e.nextElement();
68         if (debugOn) {
69           System.out.println(dumpZipEntry(ze));
70         }
71         htSizes.put(ze.getName(), new Integer JavaDoc( (int) ze.getSize()));
72       }
73       zf.close();
74       // extract resources and put them into the hashtable.
75
FileInputStream fis = new FileInputStream(jarFileName);
76       BufferedInputStream bis = new BufferedInputStream(fis);
77       ZipInputStream zis = new ZipInputStream(bis);
78       ZipEntry ze = null;
79       while ( (ze = zis.getNextEntry()) != null) {
80         if (ze.isDirectory()) {
81           continue;
82         }
83         if (debugOn) {
84           System.out.println(
85               "ze.getName()=" + ze.getName() + "," + "getSize()=" + ze.getSize()
86               );
87         }
88         int size = (int) ze.getSize();
89         // -1 means unknown size.
90
if (size == -1) {
91           size = ( (Integer JavaDoc) htSizes.get(ze.getName())).intValue();
92         }
93         byte[] b = new byte[ (int) size];
94         int rb = 0;
95         int chunk = 0;
96         while ( ( (int) size - rb) > 0) {
97           chunk = zis.read(b, rb, (int) size - rb);
98           if (chunk == -1) {
99             break;
100           }
101           rb += chunk;
102         }
103         // add to internal resource hashtable
104
htJarContents.put(ze.getName(), b);
105         if (debugOn) {
106           System.out.println(
107               ze.getName() + " rb=" + rb +
108               ",size=" + size +
109               ",csize=" + ze.getCompressedSize()
110               );
111         }
112       }
113     } catch (NullPointerException JavaDoc e) {
114       System.out.println("done.");
115     } catch (FileNotFoundException e) {
116       e.printStackTrace();
117     } catch (IOException e) {
118       e.printStackTrace();
119     }
120   }
121
122   /**
123    * Dumps a zip entry into a string.
124    * @param ze a ZipEntry
125    */

126   private String JavaDoc dumpZipEntry(ZipEntry ze) {
127     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128     if (ze.isDirectory()) {
129       sb.append("d ");
130     }
131     else {
132       sb.append("f ");
133     }
134     if (ze.getMethod() == ZipEntry.STORED) {
135       sb.append("stored ");
136     }
137     else {
138       sb.append("defalted ");
139     }
140     sb.append(ze.getName());
141     sb.append("\t");
142     sb.append("" + ze.getSize());
143     if (ze.getMethod() == ZipEntry.DEFLATED) {
144       sb.append("/" + ze.getCompressedSize());
145     }
146     return (sb.toString());
147   }
148
149   /**
150    * Is a test driver. Given a jar file and a resource name, it trys to
151    * extract the resource and then tells us whether it could or not.
152    *
153    * <strong>Example</strong>
154    * Let's say you have a JAR file which jarred up a bunch of gif image
155    * files. Now, by using JarResources, you could extract, create, and display
156    * those images on-the-fly.
157    * <pre>
158    * ...
159    * JarResources JR=new JarResources("GifBundle.jar");
160    * Image image=Toolkit.createImage(JR.getResource("logo.gif");
161    * Image logo=Toolkit.getDefaultToolkit().createImage(
162    * JR.getResources("logo.gif")
163    * );
164    * ...
165    * </pre>
166    */

167   public static void main(String JavaDoc[] args)
168       throws IOException {
169     if (args.length != 2) {
170       System.err.println(
171           "usage: java JarResources <jar file name> <resource name>"
172           );
173       System.exit(1);
174     }
175     JarResources jr = new JarResources(args[0]);
176     byte[] buff = jr.getResource(args[1]);
177     if (buff == null) {
178       System.out.println("Could not find " + args[1] + ".");
179     }
180     else {
181       System.out.println("Found " + args[1] + " (length=" + buff.length + ").");
182     }
183   }
184 }
185
Popular Tags