bug 60128: close opened resources to avoid leaks; add exception as cause where available for more context for raised exceptions.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760710 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-09-14 13:55:39 +00:00
parent 475f7ee866
commit d3fb773843
1 changed files with 29 additions and 25 deletions

View File

@ -53,6 +53,9 @@ import org.apache.poi.util.TempFile;
* Physical zip package. * Physical zip package.
*/ */
public final class ZipPackage extends OPCPackage { public final class ZipPackage extends OPCPackage {
private static final String MIMETYPE = "mimetype";
private static final String SETTINGS_XML = "settings.xml";
private static POILogger logger = POILogFactory.getLogger(ZipPackage.class); private static POILogger logger = POILogFactory.getLogger(ZipPackage.class);
/** /**
@ -191,7 +194,7 @@ public final class ZipPackage extends OPCPackage {
} catch (final IOException e2) { } catch (final IOException e2) {
throw new InvalidOperationException("Failed to read the zip entry source stream and could not close the zip input stream", e2); throw new InvalidOperationException("Failed to read the zip entry source stream and could not close the zip input stream", e2);
} }
throw new InvalidOperationException("Failed to read the zip entry source stream"); throw new InvalidOperationException("Failed to read the zip entry source stream", e);
} }
} }
@ -253,7 +256,7 @@ public final class ZipPackage extends OPCPackage {
this.contentTypeManager = new ZipContentTypeManager( this.contentTypeManager = new ZipContentTypeManager(
getZipArchive().getInputStream(entry), this); getZipArchive().getInputStream(entry), this);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage(), e);
} }
break; break;
} }
@ -269,10 +272,10 @@ public final class ZipPackage extends OPCPackage {
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement(); final ZipEntry entry = entries.nextElement();
final String name = entry.getName(); final String name = entry.getName();
if ("mimetype".equals(name)) { if (MIMETYPE.equals(name)) {
hasMimetype = true; hasMimetype = true;
} }
if ("settings.xml".equals(name)) { if (SETTINGS_XML.equals(name)) {
hasSettingsXML = true; hasSettingsXML = true;
} }
numEntries++; numEntries++;
@ -307,10 +310,10 @@ public final class ZipPackage extends OPCPackage {
String contentType = contentTypeManager.getContentType(partName); String contentType = contentTypeManager.getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
try { try {
partList.put(partName, new ZipPackagePart(this, entry, PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
partName, contentType)); partList.put(partName, part);
} catch (InvalidOperationException e) { } catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage(), e);
} }
} }
} }
@ -322,17 +325,16 @@ public final class ZipPackage extends OPCPackage {
PackagePartName partName = buildPartName(entry); PackagePartName partName = buildPartName(entry);
if(partName == null) continue; if(partName == null) continue;
String contentType = contentTypeManager String contentType = contentTypeManager.getContentType(partName);
.getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
// Already handled // Already handled
} }
else if (contentType != null) { else if (contentType != null) {
try { try {
partList.put(partName, new ZipPackagePart(this, entry, PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
partName, contentType)); partList.put(partName, part);
} catch (InvalidOperationException e) { } catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage(), e);
} }
} else { } else {
throw new InvalidFormatException( throw new InvalidFormatException(
@ -440,20 +442,22 @@ public final class ZipPackage extends OPCPackage {
// Save the final package to a temporary file // Save the final package to a temporary file
try { try {
save(tempFile); save(tempFile);
// Close the current zip file, so we can
// overwrite it on all platforms
this.zipArchive.close();
// Copy the new file over the old one
FileHelper.copyFile(tempFile, targetFile);
} finally { } finally {
// Either the save operation succeed or not, we delete the try {
// temporary file // Close the current zip file, so we can
if (!tempFile.delete()) { // overwrite it on all platforms
logger this.zipArchive.close();
.log(POILogger.WARN,"The temporary file: '" // Copy the new file over the old one
+ targetFile.getAbsolutePath() FileHelper.copyFile(tempFile, targetFile);
+ "' cannot be deleted ! Make sure that no other application use it."); } finally {
// Either the save operation succeed or not, we delete the
// temporary file
if (!tempFile.delete()) {
logger
.log(POILogger.WARN,"The temporary file: '"
+ targetFile.getAbsolutePath()
+ "' cannot be deleted ! Make sure that no other application use it.");
}
} }
} }
} else { } else {