]> source.dussan.org Git - poi.git/commitdiff
close fileinputstream on second exception in ZipPackage
authorTim Allison <tallison@apache.org>
Fri, 8 Apr 2016 14:36:05 +0000 (14:36 +0000)
committerTim Allison <tallison@apache.org>
Fri, 8 Apr 2016 14:36:05 +0000 (14:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1738251 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java

index 7e2d546ec77cfdc6f3826f349ab10c3a3f92d559..695be27f212915463cf3c57880a225d5dce6aea2 100644 (file)
@@ -131,12 +131,28 @@ public final class ZipPackage extends Package {
             // some zips can't be opened via ZipFile in JDK6, as the central directory
             // contains either non-latin entries or the compression type can't be handled
             // the workaround is to iterate over the stream and not the directory
-            FileInputStream fis;
+            FileInputStream fis = null;
+            ThresholdInputStream zis = null;
             try {
                 fis = new FileInputStream(file);
-                ThresholdInputStream zis = ZipHelper.openZipStream(fis);
+                zis = ZipHelper.openZipStream(fis);
                 ze = new ZipInputStreamZipEntrySource(zis);
             } catch (IOException e2) {
+                if (zis != null) {
+                    try {
+                        zis.close();
+                    } catch (IOException e3) {
+                        throw new InvalidOperationException("Can't open the specified file: '" + file + "'"+
+                                " and couldn't close the file input stream", e);
+                    }
+                } else if (fis != null) {
+                    try {
+                        fis.close();
+                    } catch (IOException e3) {
+                        throw new InvalidOperationException("Can't open the specified file: '" + file + "'"+
+                                " and couldn't close the file input stream", e);
+                    }
+                }
                 throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e);
             }
         }
index d733ade321f8ee83fa784ad90a4eb066cc4c60ca..7f174e07afcc17093e951daf34644a20f12c8dea 100644 (file)
@@ -25,8 +25,10 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
@@ -174,4 +176,24 @@ public class TestZipPackage {
         SlideShow<?,?> ppt = SlideShowFactory.create(f, null, true);
         ppt.close();
     }
+
+    @Test
+    public void testClosingStreamOnException() throws IOException {
+        InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
+        File tmp = File.createTempFile("poi-test-truncated-zip", "");
+        OutputStream os = new FileOutputStream(tmp);
+        for (int i = 0; i < 100; i++) {
+            os.write(is.read());
+        }
+        os.flush();
+        os.close();
+        is.close();
+
+        try {
+            OPCPackage.open(tmp, PackageAccess.READ);
+        } catch (Exception e) {
+        }
+        assertTrue("Can't delete tmp file", tmp.delete());
+
+    }
 }