]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 52204: Deprecated XSSFWorkbook(String path) constructor because it does...
authorYegor Kozlov <yegor@apache.org>
Sat, 10 Dec 2011 07:56:54 +0000 (07:56 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 10 Dec 2011 07:56:54 +0000 (07:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1212744 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/examples/src/org/apache/poi/xssf/usermodel/examples/CustomXMLMapping.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java
src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

index 2370acc90ce28073650de17bf73d6a8766c490b5..dc63536bec1e7e73667c7c89e7adc0356dc41ca7 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta5" date="2011-??-??">
+           <action dev="poi-developers" type="fix">52204 - Deprecated XSSFWorkbook(String path) constructor because it does not close underlying .zip file</action>
            <action dev="poi-developers" type="fix">46288 - fixed refcount of Fill pictures in HSLF </action>
            <action dev="poi-developers" type="add">51961 - support compression of temp files in SXSSF </action>
            <action dev="poi-developers" type="add">52268 - support cloning sheets with drawings in XSSF </action>
index 3e69df286c6de2371b7d3cbe9e374f073e7b0be0..1add0d2fb3678097c6cc95578c4efb39e32e798f 100644 (file)
@@ -16,6 +16,7 @@
 ==================================================================== */\r
 package org.apache.poi.xssf.usermodel.examples;\r
 \r
+import org.apache.poi.openxml4j.opc.OPCPackage;\r
 import org.apache.poi.xssf.extractor.XSSFExportToXml;\r
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 import org.apache.poi.xssf.usermodel.XSSFMap;\r
@@ -28,7 +29,8 @@ import java.io.ByteArrayOutputStream;
 public class CustomXMLMapping {\r
 \r
     public static void main(String[] args) throws Exception {\r
-        XSSFWorkbook wb = new XSSFWorkbook(args[0]);\r
+        OPCPackage pkg = OPCPackage.open(args[0]);\r
+        XSSFWorkbook wb = new XSSFWorkbook(pkg);\r
 \r
         for (XSSFMap map : wb.getCustomXMLMappings()) {\r
             XSSFExportToXml exporter = new XSSFExportToXml(map);\r
@@ -38,5 +40,6 @@ public class CustomXMLMapping {
             String xml = os.toString("UTF-8");\r
             System.out.println(xml);\r
         }\r
+        pkg.close();\r
     }\r
 }\r
index d6a22ea7fd79a120887c31dbfa6d83a913ec8645..b50e9594607b55fca69c48e362d36ddf9eda29bc 100644 (file)
@@ -32,7 +32,8 @@ import java.io.InputStream;
  */\r
 public class EmbeddedObjects {\r
     public static void main(String[] args) throws Exception {\r
-        XSSFWorkbook workbook = new XSSFWorkbook(args[0]);\r
+        OPCPackage pkg = OPCPackage.open(args[0]);\r
+        XSSFWorkbook workbook = new XSSFWorkbook(pkg);\r
         for (PackagePart pPart : workbook.getAllEmbedds()) {\r
             String contentType = pPart.getContentType();\r
             // Excel Workbook - either binary or OpenXML\r
@@ -66,5 +67,6 @@ public class EmbeddedObjects {
                 InputStream inputStream = pPart.getInputStream();\r
             }\r
         }\r
+        pkg.close();\r
     }\r
 }
\ No newline at end of file
index 40d5b59e7239144df1f6dd5661bbc10a1ed04dff..99d1cacf614b0918aee25ee683ba710ba28f7688 100644 (file)
@@ -23,13 +23,15 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 
+import java.io.FileInputStream;
+
 /**
  *  Iterate over rows and cells
  */
 public class IterateCells {
 
     public static void main(String[] args) throws Exception {
-        Workbook wb = new XSSFWorkbook(args[0]);
+        Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
         for (int i = 0; i < wb.getNumberOfSheets(); i++) {
             Sheet sheet = wb.getSheetAt(i);
             System.out.println(wb.getSheetName(i));
index 4b2fc3ca53daa01ee239fe3d5c806d8dbfbd87c8..1660f452aa7c211a4578f79891e1184f632c773d 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xssf.dev;
 
+import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 import java.io.FileOutputStream;
@@ -30,13 +31,16 @@ import java.io.FileOutputStream;
 public final class XSSFSave {
     public static void main(String[] args) throws Exception {
         for (int i = 0; i < args.length; i++) {
-            XSSFWorkbook wb = new XSSFWorkbook(args[i]);
+            OPCPackage pkg = OPCPackage.open(args[i]);
+            XSSFWorkbook wb = new XSSFWorkbook(pkg);
 
             int sep = args[i].lastIndexOf('.');
             String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
             FileOutputStream out = new FileOutputStream(outfile);
             wb.write(out);
             out.close();
+
+            pkg.close();
         }
     }
 
index 0f8d3d70d05be624d60a4463fb98dfcf4d57bbe8..81e79ba51c96774152cd27e42d36eea46f6449d3 100644 (file)
@@ -184,8 +184,41 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     /**
      * Constructs a XSSFWorkbook object given a file name.
      *
+     * <p>
+     *  This constructor is deprecated since POI-3.8 because it does not close
+     *  the underlying .zip file stream. In short, there are two ways to open a OPC package:
+     * </p>
+     * <ol>
+     *     <li>
+     *      from file which leads to invoking java.util.zip.ZipFile(File file)
+     *      deep in POI internals.
+     *     </li>
+     *     <li>
+     *     from input stream in which case we first read everything into memory and
+     *     then pass the data to ZipInputStream.
+     *     </li>
+     * <ol>
+     * <p>    
+     *     It should be noted, that (2) uses quite a bit more memory than (1), which
+     *      doesn't need to hold the whole zip file in memory, and can take advantage
+     *      of native methods.
+     * </p>
+     * <p>
+     *   To construct a workbook from file use the
+     *   {@link #XSSFWorkbook(org.apache.poi.openxml4j.opc.OPCPackage)}  constructor:
+     *   <pre><code>
+     *       OPCPackage pkg = OPCPackage.open(path);
+     *       XSSFWorkbook wb = new XSSFWorkbook(pkg);
+     *       // work with the wb object
+     *       ......
+     *       pkg.close(); // gracefully closes the underlying zip file
+     *   </code></pre>     
+     * </p>
+     * 
      * @param      path   the file name.
+     * @deprecated
      */
+    @Deprecated
     public XSSFWorkbook(String path) throws IOException {
         this(openPackage(path));
     }