--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.poifs.dev;\r
+\r
+import org.apache.poi.poifs.filesystem.*;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.FileOutputStream;\r
+import java.util.Iterator;\r
+\r
+/**\r
+ *\r
+ * Dump internal structure of a OLE2 file into file system\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class POIFSDump {\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ for (int i = 0; i < args.length; i++) {\r
+ System.out.println("Dumping " + args[i]);\r
+ FileInputStream is = new FileInputStream(args[i]);\r
+ POIFSFileSystem fs = new POIFSFileSystem(is);\r
+ is.close();\r
+\r
+ DirectoryEntry root = fs.getRoot();\r
+ File file = new File(root.getName());\r
+ file.mkdir();\r
+\r
+ dump(root, file);\r
+ }\r
+ }\r
+\r
+\r
+ public static void dump(DirectoryEntry root, File parent) throws IOException {\r
+ for(Iterator it = root.getEntries(); it.hasNext();){\r
+ Entry entry = (Entry)it.next();\r
+ if(entry instanceof DocumentNode){\r
+ DocumentNode node = (DocumentNode)entry;\r
+ DocumentInputStream is = new DocumentInputStream(node);\r
+ byte[] bytes = new byte[node.getSize()];\r
+ is.read(bytes);\r
+ is.close();\r
+\r
+ FileOutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));\r
+ out.write(bytes);\r
+ out.close();\r
+ } else if (entry instanceof DirectoryEntry){\r
+ DirectoryEntry dir = (DirectoryEntry)entry;\r
+ File file = new File(parent, entry.getName());\r
+ file.mkdir();\r
+ dump(dir, file);\r
+ } else {\r
+ System.err.println("Skipping unsupported POIFS entry: " + entry);\r
+ }\r
+ }\r
+ }\r
+}\r