1 26 package org.objectweb.util.explorer.parser.lib; 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.Enumeration ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Vector ; 39 import java.util.zip.ZipEntry ; 40 import java.util.zip.ZipException ; 41 import java.util.zip.ZipFile ; 42 43 import org.objectweb.util.explorer.core.common.lib.BasicLoggable; 44 import org.objectweb.util.explorer.core.common.lib.ClassResolver; 45 import org.objectweb.util.explorer.explorerConfig.Zip; 46 import org.objectweb.util.explorer.explorerConfig.beans.ExplorerBean; 47 import org.objectweb.util.explorer.parser.api.ExplorerParser; 48 49 57 public class ZipManager 58 extends BasicLoggable 59 implements ExplorerParser 60 { 61 62 68 69 protected File extractedFolder_ = null; 70 71 77 80 public ZipManager(){ 81 System.runFinalizersOnExit(true); 83 } 84 85 91 94 protected void remove(File file){ 95 if(file!=null) { 96 if(file.isDirectory()){ 97 File [] children = file.listFiles(); 98 for(int i = 0 ; i < children.length ; i++) 99 remove(children[i]); 100 } 101 file.delete(); 102 } 103 } 104 105 109 protected void writeFile(InputStream input, OutputStream output) throws IOException { 110 int bufferSize = 1024; 111 byte[] buffer = new byte[bufferSize]; 112 int length=0; 113 while((length = input.read(buffer))>0){ 114 output.write(buffer, 0, length); 115 } 116 output.close(); 117 input.close(); 118 } 119 120 126 protected void extractEntry(ZipFile file, ZipEntry entry, String fileName) throws IOException { 127 writeFile(file.getInputStream(entry), new FileOutputStream (fileName)); 128 } 129 130 137 141 protected void createDirectory(String path){ 142 File directory = new File (path); 143 if(!directory.exists()){ 144 directory.deleteOnExit(); 146 directory.mkdirs(); 147 } 148 } 149 150 153 protected void extractFile(File file, Vector javaArchives) throws IOException { 154 ZipFile zipFile = null; 155 try { 156 zipFile = new ZipFile (file.toString()); 157 } catch (ZipException e) { 158 } finally { 160 file.deleteOnExit(); 161 } 162 if(zipFile!=null){ 163 for(Enumeration entries = zipFile.entries();entries.hasMoreElements();){ 164 ZipEntry zipEntry = (ZipEntry )entries.nextElement(); 165 if(zipEntry.isDirectory()){ 166 createDirectory(extractedFolder_.getCanonicalPath() + "/" + zipEntry.getName()); 167 } else { 168 int indice = zipEntry.getName().lastIndexOf('/'); 170 if(indice!=-1){ 171 createDirectory(extractedFolder_.getCanonicalPath() + "/" + zipEntry.getName().substring(0,indice)); 172 } 173 String fileName = extractedFolder_.getCanonicalPath() + "/" + zipEntry.getName(); 175 try { 177 extractEntry(zipFile, zipEntry, fileName); 178 } catch (IOException e) { 179 getTrace().warn("IOException: " + e.getMessage()); 180 e.printStackTrace(); 181 } 182 if(zipEntry.getName().toLowerCase().endsWith(".jar")) { 183 File jarFile = new File (fileName); 185 jarFile.deleteOnExit(); 186 try { 187 javaArchives.add(jarFile.toURL()); 188 } catch (IOException e) { 189 getTrace().warn("IOException: " + e.getMessage()); 190 e.printStackTrace(); 191 } 192 } else { 193 extractFile(new File (fileName), javaArchives); 194 } 195 } 196 } 197 } 198 } 199 200 204 protected void loadZip(URL zipURL) throws IOException { 205 if(zipURL!=null){ 206 Vector javaArchives = new Vector (); 207 208 String fileName = extractedFolder_.getCanonicalPath() + "/temp.zip"; 210 writeFile(zipURL.openStream(), new FileOutputStream (fileName)); 211 212 File zipFile = new File (fileName); 214 zipFile.deleteOnExit(); 215 extractFile(zipFile, javaArchives); 216 217 ClassResolver.addContext((URL [])javaArchives.toArray(new URL [0])); 219 } 220 } 221 222 227 protected void loadZips(List zipList){ 228 if(zipList!=null && zipList.size() > 0){ 229 if(extractedFolder_== null){ 230 String tmpDir = System.getProperty("java.io.tmpdir"); 232 if ((!tmpDir.endsWith("/")) && (!tmpDir.endsWith("\\"))) 233 tmpDir = tmpDir + "/"; 234 extractedFolder_ = new File (tmpDir + "explorer" + System.currentTimeMillis()); 235 extractedFolder_.mkdir(); 236 extractedFolder_.deleteOnExit(); 237 } 238 for(Iterator i = zipList.iterator() ; i.hasNext() ; ){ 239 Zip zip = (Zip)i.next(); 240 try { 241 loadZip(new URL (zip.getUrl())); 242 } catch (MalformedURLException e) { 243 getTrace().warn("MalformedURLException: " + e.getMessage()); 244 } catch (IOException e) { 245 getTrace().warn("IOException: " + e.getMessage()); 246 } 247 } 248 } 249 } 250 251 257 260 public void parseExplorer(ExplorerBean explorer) { 261 loadZips(explorer.getZipList()); 262 } 263 264 270 275 public void finalize() throws Throwable { 276 if(extractedFolder_!=null && extractedFolder_.exists()){ 277 remove(extractedFolder_); 278 } 279 } 280 } 281 | Popular Tags |