Author: fanningpj
Date: Sun Oct 15 21:06:01 2023
New Revision: 1912986

URL: http://svn.apache.org/viewvc?rev=1912986&view=rev
Log:
try harder to close zipArchive in ZipPackage (edge cases)

Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java?rev=1912986&r1=1912985&r2=1912986&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java 
Sun Oct 15 21:06:01 2023
@@ -458,22 +458,35 @@ public final class ZipPackage extends OP
     @Override
     protected void closeImpl() throws IOException {
         // Flush the package
-        flush();
+        try {
+            flush();
+        } catch (RuntimeException|Error e) {
+            IOUtils.closeQuietly(zipArchive);
+            throw e;
+        }
 
         if (this.originalPackagePath == null || 
this.originalPackagePath.isEmpty()) {
+            IOUtils.closeQuietly(zipArchive);
             return;
         }
 
         // Save the content
         File targetFile = new File(this.originalPackagePath);
         if (!targetFile.exists()) {
+            IOUtils.closeQuietly(zipArchive);
             throw new InvalidOperationException(
                 "Can't close a package not previously open with the open() 
method !");
         }
 
         // Case of a package previously open
-        String tempFileName = 
generateTempFileName(FileHelper.getDirectory(targetFile));
-        File tempFile = TempFile.createTempFile(tempFileName, ".tmp");
+        File tempFile;
+        try {
+            String tempFileName = 
generateTempFileName(FileHelper.getDirectory(targetFile));
+            tempFile = TempFile.createTempFile(tempFileName, ".tmp");
+        } catch (IOException|RuntimeException|Error e) {
+            IOUtils.closeQuietly(zipArchive);
+            throw e;
+        }
 
         // Save the final package to a temporary file
         boolean success = false;
@@ -482,7 +495,7 @@ public final class ZipPackage extends OP
             success = true;
         } finally {
             // Close the current zip file, so we can overwrite it on all 
platforms
-            IOUtils.closeQuietly(this.zipArchive);
+            IOUtils.closeQuietly(zipArchive);
             try {
                 // Copy the new file over the old one if save() succeed
                 if(success) {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to