1 11 12 package org.eclipse.update.internal.core; 13 14 import java.io.BufferedInputStream ; 15 import java.io.BufferedOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Date ; 25 import java.util.Enumeration ; 26 import java.util.jar.JarFile ; 27 import java.util.jar.JarOutputStream ; 28 import java.util.zip.ZipEntry ; 29 import java.util.zip.ZipException ; 30 31 import org.eclipse.core.runtime.CoreException; 32 import org.eclipse.update.core.IFeatureContentConsumer; 33 import org.eclipse.update.core.IPluginEntry; 34 import org.eclipse.update.core.PluginEntry; 35 import org.eclipse.update.core.Site; 36 37 public class JarDeltaInstallHandler extends DeltaInstallHandler { 38 39 40 protected void overlayPlugin( 41 IPluginEntry oldPlugin, 42 IPluginEntry newPlugin, 43 IFeatureContentConsumer consumer) 44 throws CoreException, IOException { 45 46 if(newPlugin instanceof PluginEntry && ((PluginEntry)newPlugin).isUnpack()){ 47 super.overlayPlugin(oldPlugin, newPlugin, consumer); 49 } 50 51 URL oldURI = null; 52 try { 53 oldURI = new URL (consumer.getFeature().getSite().getURL().getPath() + 54 Site.DEFAULT_PLUGIN_PATH + 55 oldPlugin.getVersionedIdentifier().toString()); 56 } catch (MalformedURLException e) { 57 throw new IOException (e.getMessage()); 58 } 59 File oldJarFile = new File (oldURI.toExternalForm()); 60 JarFile oldJar = new JarFile (oldJarFile); 61 62 URL newURI = null; 63 try { 64 newURI = new URL (consumer.getFeature().getSite().getURL().getPath() + 65 Site.DEFAULT_PLUGIN_PATH + 66 newPlugin.getVersionedIdentifier().toString()); 67 } catch (MalformedURLException e) { 68 throw new IOException (e.getMessage()); 69 } 70 File newJarFile = new File (newURI.toExternalForm()); 71 JarFile newJar = new JarFile (newJarFile); 72 73 String tempFileName = oldURI + "-" + (new Date ()).getTime(); File tempFile = new File (tempFileName); 75 FileOutputStream fos = new FileOutputStream (tempFile); 76 JarOutputStream jos = new JarOutputStream ( fos); 77 78 addToJar(jos, newJar); 79 addToJar(jos, oldJar); 80 81 jos.closeEntry(); 82 jos.finish(); 83 fos.close(); 84 newJar.close(); 85 oldJar.close(); 86 87 newJarFile = new File (newURI.toExternalForm()); 88 newJarFile.delete(); 89 90 newJarFile.createNewFile(); 91 92 copyFile(tempFile, newJarFile); 93 } 94 95 public static void copyFile(File src, File dst) throws IOException { 96 InputStream in=null; 97 OutputStream out=null; 98 try { 99 in = new BufferedInputStream (new FileInputStream (src)); 100 out = new BufferedOutputStream (new FileOutputStream (dst)); 101 byte[] buffer = new byte[4096]; 102 int len; 103 while ((len=in.read(buffer)) != -1) { 104 out.write(buffer, 0, len); 105 } 106 } finally { 107 if (in != null) 108 try { 109 in.close(); 110 } catch (IOException e) { 111 } 112 if (out != null) 113 try { 114 out.close(); 115 } catch (IOException e) { 116 } 117 } 118 } 119 120 public static void addToJar(JarOutputStream jos, JarFile jf) throws IOException { 121 Enumeration e = jf.entries(); 122 123 while(e.hasMoreElements()) { 124 125 ZipEntry je = (ZipEntry )e.nextElement(); 126 InputStream io = jf.getInputStream(je); 127 128 byte b[] = new byte[4096]; 129 int read = 0; 130 try { 131 jos.putNextEntry(je); 132 while( ( read = io.read(b, 0, 4096)) != -1) { 133 jos.write(b, 0, read); 134 } 135 } catch (ZipException ze) { 136 throw ze; 138 } 139 } 140 } 141 } 142 | Popular Tags |