1 11 package org.eclipse.update.core; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.List ; 18 import java.util.jar.*; 19 20 import org.eclipse.update.core.model.*; 21 import org.eclipse.update.internal.core.*; 22 23 38 public class JarContentReference extends ContentReference { 39 40 private JarFile jarFile; 42 43 50 public static class ContentSelector { 51 52 61 public boolean include(JarEntry entry) { 62 return entry == null ? false : !entry.isDirectory(); 63 } 64 65 73 public String defineIdentifier(JarEntry entry) { 74 return entry == null ? null : entry.getName(); 75 } 76 } 77 78 85 public JarContentReference(String id, URL url) { 86 super(id, url); 87 this.jarFile = null; 88 } 90 91 98 public JarContentReference(String id, File file) { 99 super(id, file); 100 this.jarFile = null; 101 } 103 104 112 public ContentReference createContentReference(String id, File file) { 113 return new JarContentReference(id, file,true); 114 } 115 121 public JarContentReference(String id, File file, boolean b) { 122 this(id,file); 123 setTempLocal(b); 124 } 125 126 135 protected JarFile asJarFile() throws IOException { 136 if (this.jarFile == null) { 137 File file = asFile(); 138 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) 139 UpdateCore.debug("asJarFile :" + file); if (file != null && !file.exists()) { 141 UpdateCore.warn("JarFile does not exits:" + file); throw new FileNotFoundException(file.getAbsolutePath()); 143 } 144 this.jarFile = new JarFile(file); 145 } 146 return jarFile; 147 } 148 149 161 public ContentReference[] unpack(File dir, ContentSelector selector, InstallMonitor monitor) throws IOException, InstallAbortedException { 162 163 if (selector == null) 165 selector = new ContentSelector(); 166 167 JarFile jarArchive = this.asJarFile(); 169 List content = new ArrayList (); 170 Enumeration entries = jarArchive.entries(); 171 172 String entryId; 174 JarEntry entry; 175 InputStream is; 176 OutputStream os; 177 File localFile; 178 try { 179 if (monitor != null) { 180 monitor.saveState(); 181 monitor.setTaskName(Messages.JarContentReference_Unpacking); 182 monitor.subTask(this.getIdentifier()); 183 monitor.showCopyDetails(false); 184 } 185 while (entries.hasMoreElements()) { 186 entry = (JarEntry) entries.nextElement(); 187 if (entry != null && selector.include(entry)) { 188 is = null; 189 os = null; 190 entryId = selector.defineIdentifier(entry); 191 localFile = Utilities.createLocalFile(dir, entryId); if (!entry.isDirectory()) { 193 try { 194 is = jarArchive.getInputStream(entry); 195 os = new FileOutputStream(localFile); 196 Utilities.copy(is, os, monitor); 197 } finally { 198 if (is != null) 199 try { 200 is.close(); 201 } catch (IOException e) { 202 } 203 if (os != null) 204 try { 205 os.close(); 206 } catch (IOException e) { 207 } 208 } 209 content.add(new ContentReference(entryId, localFile)); 210 } 211 } 212 } 213 } finally { 214 if (monitor != null) 215 monitor.restoreState(); 216 } 217 return (ContentReference[]) content.toArray(new ContentReference[0]); 218 } 219 220 233 public ContentReference unpack(File dir, String entryName, ContentSelector selector, InstallMonitor monitor) throws IOException, InstallAbortedException { 234 235 if (selector == null) 237 selector = new ContentSelector(); 238 239 JarFile jarArchive = this.asJarFile(); 241 entryName = entryName.replace(File.separatorChar, '/'); 242 JarEntry entry = jarArchive.getJarEntry(entryName); 243 String entryId; 244 if (entry != null) { 245 InputStream is = null; 246 OutputStream os = null; 247 entryId = selector.defineIdentifier(entry); 248 File localFile = Utilities.createLocalFile(dir, entryId); if (!entry.isDirectory()) { 250 try { 251 is = jarArchive.getInputStream(entry); 252 os = new FileOutputStream(localFile); 253 Utilities.copy(is, os, monitor); 254 } finally { 255 if (is != null) 256 try { 257 is.close(); 258 } catch (IOException e) { 259 } 260 if (os != null) 261 try { 262 os.close(); 263 } catch (IOException e) { 264 } 265 } 266 return new ContentReference(entryId, localFile); 267 } else 268 return null; } else 270 throw new FileNotFoundException(this.asFile().getAbsolutePath() + " " + entryName); } 272 273 283 public ContentReference[] peek(ContentSelector selector, InstallMonitor monitor) throws IOException { 284 285 if (selector == null) 287 selector = new ContentSelector(); 288 289 JarFile jarArchive = this.asJarFile(); 291 List content = new ArrayList (); 292 Enumeration entries = jarArchive.entries(); 293 294 JarEntry entry; 296 String entryId; 297 while (entries.hasMoreElements()) { 298 entry = (JarEntry) entries.nextElement(); 299 if (selector.include(entry)) { 300 entryId = selector.defineIdentifier(entry); 301 content.add(new JarEntryContentReference(entryId, this, entry)); 302 } 303 } 304 return (ContentReference[]) content.toArray(new ContentReference[0]); 305 } 306 307 319 public ContentReference peek(String entryName, ContentSelector selector, InstallMonitor monitor) throws IOException { 320 321 if (selector == null) 323 selector = new ContentSelector(); 324 325 JarFile jarArchive = this.asJarFile(); 327 entryName = entryName.replace(File.separatorChar, '/'); 328 JarEntry entry = jarArchive.getJarEntry(entryName); 329 if (entry == null) 330 return null; 331 332 String entryId = selector.defineIdentifier(entry); 333 return new JarEntryContentReference(entryId, this, entry); 334 } 335 336 342 public void closeArchive() throws IOException { 343 if (this.jarFile != null) { 344 this.jarFile.close(); 345 this.jarFile = null; 346 } 347 } 348 349 358 public static void shutdown() { 359 367 } 368 } 369 | Popular Tags |