1 10 package org.jgap.util; 11 12 import java.io.*; 13 import java.util.*; 14 import java.util.zip.*; 15 16 25 public final class JarResources { 26 27 private final static String CVS_REVISION = "$Revision: 1.1 $"; 28 29 public boolean debugOn = false; 31 32 private Hashtable htSizes = new Hashtable(); 34 35 private Hashtable htJarContents = new Hashtable(); 36 37 private String jarFileName; 39 40 45 public JarResources(String jarFileName) { 46 this.jarFileName = jarFileName; 47 init(); 48 } 49 50 54 public byte[] getResource(String name) { 55 return (byte[]) htJarContents.get(name); 56 } 57 58 61 private void init() { 62 try { 63 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 ( (int) ze.getSize())); 72 } 73 zf.close(); 74 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 if (size == -1) { 91 size = ( (Integer ) 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 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 e) { 114 System.out.println("done."); 115 } catch (FileNotFoundException e) { 116 e.printStackTrace(); 117 } catch (IOException e) { 118 e.printStackTrace(); 119 } 120 } 121 122 126 private String dumpZipEntry(ZipEntry ze) { 127 StringBuffer sb = new StringBuffer (); 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 167 public static void main(String [] 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 |