1 11 package org.eclipse.compare; 12 13 import java.io.*; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.zip.*; 17 18 import org.eclipse.core.runtime.CoreException; 19 20 import org.eclipse.swt.graphics.Image; 21 22 import org.eclipse.jface.util.Assert; 23 24 import org.eclipse.compare.internal.*; 25 import org.eclipse.compare.structuremergeviewer.*; 26 27 28 37 public class ZipFileStructureCreator implements IStructureCreator { 38 39 42 static abstract class ZipResource implements IStructureComparator, ITypedElement { 43 44 private String fName; 45 46 ZipResource(String name) { 47 fName= name; 48 } 49 50 public String getName() { 51 return fName; 52 } 53 54 public Image getImage() { 55 return CompareUI.getImage(getType()); 56 } 57 58 62 public boolean equals(Object other) { 63 if (other instanceof ITypedElement) 64 return fName.equals(((ITypedElement) other).getName()); 65 return super.equals(other); 66 } 67 68 public int hashCode() { 69 return fName.hashCode(); 70 } 71 } 72 73 static class ZipFolder extends ZipResource { 74 75 private HashMap fChildren= new HashMap (10); 76 77 ZipFolder(String name) { 78 super(name); 79 } 80 81 public String getType() { 82 return ITypedElement.FOLDER_TYPE; 83 } 84 85 public Object [] getChildren() { 86 Object [] children= new Object [fChildren.size()]; 87 Iterator iter= fChildren.values().iterator(); 88 for (int i= 0; iter.hasNext(); i++) 89 children[i]= iter.next(); 90 return children; 91 } 92 93 ZipFile createContainer(String path) { 94 String entry= path; 95 int pos= path.indexOf('/'); 96 if (pos < 0) 97 pos= path.indexOf('\\'); 98 if (pos >= 0) { 99 entry= path.substring(0, pos); 100 path= path.substring(pos + 1); 101 } else if (entry.length() > 0) { 102 if (CompareUIPlugin.getDefault().filter(path, false, true)) 103 return null; 104 ZipFile ze= new ZipFile(entry); 105 fChildren.put(entry, ze); 106 return ze; 107 } else 108 return null; 109 110 ZipFolder folder= null; 111 if (fChildren != null) { 112 Object o= fChildren.get(entry); 113 if (o instanceof ZipFolder) 114 folder= (ZipFolder) o; 115 } 116 117 if (folder == null) { 118 if (path.length() > 0 && CompareUIPlugin.getDefault().filter(path, true, true)) 119 return null; 120 folder= new ZipFolder(entry); 121 fChildren.put(entry, folder); 122 } 123 124 return folder.createContainer(path); 125 } 126 } 127 128 static class ZipFile extends ZipResource implements IStreamContentAccessor { 129 130 private byte[] fContents; 131 132 ZipFile(String name) { 133 super(name); 134 } 135 136 public String getType() { 137 String s= this.getName(); 138 int pos= s.lastIndexOf('.'); 139 if (pos >= 0) 140 return s.substring(pos + 1); 141 return ITypedElement.UNKNOWN_TYPE; 142 } 143 144 public Object [] getChildren() { 145 return null; 146 } 147 148 public InputStream getContents() { 149 if (fContents == null) 150 fContents= new byte[0]; 151 return new ByteArrayInputStream(fContents); 152 } 153 154 byte[] getBytes() { 155 return fContents; 156 } 157 158 void setBytes(byte[] buffer) { 159 fContents= buffer; 160 } 161 162 void appendBytes(byte[] buffer, int length) { 163 if (length > 0) { 164 int oldLen= 0; 165 if (fContents != null) 166 oldLen= fContents.length; 167 byte[] newBuf= new byte[oldLen + length]; 168 if (oldLen > 0) 169 System.arraycopy(fContents, 0, newBuf, 0, oldLen); 170 System.arraycopy(buffer, 0, newBuf, oldLen, length); 171 fContents= newBuf; 172 } 173 } 174 } 175 176 private String fTitle; 177 178 181 public ZipFileStructureCreator() { 182 this(Utilities.getString("ZipStructureCreator.name")); } 184 185 190 public ZipFileStructureCreator(String title) { 191 fTitle= title; 192 } 193 194 public String getName() { 195 return fTitle; 196 } 197 198 public IStructureComparator getStructure(Object input) { 199 200 InputStream is= null; 201 202 if (input instanceof IStreamContentAccessor) { 203 IStreamContentAccessor sca= (IStreamContentAccessor) input; 204 try { 205 is= sca.getContents(); 206 } catch (CoreException ex) { 207 } 209 } 210 211 if (is == null) 212 return null; 213 214 ZipInputStream zip= new ZipInputStream(is); 215 ZipFolder root= new ZipFolder(""); try { 217 for (;;) { 218 ZipEntry entry= zip.getNextEntry(); 219 if (entry == null) 220 break; 221 222 ZipFile ze= root.createContainer(entry.getName()); 223 if (ze != null) { 224 int length= (int) entry.getSize(); 225 if (length >= 0) { 226 byte[] buffer= new byte[length]; 227 int offset= 0; 228 229 do { 230 int n= zip.read(buffer, offset, length); 231 offset += n; 232 length -= n; 233 } while (length > 0); 234 235 ze.setBytes(buffer); 236 } else { 237 byte[] buffer= new byte[1024]; 238 int n; 239 do { 240 n= zip.read(buffer, 0, 1024); 241 ze.appendBytes(buffer, n); 242 } while (n >= 0); 243 } 244 } 245 zip.closeEntry(); 246 } 247 } catch (IOException ex) { 248 return null; 249 } finally { 250 try { 251 zip.close(); 252 } catch (IOException ex) { 253 } 255 } 256 257 if (root.fChildren.size() == 1) { 258 Iterator iter= root.fChildren.values().iterator(); 259 return (IStructureComparator) iter.next(); 260 } 261 return root; 262 } 263 264 public String getContents(Object o, boolean ignoreWhitespace) { 265 if (o instanceof ZipFile) { 266 byte[] bytes= ((ZipFile)o).getBytes(); 267 if (bytes != null) 268 return new String (bytes); 269 return ""; } 271 return null; 272 } 273 274 278 public boolean canSave() { 279 return false; 280 } 281 282 290 public void save(IStructureComparator structure, Object input) { 291 Assert.isTrue(false); } 293 294 public IStructureComparator locate(Object path, Object source) { 295 return null; 296 } 297 298 306 public boolean canRewriteTree() { 307 return false; 308 } 309 310 319 public void rewriteTree(Differencer differencer, IDiffContainer root) { 320 } 322 } 323 324 | Popular Tags |