1 11 12 package org.eclipse.osgi.baseadaptor.bundlefile; 13 14 import java.io.*; 15 import java.util.*; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipFile ; 18 import org.eclipse.osgi.baseadaptor.BaseData; 19 import org.eclipse.osgi.framework.debug.Debug; 20 import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg; 21 import org.eclipse.osgi.internal.baseadaptor.AdaptorUtil; 22 import org.eclipse.osgi.util.NLS; 23 import org.osgi.framework.FrameworkEvent; 24 25 29 public class ZipBundleFile extends BundleFile { 30 protected static MRUBundleFileList mruList = new MRUBundleFileList(); 31 32 35 protected BaseData bundledata; 36 39 protected ZipFile zipFile; 40 43 protected boolean closed = true; 44 45 51 public ZipBundleFile(File basefile, BaseData bundledata) throws IOException { 52 super(basefile); 53 if (!BundleFile.secureAction.exists(basefile)) 54 throw new IOException(NLS.bind(AdaptorMsg.ADAPTER_FILEEXIST_EXCEPTION, basefile)); 55 this.bundledata = bundledata; 56 this.closed = true; 57 } 58 59 63 protected boolean checkedOpen() { 64 try { 65 return getZipFile() != null; 66 } catch (IOException e) { 67 if (bundledata != null) 68 bundledata.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, bundledata.getBundle(), e); 69 return false; 70 } 71 } 72 73 78 protected ZipFile basicOpen() throws IOException { 79 return BundleFile.secureAction.getZipFile(this.basefile); 80 } 81 82 89 protected synchronized ZipFile getZipFile() throws IOException { 90 if (closed) { 91 mruList.add(this); 92 zipFile = basicOpen(); 93 closed = false; 94 } else 95 mruList.use(this); 96 return zipFile; 97 } 98 99 106 protected ZipEntry getZipEntry(String path) { 107 if (path.length() > 0 && path.charAt(0) == '/') 108 path = path.substring(1); 109 ZipEntry entry = zipFile.getEntry(path); 110 if (entry != null && entry.getSize() == 0 && !entry.isDirectory()) { 111 ZipEntry dirEntry = zipFile.getEntry(path + '/'); 113 if (dirEntry != null) 114 entry = dirEntry; 115 } 116 return entry; 117 } 118 119 126 protected synchronized File extractDirectory(String dirName) { 127 if (!checkedOpen()) 128 return null; 129 Enumeration entries = zipFile.entries(); 130 while (entries.hasMoreElements()) { 131 String entryPath = ((ZipEntry ) entries.nextElement()).getName(); 132 if (entryPath.startsWith(dirName) && !entryPath.endsWith("/")) getFile(entryPath, false); 134 } 135 return getExtractFile(dirName); 136 } 137 138 protected File getExtractFile(String entryName) { 139 if (bundledata == null) 140 return null; 141 String path = ".cp"; String name = entryName.replace('/', File.separatorChar); 143 if ((name.length() > 1) && (name.charAt(0) == File.separatorChar)) 144 path = path.concat(name); 145 else 146 path = path + File.separator + name; 147 return bundledata.getExtractFile(path); 148 } 149 150 public synchronized File getFile(String entry, boolean nativeCode) { 151 if (!checkedOpen()) 152 return null; 153 ZipEntry zipEntry = getZipEntry(entry); 154 if (zipEntry == null) 155 return null; 156 157 try { 158 File nested = getExtractFile(zipEntry.getName()); 159 if (nested != null) { 160 if (nested.exists()) { 161 162 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 163 Debug.println("File already present: " + nested.getPath()); if (nested.isDirectory()) 165 extractDirectory(zipEntry.getName()); 167 } else { 168 if (zipEntry.getName().endsWith("/")) { if (!nested.mkdirs()) { 170 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 171 Debug.println("Unable to create directory: " + nested.getPath()); throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, nested.getAbsolutePath())); 173 } 174 extractDirectory(zipEntry.getName()); 175 } else { 176 InputStream in = zipFile.getInputStream(zipEntry); 177 if (in == null) 178 return null; 179 180 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 181 Debug.println("Creating file: " + nested.getPath()); 183 File dir = new File (nested.getParent()); 184 if (!dir.exists() && !dir.mkdirs()) { 185 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 186 Debug.println("Unable to create directory: " + dir.getPath()); throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, dir.getAbsolutePath())); 188 } 189 190 AdaptorUtil.readFile(in, nested); 191 if (nativeCode) 192 setPermissions(nested); 193 } 194 } 195 196 return nested; 197 } 198 } catch (IOException e) { 199 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 200 Debug.printStackTrace(e); 201 } 202 return null; 203 } 204 205 public synchronized boolean containsDir(String dir) { 206 if (!checkedOpen()) 207 return false; 208 if (dir == null) 209 return false; 210 211 if (dir.length() == 0) 212 return true; 213 214 if (dir.charAt(0) == '/') { 215 if (dir.length() == 1) 216 return true; 217 dir = dir.substring(1); 218 } 219 220 if (dir.length() > 0 && dir.charAt(dir.length() - 1) != '/') 221 dir = dir + '/'; 222 223 Enumeration entries = zipFile.entries(); 224 ZipEntry zipEntry; 225 String entryPath; 226 while (entries.hasMoreElements()) { 227 zipEntry = (ZipEntry ) entries.nextElement(); 228 entryPath = zipEntry.getName(); 229 if (entryPath.startsWith(dir)) { 230 return true; 231 } 232 } 233 return false; 234 } 235 236 public synchronized BundleEntry getEntry(String path) { 237 if (!checkedOpen()) 238 return null; 239 ZipEntry zipEntry = getZipEntry(path); 240 if (zipEntry == null) { 241 if (path.length() == 0 || path.charAt(path.length() - 1) == '/') { 242 if (containsDir(path)) 244 return new DirZipBundleEntry(this, path); 245 } 246 return null; 247 } 248 249 return new ZipBundleEntry(zipEntry, this); 250 251 } 252 253 public synchronized Enumeration getEntryPaths(String path) { 254 if (!checkedOpen()) 255 return null; 256 if (path == null) 257 throw new NullPointerException (); 258 259 if (path.length() > 0 && path.charAt(0) == '/') 260 path = path.substring(1); 261 if (path.length() > 0 && path.charAt(path.length() - 1) != '/') 262 path = new StringBuffer (path).append("/").toString(); 264 Vector vEntries = new Vector(); 265 Enumeration entries = zipFile.entries(); 266 while (entries.hasMoreElements()) { 267 ZipEntry zipEntry = (ZipEntry ) entries.nextElement(); 268 String entryPath = zipEntry.getName(); 269 if (entryPath.startsWith(path)) { 270 if (path.length() < entryPath.length()) { 271 if (entryPath.lastIndexOf('/') < path.length()) { 272 vEntries.add(entryPath); 273 } else { 274 entryPath = entryPath.substring(path.length()); 275 int slash = entryPath.indexOf('/'); 276 entryPath = path + entryPath.substring(0, slash + 1); 277 if (!vEntries.contains(entryPath)) 278 vEntries.add(entryPath); 279 } 280 } 281 } 282 } 283 return vEntries.size() == 0 ? null : vEntries.elements(); 284 } 285 286 public synchronized void close() throws IOException { 287 if (!closed) { 288 closed = true; 289 zipFile.close(); 290 mruList.remove(this); 291 } 292 } 293 294 public void open() throws IOException { 295 } 297 298 301 public static void shutdown() { 302 mruList.shutdown(); 303 } 304 } 305 | Popular Tags |