1 23 24 package com.sun.enterprise.connectors.util; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.Logger ; 29 import java.util.zip.*; 30 31 import com.sun.logging.LogDomains; 32 33 34 40 41 public final class JarResourceExtractor { 42 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 43 44 private Hashtable htJarContents = new Hashtable(); 46 47 54 public JarResourceExtractor(String jarFileName) { 55 init(jarFileName); 56 } 57 58 64 public byte[] getResource(String name) { 65 _logger.finer("getResource: " + name); 66 return (byte[]) htJarContents.get(name); 67 } 68 69 70 private void init(String jarFileName) { 71 try { 72 FileInputStream fis = new FileInputStream(jarFileName); 74 BufferedInputStream bis = new BufferedInputStream(fis); 75 ZipInputStream zis = new ZipInputStream(bis); 76 extractResources(zis); 77 } catch (Exception ex){ 78 ex.printStackTrace(); 79 } 80 81 } 82 83 87 private void extractResources(ZipInputStream zis) throws FileNotFoundException, IOException { 88 ZipEntry ze = null; 89 while ((ze = zis.getNextEntry()) != null) { 90 _logger.finer("ExtractResources : " + ze.getName()); 91 extractZipEntryContents(ze, zis); 92 } 93 } 94 95 99 private void extractZipEntryContents(ZipEntry ze, ZipInputStream zis) throws IOException { 100 if (ze.isDirectory()) { 101 return; 102 } 103 104 _logger.finer("ze.getName()=" + ze.getName() + "," 105 + "getSize()=" + ze.getSize()); 106 107 byte[] b = getZipEntryContents(ze,zis); 108 if(ze.getName().trim().endsWith(".jar")){ 110 _logger.finer("JAR - going into it !!"); 111 BufferedInputStream bis = new BufferedInputStream( (new ByteArrayInputStream(b))); 112 extractResources(new ZipInputStream(bis)); 113 } else { 114 htJarContents.put(ze.getName(), b ); 116 if (ze.getName().trim().endsWith("class")){ 117 _logger.finer("CLASS added " + ze.getName()); 118 } 119 _logger.finer(ze.getName() + ",size=" 120 + b.length + ",csize=" + ze.getCompressedSize()); 121 } 122 } 123 124 private byte[] getZipEntryContents(ZipEntry ze, ZipInputStream zis) throws IOException{ 125 int size = (int) ze.getSize(); 126 127 byte[] b = null; 128 if (size != -1) { 130 b = new byte[(int) size]; 132 133 int rb = 0; 134 int chunk = 0; 135 136 while (((int) size - rb) > 0) { 137 chunk = zis.read(b, rb, (int) size - rb); 138 if (chunk == -1) { 139 break; 140 } 141 rb += chunk; 142 } 143 } else { 144 ArrayList al = new ArrayList(); 146 int c = 0; 147 while( (c = zis.read()) != -1) { 148 al.add(new Byte ((byte)c)); 149 } 150 Byte [] btArr = (Byte [])al.toArray(new Byte []{}); 151 b = new byte[btArr.length]; 152 _logger.finer("ByteArray length" + btArr.length); 153 for (int i = 0; i < btArr.length; i++) { 154 b[i] = btArr[i].byteValue(); 155 } 156 } 157 158 return b; 159 } 160 } 161 | Popular Tags |