1 11 package org.eclipse.jdt.ui.jarpackager; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Set ; 22 import java.util.jar.JarEntry ; 23 import java.util.jar.JarOutputStream ; 24 import java.util.jar.Manifest ; 25 import java.util.zip.CRC32 ; 26 import java.util.zip.ZipEntry ; 27 28 import org.eclipse.core.runtime.Assert; 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.OperationCanceledException; 32 33 import org.eclipse.core.resources.IFile; 34 import org.eclipse.core.resources.IProject; 35 import org.eclipse.core.resources.IResource; 36 import org.eclipse.core.resources.ResourcesPlugin; 37 38 import org.eclipse.swt.widgets.Shell; 39 40 41 import org.eclipse.jdt.internal.corext.util.Messages; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerMessages; 45 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil; 46 47 57 public class JarWriter { 58 private JarOutputStream fJarOutputStream; 59 private JarPackageData fJarPackage; 60 61 private Set fDirectories= new HashSet (); 62 63 75 public JarWriter(JarPackageData jarPackage, Shell parent) throws CoreException { 76 Assert.isNotNull(jarPackage, "The JAR specification is null"); fJarPackage= jarPackage; 78 Assert.isTrue(fJarPackage.isValid(), "The JAR package specification is invalid"); if (!canCreateJar(parent)) 80 throw new OperationCanceledException(); 81 82 try { 83 if (fJarPackage.usesManifest() && fJarPackage.areGeneratedFilesExported()) { 84 Manifest manifest= fJarPackage.getManifestProvider().create(fJarPackage); 85 fJarOutputStream= new JarOutputStream (new FileOutputStream (fJarPackage.getAbsoluteJarLocation().toOSString()), manifest); 86 } else 87 fJarOutputStream= new JarOutputStream (new FileOutputStream (fJarPackage.getAbsoluteJarLocation().toOSString())); 88 String comment= jarPackage.getComment(); 89 if (comment != null) 90 fJarOutputStream.setComment(comment); 91 } catch (IOException ex) { 92 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 93 } 94 } 95 96 103 public void close() throws CoreException { 104 if (fJarOutputStream != null) 105 try { 106 fJarOutputStream.close(); 107 registerInWorkspaceIfNeeded(); 108 } catch (IOException ex) { 109 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 110 } 111 } 112 113 122 public void write(IFile resource, IPath destinationPath) throws CoreException { 123 ByteArrayOutputStream output= new ByteArrayOutputStream (); 124 BufferedInputStream contentStream= null; 125 try { 126 contentStream= new BufferedInputStream (resource.getContents(false)); 127 int chunkSize= 4096; 128 byte[] readBuffer= new byte[chunkSize]; 129 int count; 130 while ((count= contentStream.read(readBuffer, 0, chunkSize)) != -1) 131 output.write(readBuffer, 0, count); 132 } catch (IOException ex) { 133 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 134 } finally { 135 try { 136 if (contentStream != null) 137 contentStream.close(); 138 } catch (IOException ex) { 139 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 140 } 141 } 142 try { 143 IPath fileLocation= resource.getLocation(); 144 long lastModified= System.currentTimeMillis(); 145 File file= null; 146 if (fileLocation != null) { 147 file= new File (fileLocation.toOSString()); 148 if (file.exists()) { 149 lastModified= file.lastModified(); 150 } 151 } 152 if (fJarPackage.areDirectoryEntriesIncluded()) 153 addDirectories(destinationPath, file); 154 write(destinationPath, output.toByteArray(), lastModified); 155 } catch (IOException ex) { 156 String message= null; 158 if (ex.getLocalizedMessage() != null) 159 message= Messages.format(JarPackagerMessages.JarWriter_writeProblemWithMessage, new Object [] {resource.getFullPath(), ex.getLocalizedMessage()}); 160 else 161 message= Messages.format(JarPackagerMessages.JarWriter_writeProblem, resource.getFullPath()); 162 throw JarPackagerUtil.createCoreException(message, ex); 163 } 164 } 165 166 175 protected void write(IPath path, byte[] contents, long lastModified) throws IOException { 176 JarEntry newEntry= new JarEntry (path.toString().replace(File.separatorChar, '/')); 177 if (fJarPackage.isCompressed()) 178 newEntry.setMethod(ZipEntry.DEFLATED); 179 else { 181 newEntry.setMethod(ZipEntry.STORED); 182 newEntry.setSize(contents.length); 183 CRC32 checksumCalculator= new CRC32 (); 184 checksumCalculator.update(contents); 185 newEntry.setCrc(checksumCalculator.getValue()); 186 } 187 188 newEntry.setTime(lastModified); 190 191 try { 192 fJarOutputStream.putNextEntry(newEntry); 193 fJarOutputStream.write(contents); 194 } finally { 195 201 } 203 } 204 205 213 private void addDirectories(IPath destinationPath, File correspondingFile) throws IOException { 214 String path= destinationPath.toString().replace(File.separatorChar, '/'); 215 int lastSlash= path.lastIndexOf('/'); 216 List directories= new ArrayList (2); 217 while(lastSlash != -1) { 218 path= path.substring(0, lastSlash + 1); 219 if (!fDirectories.add(path)) 220 break; 221 222 if (correspondingFile != null) 223 correspondingFile= correspondingFile.getParentFile(); 224 long timeStamp= correspondingFile != null && correspondingFile.exists() 225 ? correspondingFile.lastModified() 226 : System.currentTimeMillis(); 227 228 JarEntry newEntry= new JarEntry (path); 229 newEntry.setMethod(ZipEntry.STORED); 230 newEntry.setSize(0); 231 newEntry.setCrc(0); 232 newEntry.setTime(timeStamp); 233 directories.add(newEntry); 234 235 lastSlash= path.lastIndexOf('/', lastSlash - 1); 236 } 237 238 for(int i= directories.size() - 1; i >= 0; --i) { 239 fJarOutputStream.putNextEntry((JarEntry )directories.get(i)); 240 } 241 } 242 243 252 protected boolean canCreateJar(Shell parent) { 253 File file= fJarPackage.getAbsoluteJarLocation().toFile(); 254 if (file.exists()) { 255 if (!file.canWrite()) 256 return false; 257 if (fJarPackage.allowOverwrite()) 258 return true; 259 return parent != null && JarPackagerUtil.askForOverwritePermission(parent, fJarPackage.getAbsoluteJarLocation().toOSString()); 260 } 261 262 String path= file.getAbsolutePath(); 264 int separatorIndex = path.lastIndexOf(File.separator); 265 if (separatorIndex == -1) return true; 267 File directory= new File (path.substring(0, separatorIndex)); 268 if (!directory.exists()) { 269 if (JarPackagerUtil.askToCreateDirectory(parent, directory)) 270 return directory.mkdirs(); 271 else 272 return false; 273 } 274 return true; 275 } 276 277 private void registerInWorkspaceIfNeeded() { 278 IPath jarPath= fJarPackage.getAbsoluteJarLocation(); 279 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 280 for (int i= 0; i < projects.length; i++) { 281 IProject project= projects[i]; 282 IPath projectLocation= project.getLocation(); 286 if (projectLocation != null && projectLocation.isPrefixOf(jarPath)) { 287 try { 288 jarPath= jarPath.removeFirstSegments(projectLocation.segmentCount()); 289 jarPath= jarPath.removeLastSegments(1); 290 IResource containingFolder= project.findMember(jarPath); 291 if (containingFolder != null && containingFolder.isAccessible()) 292 containingFolder.refreshLocal(IResource.DEPTH_ONE, null); 293 } catch (CoreException ex) { 294 JavaPlugin.log(ex); 296 } 297 } 298 } 299 } 300 } 301 | Popular Tags |