Simplify handling of exceptions in ZipPackage

The code became overly complex and hard to reason about.

We can avoid some additional catching/rethrowing of exceptions.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1914409 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2023-12-06 19:51:19 +00:00
parent fd9300d3b7
commit d021e6bc5e

View File

@ -210,57 +210,31 @@ public final class ZipPackage extends OPCPackage {
throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e);
}
ZipArchiveThresholdInputStream zis = null;
// If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource
try {
// read from the file input stream
return openZipEntrySourceStream(fis);
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream
zis = ZipHelper.openZipStream(fis); // NOSONAR
// If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource
// read from the zip input stream
// Acquire the final level resource. If this is acquired successfully, the zip package was read successfully from the input stream
return new ZipInputStreamZipEntrySource(zis);
} catch (final InvalidOperationException|UnsupportedFileFormatException e) {
// abort: close the zip input stream
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(zis);
throw e;
} catch (final Exception e) {
// abort: close the file input stream
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(zis);
throw new InvalidOperationException("Failed to read the file input stream from file: '" + file + "'", e);
}
}
private static ZipEntrySource openZipEntrySourceStream(InputStream fis) throws InvalidOperationException {
final ZipArchiveThresholdInputStream zis;
// Acquire a resource that is needed to read the next level of openZipEntrySourceStream
try {
// open the zip input stream
zis = ZipHelper.openZipStream(fis); // NOSONAR
} catch (final IOException e) {
// If the source cannot be acquired, abort (no resources to free at this level)
throw new InvalidOperationException("Could not open the file input stream", e);
}
// If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource
try {
// read from the zip input stream
return openZipEntrySourceStream(zis);
} catch (final InvalidOperationException|UnsupportedFileFormatException e) {
// abort: close the zip input stream
IOUtils.closeQuietly(zis);
throw e;
} catch (final Exception e) {
// abort: close the zip input stream
IOUtils.closeQuietly(zis);
throw new InvalidOperationException("Failed to read the zip entry source stream", e);
}
}
private static ZipEntrySource openZipEntrySourceStream(ZipArchiveThresholdInputStream zis) throws InvalidOperationException {
// Acquire the final level resource. If this is acquired successfully, the zip package was read successfully from the input stream
try {
// open the zip entry source stream
return new ZipInputStreamZipEntrySource(zis);
} catch (IOException e) {
throw new InvalidOperationException("Could not open the specified zip entry source stream", e);
}
}
/**
* Constructor. Opens a Zip based Open XML document from
* a custom ZipEntrySource, typically an open archive