Skip to content

Commit

Permalink
try harder to close zipArchive in ZipPackage (edge cases)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912986 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Oct 15, 2023
1 parent dbddc32 commit edef04d
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,35 @@ protected void flushImpl() {
@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;
Expand All @@ -482,7 +495,7 @@ protected void closeImpl() throws IOException {
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) {
Expand Down

0 comments on commit edef04d

Please sign in to comment.