1 package com.ca.commons.cbutil; 2 3 import java.awt.*; 4 import java.io.*; 5 import java.util.*; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 import java.util.zip.*; 9 10 17 18 public final class CBJarResource 19 { 20 24 protected Hashtable contents = new Hashtable(); 25 26 30 protected Hashtable entries = new Hashtable(); 31 32 35 protected String zipFileName; 36 37 40 protected ZipFile zipFile; 41 42 43 private static Logger log = Logger.getLogger(CBJarResource.class.getName()); 44 45 54 55 60 61 66 67 public String getZipFileName() 68 { 69 return zipFileName; 70 } 71 72 75 76 public long getLastModified() 77 { 78 File zip = new File(zipFileName); 79 if (zip == null) 80 return 0; 81 else 82 return zip.lastModified(); 83 } 84 85 91 92 public CBJarResource(String zipFileName) 93 { 94 this.zipFileName = zipFileName; 95 init(); 96 log.fine("INITIALIZED JAR " + zipFileName); 97 } 98 99 102 103 public String toString() 104 { 105 return "CBJarResource (" + zipFileName + ")"; 106 } 107 108 114 115 public boolean hasResource(String name) 116 { 117 return entries.containsKey(name); 118 } 119 120 126 127 public byte[] getResource(String name) throws ZipException 128 { 129 return getRawResource(name); 130 } 131 132 135 136 protected byte[] getRawResource(String name) throws ZipException 137 { 138 try 139 { 140 ZipEntry entry = (ZipEntry) entries.get(name); 141 InputStream in = zipFile.getInputStream(entry); 142 143 if (entry == null) 144 { 145 throw new ZipException("Unable to find: " + name + " in zip file " + zipFileName); 146 } 147 148 return readZipEntryData(in, entry); 149 } 150 catch (IOException e) 151 { 152 throw new ZipException("Error reading zip file: " + e); 153 } 154 } 155 156 157 164 165 public Image getImage(String imageName, Toolkit imageCreator) 166 throws ZipException 167 { 168 if (hasResource(imageName) == false) 169 throw new ZipException("resource: " + imageName + " not found in zip file: " + zipFileName); 170 171 172 Image ret = imageCreator.createImage(getRawResource(imageName)); 173 return ret; 174 } 175 176 182 public InputStream getInputStream(String resourceName) 183 throws ZipException 184 { 185 try 186 { 187 ZipEntry entry = (ZipEntry) entries.get(resourceName); 188 InputStream in = zipFile.getInputStream(entry); 189 return in; 190 } 191 catch (IOException e) 192 { 193 throw new ZipException("Error getting input stream: " + e.toString()); 194 } 195 } 196 197 200 protected void init() 201 { 202 try 203 { 204 zipFile = new ZipFile(zipFileName); 206 Enumeration e = zipFile.entries(); 207 while (e.hasMoreElements()) 208 { 209 ZipEntry ze = (ZipEntry) e.nextElement(); 210 String name = ze.getName(); 212 log.fine("added zip entry: " + name); 213 entries.put(name, ze); 214 215 218 if (name.endsWith("Editor") || name.endsWith("editor")) 219 { 220 log.fine("double loading " + name.toLowerCase()); 221 entries.put(name.toLowerCase(), ze); 222 } 223 } 224 } 225 catch (NullPointerException e) 226 { 227 log.warning("unable to init zip file " + zipFileName + " - no entries?"); 228 } 229 catch (FileNotFoundException e) 230 { 231 log.log(Level.WARNING, "can't find zip file", e); 232 } 233 catch (IOException e) 234 { 235 log.log(Level.WARNING, "error reading zip file", e); 236 } 237 } 238 239 246 247 protected byte[] readZipEntryData(InputStream is, ZipEntry ze) 248 throws IOException 249 { 250 int size = (int) ze.getSize(); 251 252 if (size == -1) 253 { 254 log.warning("bizarre size error in zip entry reading = corrupt zip file?"); 255 return null; 256 } 257 258 byte[] b = new byte[(int) size]; 259 int rb = 0; 260 int chunk = 0; 261 while (((int) size - rb) > 0) 262 { 263 chunk = is.read(b, rb, (int) size - rb); 264 if (chunk == -1) 265 { 266 break; 267 } 268 rb += chunk; 269 } 270 271 278 279 return b; 280 } 281 282 287 protected String dumpZipEntry(ZipEntry ze) 288 { 289 StringBuffer sb = new StringBuffer (); 290 if (ze.isDirectory()) 291 { 292 sb.append("dir: "); 293 } 294 else 295 { 296 sb.append("file: "); 297 } 298 if (ze.getMethod() == ZipEntry.STORED) 299 { 300 sb.append("stored "); 301 } 302 else 303 { 304 sb.append("deflated "); 305 } 306 sb.append(ze.getName()); 307 sb.append("\t\t"); 308 sb.append("" + ze.getSize()); 309 if (ze.getMethod() == ZipEntry.DEFLATED) 310 { 311 sb.append("/" + ze.getCompressedSize()); 312 } 313 return (sb.toString()); 314 } 315 316 334 335 public static void main(String [] args) throws IOException 336 { 337 if (args.length != 2) 338 { 339 System.out.println("usage: java CBJarResources <jar file name> <resource name>"); 340 System.exit(1); 341 } 342 CBJarResource jr = new CBJarResource(args[0]); 343 byte[] buff = jr.getResource(args[1]); 344 if (buff == null) 345 { 346 System.out.println("Could not find " + args[1] + "."); 347 } 348 else 349 { 350 System.out.println("Found " + args[1] + " (length=" + buff.length + ")."); 351 } 352 } 353 354 protected void finalize() 355 { 356 try 357 { 358 zipFile.close(); 359 } 360 catch (Exception e) 361 { 362 } 363 } 364 365 370 371 public String [] getPrefixedResources(String prefix) 372 { 373 prefix = prefix.toLowerCase(); 374 Vector results = new Vector(); 375 Enumeration keys = entries.keys(); 376 377 while (keys.hasMoreElements()) 379 { 380 String name = (String ) keys.nextElement(); 381 if (name.toLowerCase().startsWith(prefix)) 382 { 383 results.add(name); 384 } 385 } 386 387 388 if (results.size() == 0) 390 { 391 return new String []{}; 392 } 393 else 394 { 395 397 return (String []) results.toArray(new String [results.size()]); 398 399 406 } 407 } 408 409 } | Popular Tags |