1 16 17 package org.apache.poi.hpsf.examples; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.UnsupportedEncodingException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import org.apache.poi.hpsf.HPSFRuntimeException; 34 import org.apache.poi.hpsf.MarkUnsupportedException; 35 import org.apache.poi.hpsf.MutablePropertySet; 36 import org.apache.poi.hpsf.NoPropertySetStreamException; 37 import org.apache.poi.hpsf.PropertySet; 38 import org.apache.poi.hpsf.PropertySetFactory; 39 import org.apache.poi.hpsf.Util; 40 import org.apache.poi.hpsf.WritingNotSupportedException; 41 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 42 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 43 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 44 import org.apache.poi.poifs.filesystem.DirectoryEntry; 45 import org.apache.poi.poifs.filesystem.DocumentEntry; 46 import org.apache.poi.poifs.filesystem.DocumentInputStream; 47 import org.apache.poi.poifs.filesystem.Entry; 48 import org.apache.poi.poifs.filesystem.POIFSDocumentPath; 49 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 50 import org.apache.poi.util.TempFile; 51 52 77 public class CopyCompare 78 { 79 105 public static void main(final String [] args) 106 throws NoPropertySetStreamException, MarkUnsupportedException, 107 UnsupportedEncodingException , IOException 108 { 109 String originalFileName = null; 110 String copyFileName = null; 111 112 113 if (args.length == 1) 114 { 115 originalFileName = args[0]; 116 File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2"); 117 f.deleteOnExit(); 118 copyFileName = f.getAbsolutePath(); 119 } 120 else if (args.length == 2) 121 { 122 originalFileName = args[0]; 123 copyFileName = args[1]; 124 } 125 else 126 { 127 System.err.println("Usage: " + CopyCompare.class.getName() + 128 "originPOIFS [copyPOIFS]"); 129 System.exit(1); 130 } 131 132 134 final POIFSReader r = new POIFSReader(); 135 final CopyFile cf = new CopyFile(copyFileName); 136 r.registerListener(cf); 137 r.read(new FileInputStream (originalFileName)); 138 139 140 cf.close(); 141 142 144 final POIFSFileSystem opfs = 145 new POIFSFileSystem(new FileInputStream (originalFileName)); 146 final POIFSFileSystem cpfs = 147 new POIFSFileSystem(new FileInputStream (copyFileName)); 148 149 final DirectoryEntry oRoot = opfs.getRoot(); 150 final DirectoryEntry cRoot = cpfs.getRoot(); 151 final StringBuffer messages = new StringBuffer (); 152 if (equal(oRoot, cRoot, messages)) 153 System.out.println("Equal"); 154 else 155 System.out.println("Not equal: " + messages.toString()); 156 } 157 158 159 160 178 private static boolean equal(final DirectoryEntry d1, 179 final DirectoryEntry d2, 180 final StringBuffer msg) 181 throws NoPropertySetStreamException, MarkUnsupportedException, 182 UnsupportedEncodingException , IOException 183 { 184 boolean equal = true; 185 186 for (final Iterator i = d1.getEntries(); equal && i.hasNext();) 187 { 188 final Entry e1 = (Entry) i.next(); 189 final String n1 = e1.getName(); 190 Entry e2 = null; 191 try 192 { 193 e2 = d2.getEntry(n1); 194 } 195 catch (FileNotFoundException ex) 196 { 197 msg.append("Document \"" + e1 + "\" exists, document \"" + 198 e2 + "\" does not.\n"); 199 equal = false; 200 break; 201 } 202 203 if (e1.isDirectoryEntry() && e2.isDirectoryEntry()) 204 equal = equal((DirectoryEntry) e1, (DirectoryEntry) e2, msg); 205 else if (e1.isDocumentEntry() && e2.isDocumentEntry()) 206 equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg); 207 else 208 { 209 msg.append("One of \"" + e1 + "\" and \"" + e2 + "\" is a " + 210 "document while the other one is a directory.\n"); 211 equal = false; 212 } 213 } 214 215 217 for (final Iterator i = d2.getEntries(); equal && i.hasNext();) 218 { 219 final Entry e2 = (Entry) i.next(); 220 final String n2 = e2.getName(); 221 Entry e1 = null; 222 try 223 { 224 e1 = d1.getEntry(n2); 225 } 226 catch (FileNotFoundException ex) 227 { 228 msg.append("Document \"" + e2 + "\" exitsts, document \"" + 229 e1 + "\" does not.\n"); 230 equal = false; 231 break; 232 } 233 } 234 return equal; 235 } 236 237 238 239 257 private static boolean equal(final DocumentEntry d1, final DocumentEntry d2, 258 final StringBuffer msg) 259 throws NoPropertySetStreamException, MarkUnsupportedException, 260 UnsupportedEncodingException , IOException 261 { 262 boolean equal = true; 263 final DocumentInputStream dis1 = new DocumentInputStream(d1); 264 final DocumentInputStream dis2 = new DocumentInputStream(d2); 265 if (PropertySet.isPropertySetStream(dis1) && 266 PropertySet.isPropertySetStream(dis2)) 267 { 268 final PropertySet ps1 = PropertySetFactory.create(dis1); 269 final PropertySet ps2 = PropertySetFactory.create(dis2); 270 equal = ps1.equals(ps2); 271 if (!equal) 272 { 273 msg.append("Property sets are not equal.\n"); 274 return equal; 275 } 276 } 277 else 278 { 279 int i1; 280 int i2; 281 do 282 { 283 i1 = dis1.read(); 284 i2 = dis2.read(); 285 if (i1 != i2) 286 { 287 equal = false; 288 msg.append("Documents are not equal.\n"); 289 break; 290 } 291 } 292 while (equal && i1 == -1); 293 } 294 return true; 295 } 296 297 298 299 308 static class CopyFile implements POIFSReaderListener 309 { 310 String dstName; 311 OutputStream out; 312 POIFSFileSystem poiFs; 313 314 315 324 public CopyFile(final String dstName) 325 { 326 this.dstName = dstName; 327 poiFs = new POIFSFileSystem(); 328 } 329 330 331 335 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 336 { 337 339 final POIFSDocumentPath path = event.getPath(); 340 final String name = event.getName(); 341 final DocumentInputStream stream = event.getStream(); 342 343 Throwable t = null; 344 345 try 346 { 347 349 if (PropertySet.isPropertySetStream(stream)) 350 { 351 353 PropertySet ps = null; 354 try 355 { 356 ps = PropertySetFactory.create(stream); 357 } 358 catch (NoPropertySetStreamException ex) 359 { 360 362 } 363 364 366 copy(poiFs, path, name, ps); 367 } 368 else 369 371 copy(poiFs, event.getPath(), event.getName(), stream); 372 } 373 catch (MarkUnsupportedException ex) 374 { 375 t = ex; 376 } 377 catch (IOException ex) 378 { 379 t = ex; 380 } 381 catch (WritingNotSupportedException ex) 382 { 383 t = ex; 384 } 385 386 391 if (t != null) 392 { 393 throw new HPSFRuntimeException 394 ("Could not read file \"" + path + "/" + name + 395 "\". Reason: " + Util.toString(t)); 396 } 397 } 398 399 400 401 409 public void copy(final POIFSFileSystem poiFs, 410 final POIFSDocumentPath path, 411 final String name, 412 final PropertySet ps) 413 throws WritingNotSupportedException, IOException 414 { 415 final DirectoryEntry de = getPath(poiFs, path); 416 final MutablePropertySet mps = new MutablePropertySet(ps); 417 de.createDocument(name, mps.toInputStream()); 418 } 419 420 421 422 430 public void copy(final POIFSFileSystem poiFs, 431 final POIFSDocumentPath path, 432 final String name, 433 final DocumentInputStream stream) throws IOException 434 { 435 final DirectoryEntry de = getPath(poiFs, path); 436 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 437 int c; 438 while ((c = stream.read()) != -1) 439 out.write(c); 440 stream.close(); 441 out.close(); 442 final InputStream in = 443 new ByteArrayInputStream (out.toByteArray()); 444 de.createDocument(name, in); 445 } 446 447 448 454 public void close() throws FileNotFoundException , IOException 455 { 456 out = new FileOutputStream (dstName); 457 poiFs.writeFilesystem(out); 458 out.close(); 459 } 460 461 462 463 466 private final Map paths = new HashMap (); 467 468 469 470 493 public DirectoryEntry getPath(final POIFSFileSystem poiFs, 494 final POIFSDocumentPath path) 495 { 496 try 497 { 498 499 final String s = path.toString(); 500 DirectoryEntry de = (DirectoryEntry) paths.get(s); 501 if (de != null) 502 503 return de; 504 505 507 int l = path.length(); 508 if (l == 0) 509 511 de = poiFs.getRoot(); 512 else 513 { 514 516 de = getPath(poiFs, path.getParent()); 517 518 de = de.createDirectory(path.getComponent 519 (path.length() - 1)); 520 } 521 paths.put(s, de); 522 return de; 523 } 524 catch (IOException ex) 525 { 526 529 ex.printStackTrace(System.err); 530 throw new RuntimeException (ex.toString()); 531 533 } 535 } 536 } 537 538 } 539 | Popular Tags |