You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POIFSDump.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.dev;
  16. import org.apache.poi.poifs.filesystem.*;
  17. import java.io.FileInputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.FileOutputStream;
  21. import java.util.Iterator;
  22. /**
  23. *
  24. * Dump internal structure of a OLE2 file into file system
  25. *
  26. * @author Yegor Kozlov
  27. */
  28. public class POIFSDump {
  29. public static void main(String[] args) throws Exception {
  30. for (int i = 0; i < args.length; i++) {
  31. System.out.println("Dumping " + args[i]);
  32. FileInputStream is = new FileInputStream(args[i]);
  33. POIFSFileSystem fs = new POIFSFileSystem(is);
  34. is.close();
  35. DirectoryEntry root = fs.getRoot();
  36. File file = new File(root.getName());
  37. file.mkdir();
  38. dump(root, file);
  39. }
  40. }
  41. public static void dump(DirectoryEntry root, File parent) throws IOException {
  42. for(Iterator it = root.getEntries(); it.hasNext();){
  43. Entry entry = (Entry)it.next();
  44. if(entry instanceof DocumentNode){
  45. DocumentNode node = (DocumentNode)entry;
  46. DocumentInputStream is = new DocumentInputStream(node);
  47. byte[] bytes = new byte[node.getSize()];
  48. is.read(bytes);
  49. is.close();
  50. FileOutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
  51. out.write(bytes);
  52. out.close();
  53. } else if (entry instanceof DirectoryEntry){
  54. DirectoryEntry dir = (DirectoryEntry)entry;
  55. File file = new File(parent, entry.getName());
  56. file.mkdir();
  57. dump(dir, file);
  58. } else {
  59. System.err.println("Skipping unsupported POIFS entry: " + entry);
  60. }
  61. }
  62. }
  63. }