]> source.dussan.org Git - poi.git/commitdiff
utility to dump ooxml files into file system
authorYegor Kozlov <yegor@apache.org>
Tue, 16 Sep 2008 12:22:55 +0000 (12:22 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 16 Sep 2008 12:22:55 +0000 (12:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@695828 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java [new file with mode: 0755]

diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java
new file mode 100755 (executable)
index 0000000..9cd5081
--- /dev/null
@@ -0,0 +1,93 @@
+/* ====================================================================\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.xssf.dev;\r
+\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.w3c.dom.Document;\r
+\r
+import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.parsers.DocumentBuilder;\r
+import java.io.FileOutputStream;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.zip.ZipFile;\r
+import java.util.zip.ZipEntry;\r
+import java.util.Enumeration;\r
+\r
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;\r
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;\r
+\r
+/**\r
+ * Utility class which dumps the contents of a *.xlsx file into file system.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class XSSFDump {\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
+            ZipFile zip = new ZipFile(args[i]);\r
+            dump(zip);\r
+        }\r
+    }\r
+\r
+    public static void dump(ZipFile zip) throws Exception {\r
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
+        DocumentBuilder builder = factory.newDocumentBuilder();\r
+\r
+        String zipname = zip.getName();\r
+        int sep = zipname.lastIndexOf('.');\r
+        File root = new File(zipname.substring(0, sep));\r
+        root.mkdir();\r
+\r
+        Enumeration en = zip.entries();\r
+        while(en.hasMoreElements()){\r
+            ZipEntry entry = (ZipEntry)en.nextElement();\r
+            String name = entry.getName();\r
+            int idx = name.lastIndexOf('/');\r
+            if(idx != -1){\r
+                File bs = new File(root, name.substring(0, idx));\r
+                bs.mkdirs();\r
+            }\r
+\r
+            File f = new File(root, entry.getName());\r
+            FileOutputStream out = new FileOutputStream(f);\r
+\r
+            if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml")){\r
+                //pass the xml through the Xerces serializer to produce nicely formatted output\r
+                Document doc = builder.parse(zip.getInputStream(entry));\r
+\r
+                OutputFormat  format  = new OutputFormat( doc );\r
+                format.setIndenting(true);\r
+\r
+                XMLSerializer serial = new  XMLSerializer( out, format );\r
+                serial.asDOMSerializer();\r
+                serial.serialize( doc.getDocumentElement() );\r
+\r
+            } else {\r
+                int pos;\r
+                byte[] chunk = new byte[2048];\r
+                InputStream is = zip.getInputStream(entry);\r
+                while((pos = is.read(chunk)) > 0) out.write(chunk, 0, pos);\r
+            }\r
+            out.close();\r
+\r
+        }\r
+    }\r
+}\r