1 26 27 28 package org.objectweb.openccm.descriptor; 29 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.util.LinkedList ; 36 import java.util.zip.*; 37 import org.objectweb.openccm.descriptor.StreamManager; 38 39 43 public class ZipEntryRetriever { 44 50 private ZipFile zf; 52 private File tmpFile; 54 private java.util.Hashtable table; 56 57 private StreamManager streamManager; 58 59 65 68 private void 69 setIdentifierTable() 70 { 71 String entryName; 72 73 for(java.util.Enumeration entries=zf.entries(); 74 entries.hasMoreElements();) 75 { 76 entryName=((java.util.zip.ZipEntry )entries.nextElement()).getName(); 77 table.put(entryName.replace('\\','/').trim(),entryName); 78 } 79 80 } 81 82 88 93 public ZipEntryRetriever(ZipFile zf){ 94 this.zf=zf; 95 table=new java.util.Hashtable (); 96 setIdentifierTable(); 97 } 98 99 109 public 110 ZipEntryRetriever(InputStream in) 111 throws Exception 112 { 113 tmpFile=File.createTempFile("componentarchive"+System.currentTimeMillis(),".car"); 114 FileOutputStream out=new FileOutputStream (tmpFile); 115 byte [] byteArray= new byte[32000]; 116 117 for(int readed=in.read(byteArray,0,32000); readed>0; 118 readed=in.read(byteArray,0,32000)) 119 out.write(byteArray,0,readed); 120 121 out.flush(); 122 out.close(); 123 zf=new ZipFile(tmpFile); 124 table=new java.util.Hashtable (); 125 setIdentifierTable(); 126 } 128 138 public 139 ZipEntryRetriever(File file) 140 throws Exception 141 { 142 tmpFile=file; 143 zf=new ZipFile(tmpFile); 144 table=new java.util.Hashtable (); 146 setIdentifierTable(); 147 } 148 149 155 161 public ZipEntry 162 retrieveZipEntryIgnoringDelimiterType(String entryName) 163 throws java.util.zip.ZipException , java.io.IOException 164 { 165 if(!table.containsKey(entryName.replace('\\','/').trim())) 166 throw new java.util.zip.ZipException ("Cannot found zip entry:"+entryName) ; 167 168 String originalName=(String )table.get(entryName.replace('\\','/')); 169 return zf.getEntry(originalName); 170 } 171 172 179 public String [] 180 getZipEntriesEndingBy(String ending) 181 { 182 LinkedList ll=new LinkedList (); 183 for(java.util.Enumeration entries=zf.entries(); 184 entries.hasMoreElements();) 185 { 186 ZipEntry ze=(java.util.zip.ZipEntry )entries.nextElement(); 187 if(ze.getName().endsWith(ending)) 188 ll.add(ze.getName()); 189 } 190 return (String [])ll.toArray(new String [ll.size()]); 191 } 192 193 199 public java.io.InputStream 200 getZipEntryISIgnoringDelimiterType(String entryName) 201 throws java.util.zip.ZipException ,java.io.IOException 202 { 203 java.io.InputStream is= zf.getInputStream(retrieveZipEntryIgnoringDelimiterType(entryName)); 204 getStreamManager().connectOpenInputStream(is); 205 return is; 206 } 207 208 215 public InputStream 216 getNewInputStream() 217 throws IOException 218 { 219 InputStream is= 220 new FileInputStream (tmpFile); 221 getStreamManager().connectOpenInputStream(is); 222 return is; 223 } 224 225 229 public void 230 connectStreamManager(StreamManager streamManager) 231 { 232 this.streamManager=streamManager; 233 getStreamManager().connectOpenZipFile(zf); 234 if(tmpFile!=null) 235 getStreamManager().connectTemporaryFile(tmpFile); 236 } 237 238 241 public StreamManager 242 getStreamManager() 243 { 244 return streamManager; 245 } 246 } 247 | Popular Tags |