1 11 package org.eclipse.jdt.ui.jarpackager; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.HashSet ; 19 import java.util.List ; 20 import java.util.Set ; 21 import java.util.jar.JarEntry ; 22 import java.util.jar.JarOutputStream ; 23 import java.util.jar.Manifest ; 24 import java.util.zip.CRC32 ; 25 import java.util.zip.ZipEntry ; 26 27 import org.eclipse.core.runtime.Assert; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IPath; 30 import org.eclipse.core.runtime.OperationCanceledException; 31 32 import org.eclipse.core.resources.IFile; 33 import org.eclipse.core.resources.IProject; 34 import org.eclipse.core.resources.IResource; 35 import org.eclipse.core.resources.ResourcesPlugin; 36 37 import org.eclipse.swt.widgets.Shell; 38 39 40 import org.eclipse.jdt.internal.corext.util.Messages; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerMessages; 44 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil; 45 46 57 public class JarWriter2 { 58 59 private JarOutputStream fJarOutputStream; 60 private JarPackageData fJarPackage; 61 62 private Set fDirectories= new HashSet (); 63 64 76 public JarWriter2(JarPackageData jarPackage, Shell parent) throws CoreException { 77 Assert.isNotNull(jarPackage, "The JAR specification is null"); fJarPackage= jarPackage; 79 Assert.isTrue(fJarPackage.isValid(), "The JAR package specification is invalid"); if (!canCreateJar(parent)) 81 throw new OperationCanceledException(); 82 83 try { 84 if (fJarPackage.usesManifest() && fJarPackage.areGeneratedFilesExported()) { 85 Manifest manifest= fJarPackage.getManifestProvider().create(fJarPackage); 86 fJarOutputStream= new JarOutputStream (new FileOutputStream (fJarPackage.getAbsoluteJarLocation().toOSString()), manifest); 87 } else 88 fJarOutputStream= new JarOutputStream (new FileOutputStream (fJarPackage.getAbsoluteJarLocation().toOSString())); 89 String comment= jarPackage.getComment(); 90 if (comment != null) 91 fJarOutputStream.setComment(comment); 92 } catch (IOException ex) { 93 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 94 } 95 } 96 97 104 public void close() throws CoreException { 105 if (fJarOutputStream != null) 106 try { 107 fJarOutputStream.close(); 108 registerInWorkspaceIfNeeded(); 109 } catch (IOException ex) { 110 throw JarPackagerUtil.createCoreException(ex.getLocalizedMessage(), ex); 111 } 112 } 113 114 123 public void write(IFile resource, IPath destinationPath) throws CoreException { 124 try { 125 IPath fileLocation= resource.getLocation(); 126 File file= null; 127 if (fileLocation != null) { 128 file= new File (fileLocation.toOSString()); 129 } 130 if (fJarPackage.areDirectoryEntriesIncluded()) 131 addDirectories(destinationPath, file); 132 addFile(resource, destinationPath, file); 133 } catch (IOException ex) { 134 String message= null; 136 if (ex.getLocalizedMessage() != null) 137 message= Messages.format(JarPackagerMessages.JarWriter_writeProblemWithMessage, new Object [] {resource.getFullPath(), ex.getLocalizedMessage()}); 138 else 139 message= Messages.format(JarPackagerMessages.JarWriter_writeProblem, resource.getFullPath()); 140 throw JarPackagerUtil.createCoreException(message, ex); 141 } 142 } 143 144 155 protected void addFile(IFile resource, IPath path, File correspondingFile) throws IOException , CoreException { 156 JarEntry newEntry= new JarEntry (path.toString().replace(File.separatorChar, '/')); 157 byte[] readBuffer= new byte[4096]; 158 159 if (fJarPackage.isCompressed()) 160 newEntry.setMethod(ZipEntry.DEFLATED); 161 else { 163 newEntry.setMethod(ZipEntry.STORED); 164 calculateCrcAndSize(newEntry, resource, readBuffer); 165 } 166 167 long lastModified= correspondingFile != null && correspondingFile.exists() ? correspondingFile.lastModified() : System.currentTimeMillis(); 168 newEntry.setTime(lastModified); 170 171 InputStream contentStream = resource.getContents(false); 172 173 try { 174 fJarOutputStream.putNextEntry(newEntry); 175 int count; 176 while ((count= contentStream.read(readBuffer, 0, readBuffer.length)) != -1) 177 fJarOutputStream.write(readBuffer, 0, count); 178 } finally { 179 if (contentStream != null) 180 contentStream.close(); 181 182 188 } 190 } 191 192 202 private void calculateCrcAndSize(JarEntry jarEntry, IFile resource, byte[] readBuffer) throws IOException , CoreException { 203 InputStream contentStream = resource.getContents(false); 204 int size = 0; 205 CRC32 checksumCalculator= new CRC32 (); 206 int count; 207 try { 208 while ((count= contentStream.read(readBuffer, 0, readBuffer.length)) != -1) { 209 checksumCalculator.update(readBuffer, 0, count); 210 size += count; 211 } 212 } finally { 213 if (contentStream != null) 214 contentStream.close(); 215 } 216 jarEntry.setSize(size); 217 jarEntry.setCrc(checksumCalculator.getValue()); 218 } 219 220 229 protected void addDirectories(IPath destinationPath, File correspondingFile) throws IOException { 230 String path= destinationPath.toString().replace(File.separatorChar, '/'); 231 int lastSlash= path.lastIndexOf('/'); 232 List directories= new ArrayList (2); 233 while(lastSlash != -1) { 234 path= path.substring(0, lastSlash + 1); 235 if (!fDirectories.add(path)) 236 break; 237 238 if (correspondingFile != null) 239 correspondingFile= correspondingFile.getParentFile(); 240 long timeStamp= correspondingFile != null && correspondingFile.exists() 241 ? correspondingFile.lastModified() 242 : System.currentTimeMillis(); 243 244 JarEntry newEntry= new JarEntry (path); 245 newEntry.setMethod(ZipEntry.STORED); 246 newEntry.setSize(0); 247 newEntry.setCrc(0); 248 newEntry.setTime(timeStamp); 249 directories.add(newEntry); 250 251 lastSlash= path.lastIndexOf('/', lastSlash - 1); 252 } 253 254 for(int i= directories.size() - 1; i >= 0; --i) { 255 fJarOutputStream.putNextEntry((JarEntry )directories.get(i)); 256 } 257 } 258 259 268 protected boolean canCreateJar(Shell parent) { 269 File file= fJarPackage.getAbsoluteJarLocation().toFile(); 270 if (file.exists()) { 271 if (!file.canWrite()) 272 return false; 273 if (fJarPackage.allowOverwrite()) 274 return true; 275 return parent != null && JarPackagerUtil.askForOverwritePermission(parent, fJarPackage.getAbsoluteJarLocation().toOSString()); 276 } 277 278 String path= file.getAbsolutePath(); 280 int separatorIndex = path.lastIndexOf(File.separator); 281 if (separatorIndex == -1) return true; 283 File directory= new File (path.substring(0, separatorIndex)); 284 if (!directory.exists()) { 285 if (JarPackagerUtil.askToCreateDirectory(parent, directory)) 286 return directory.mkdirs(); 287 else 288 return false; 289 } 290 return true; 291 } 292 293 private void registerInWorkspaceIfNeeded() { 294 IPath jarPath= fJarPackage.getAbsoluteJarLocation(); 295 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 296 for (int i= 0; i < projects.length; i++) { 297 IProject project= projects[i]; 298 IPath projectLocation= project.getLocation(); 302 if (projectLocation != null && projectLocation.isPrefixOf(jarPath)) { 303 try { 304 jarPath= jarPath.removeFirstSegments(projectLocation.segmentCount()); 305 jarPath= jarPath.removeLastSegments(1); 306 IResource containingFolder= project.findMember(jarPath); 307 if (containingFolder != null && containingFolder.isAccessible()) 308 containingFolder.refreshLocal(IResource.DEPTH_ONE, null); 309 } catch (CoreException ex) { 310 JavaPlugin.log(ex); 312 } 313 } 314 } 315 } 316 } 317 | Popular Tags |