/Users/lyon/j4p/src/bookExamples/ch15Streams/Unzipper.java
|
1 package bookExamples.ch15Streams;
2
3 import gui.In;
4 import futils.Futil;
5
6 import java.io.BufferedInputStream;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.util.Enumeration;
12 import java.util.Hashtable;
13 import java.util.Vector;
14 import java.util.zip.ZipEntry;
15 import java.util.zip.ZipFile;
16 import java.util.zip.ZipInputStream;
17
18
19 /**
20 * JarResources: JarResources maps all resources included in a
21 * Zip or Jar file. Additionaly, it provides a method to extract one
22 * as a blob.
23 */
24 public final class Unzipper {
25
26 // external debug flag
27 public boolean debugOn = false;
28
29 // jar resource mapping tables
30 private Hashtable htSizes = new Hashtable();
31 private Hashtable htJarContents = new Hashtable();
32 private Vector nameVector = new Vector();
33
34 private String fileName;
35
36 public String[] getNames() {
37 String s[] = new String[nameVector.size()];
38 nameVector.copyInto(s);
39 return s;
40 }
41
42 /**
43 * creates a JarResources. It extracts all resources from a Jar
44 * into an internal hashtable, keyed by resource names.
45 *
46 * @param jarFileName a jar or zip file
47 */
48 public Unzipper(String jarFileName) {
49 this.fileName = jarFileName;
50 init();
51 }
52
53 /**
54 * Extracts a jar resource as a blob.
55 *
56 * @param name a resource name.
57 */
58 public byte[] getBlob(String name) {
59 return (byte[]) htJarContents.get(name);
60 }
61
62 /**
63 * initializes internal hash tables with Jar file resources.
64 */
65 private void init() {
66 try {
67 // extracts just sizes only.
68 ZipFile zf = new ZipFile(fileName);
69 Enumeration e = zf.entries();
70 while (e.hasMoreElements()) {
71 ZipEntry ze = (ZipEntry) e.nextElement();
72 String name = ze.getName();
73 nameVector.addElement(name);
74 if (debugOn) {
75 System.out.println(dumpZipEntry(ze));
76 }
77
78 htSizes.put(name, new Integer((int) ze.getSize()));
79 }
80 zf.close();
81
82 // extract resources and put them into the hashtable.
83 FileInputStream fis = new FileInputStream(fileName);
84 BufferedInputStream bis = new BufferedInputStream(fis);
85 ZipInputStream zis = new ZipInputStream(bis);
86 ZipEntry ze = null;
87 while ((ze = zis.getNextEntry()) != null) {
88 if (ze.isDirectory()) {
89 continue;
90 }
91 if (debugOn) {
92 System.out.println("ze.getName()=" + ze.getName() + "," + "getSize()=" + ze.getSize());
93 }
94 int size = (int) ze.getSize();
95 // -1 means unknown size.
96 if (size == -1) {
97 size = ((Integer) htSizes.get(ze.getName())).intValue();
98 }
99 byte[] b = new byte[(int) size];
100 int rb = 0;
101 int chunk = 0;
102 while (((int) size - rb) > 0) {
103 chunk = zis.read(b, rb, (int) size - rb);
104 if (chunk == -1) {
105 break;
106 }
107 rb += chunk;
108 }
109 // add to internal resource hashtable
110 htJarContents.put(ze.getName(), b);
111 if (debugOn) {
112 System.out.println(ze.getName() + " rb=" + rb +
113 ",size=" + size +
114 ",csize=" + ze.getCompressedSize());
115 }
116 }
117 } catch (NullPointerException e) {
118 System.out.println("done.");
119 } catch (FileNotFoundException e) {
120 e.printStackTrace();
121 } catch (IOException e) {
122 e.printStackTrace();
123 }
124 }
125
126 /**
127 * Dumps a zip entry into a string.
128 *
129 * @param ze a ZipEntry
130 */
131 private String dumpZipEntry(ZipEntry ze) {
132 StringBuffer sb = new StringBuffer();
133 if (ze.isDirectory()) {
134 sb.append("d ");
135 } else {
136 sb.append("f ");
137 }
138 if (ze.getMethod() == ZipEntry.STORED) {
139 sb.append("stored ");
140 } else {
141 sb.append("defalted ");
142 }
143 sb.append(ze.getName());
144 sb.append("\t");
145 sb.append("" + ze.getSize());
146 if (ze.getMethod() == ZipEntry.DEFLATED) {
147 sb.append("/" + ze.getCompressedSize());
148 }
149 return (sb.toString());
150 }
151
152 /**
153 * Is a test driver. Given a jar file and a resource name, it trys to
154 * extract the resource and then tells us whether it could or not.
155 * <p/>
156 * <strong>Example</strong>
157 * Let's say you have a JAR file which jarred up a bunch of gif image
158 * files. Now, by using JarResources, you could extract, create, and display
159 * those images on-the-fly.
160 * <pre>
161 * ...
162 * JarResources JR=new JarResources("GifBundle.jar");
163 * Image image=Toolkit.createImage(JR.getResource("logo.gif");
164 * Image logo=Toolkit.getDefaultToolkit().createImage(
165 * JR.getResources("logo.gif")
166 * );
167 * ...
168 * </pre>
169 */
170 public static void main(String[] args) {
171 getResourceByName();
172 //Hw1Start();
173 }
174
175 private static void getResourceByName() {
176 Futil.setSwing(false);
177 File f = futils.Futil.getReadFile("select a jar or zip file");
178 String absolutePath = f.getAbsolutePath();
179 Unzipper uz = new Unzipper(absolutePath);
180 String names[] = uz.getNames();
181 while (true) {
182 String resource = (String)
183 In.multiPrompt(names, "select a resource", "resource selection dialog");
184 if (resource == null) System.exit(0);
185 printResourceInfo(uz, resource);
186 }
187 }
188
189 private static void printResourceInfo(Unzipper uz, String resource) {
190 byte[] buff = uz.getBlob(resource);
191 if (buff == null) {
192 System.out.println("Could not find " + resource + ".");
193 } else {
194 System.out.println("Found " + resource + " (length=" + buff.length + ").");
195 }
196 }
197
198 } // End of JarResources class.
199