1 package org.mortbay.util; 16 17 import java.io.File ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.JarURLConnection ; 22 import java.net.URL ; 23 import java.util.jar.JarEntry ; 24 import java.util.jar.JarInputStream ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 29 30 31 public class JarResource extends URLResource 32 { 33 private static Log log = LogFactory.getLog(JarResource.class); 34 35 protected transient JarURLConnection _jarConnection; 36 37 38 JarResource(URL url) 39 { 40 super(url,null); 41 } 42 43 44 public synchronized void release() 45 { 46 _jarConnection=null; 47 super.release(); 48 } 49 50 51 protected boolean checkConnection() 52 { 53 super.checkConnection(); 54 try 55 { 56 if (_jarConnection!=_connection) 57 newConnection(); 58 } 59 catch(IOException e) 60 { 61 LogSupport.ignore(log,e); 62 _jarConnection=null; 63 } 64 65 return _jarConnection!=null; 66 } 67 68 69 protected void newConnection() 70 throws IOException 71 { 72 _jarConnection=(JarURLConnection )_connection; 73 } 74 75 76 79 public boolean exists() 80 { 81 if (_urlString.endsWith("!/")) 82 return checkConnection(); 83 else 84 return super.exists(); 85 } 86 87 88 public File getFile() 89 throws IOException 90 { 91 return null; 92 } 93 94 95 public InputStream getInputStream() 96 throws java.io.IOException 97 { 98 if (!_urlString.endsWith("!/")) 99 return super.getInputStream(); 100 101 URL url = new URL (_urlString.substring(4,_urlString.length()-2)); 102 return url.openStream(); 103 } 104 105 106 public static void extract(Resource resource, File directory, boolean deleteOnExit) 107 throws IOException 108 { 109 if(log.isDebugEnabled())log.debug("Extract "+resource+" to "+directory); 110 JarInputStream jin = new JarInputStream (resource.getInputStream()); 111 JarEntry entry=null; 112 while((entry=jin.getNextJarEntry())!=null) 113 { 114 File file=new File (directory,entry.getName()); 115 if (entry.isDirectory()) 116 { 117 if (!file.exists()) 119 file.mkdirs(); 120 } 121 else 122 { 123 File dir = new File (file.getParent()); 125 if (!dir.exists()) 126 dir.mkdirs(); 127 128 FileOutputStream fout = null; 130 try 131 { 132 fout = new FileOutputStream (file); 133 IO.copy(jin,fout); 134 } 135 finally 136 { 137 IO.close(fout); 138 } 139 140 if (entry.getTime()>=0) 142 file.setLastModified(entry.getTime()); 143 } 144 if (deleteOnExit) 145 file.deleteOnExit(); 146 } 147 } 148 149 150 public void extract(File directory, boolean deleteOnExit) 151 throws IOException 152 { 153 extract(this,directory,deleteOnExit); 154 } 155 } 156 | Popular Tags |