diff options
6 files changed, 35 insertions, 8 deletions
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java index 18c9c23c0e..bca850186d 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java @@ -84,6 +84,7 @@ public final class BigGridDemo { // Step 1. Create a template file. Setup sheets and workbook-level objects such as // cell styles, number formats, etc. + File tmp = null; try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet("Big Grid"); @@ -97,7 +98,7 @@ public final class BigGridDemo { } //Step 2. Generate XML file. - File tmp = File.createTempFile("sheet", ".xml"); + tmp = File.createTempFile("sheet", ".xml"); try ( FileOutputStream stream = new FileOutputStream(tmp); Writer fw = new OutputStreamWriter(stream, XML_ENCODING) @@ -109,6 +110,8 @@ public final class BigGridDemo { try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) { substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out); } + } finally { + if (tmp != null) tmp.delete(); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java index 7582d323a0..a6e3286b51 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java +++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java @@ -111,8 +111,15 @@ public final class AesZipFileZipEntrySource implements ZipEntrySource { RandomSingleton.getInstance().nextBytes(ivBytes); RandomSingleton.getInstance().nextBytes(keyBytes); final File tmpFile = TempFile.createTempFile("protectedXlsx", ".zip"); - copyToFile(is, tmpFile, keyBytes, ivBytes); - return fileToSource(tmpFile, keyBytes, ivBytes); + try { + copyToFile(is, tmpFile, keyBytes, ivBytes); + return fileToSource(tmpFile, keyBytes, ivBytes); + } catch (IOException|RuntimeException e) { + if (!tmpFile.delete()) { + LOG.atInfo().log("Temp file was not deleted, may already have been deleted by another method."); + } + throw e; + } } finally { IOUtils.closeQuietly(is); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java index eff1234791..0e634660a1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/GZIPSheetDataWriter.java @@ -28,6 +28,7 @@ import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import org.apache.poi.util.Removal; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.model.SharedStringsTable; @@ -49,8 +50,10 @@ public class GZIPSheetDataWriter extends SheetDataWriter { /** * @return temp file to write sheet data + * @deprecated no need for this be public, will be made private or protected in an upcoming release */ @Override + @Removal(version = "6.0.0") public File createTempFile() throws IOException { return TempFile.createTempFile("poi-sxssf-sheet-xml", ".gz"); } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index 7f174b2381..d4ae39bf47 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -267,7 +267,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { throw new IOException(e); } finally { if (fileOut != null) { - fileOut.delete(); + if (!fileOut.delete()) { + //ignore + } } } } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index d5fbc712f8..0e1beaeb11 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -131,9 +131,10 @@ public class StandardEncryptor extends Encryptor { protected long countBytes; protected final File fileOut; protected final DirectoryNode dir; + protected final boolean deleteFile; @SuppressWarnings({"resource", "squid:S2095"}) - private StandardCipherOutputStream(DirectoryNode dir, File fileOut) throws IOException { + private StandardCipherOutputStream(DirectoryNode dir, File fileOut, boolean deleteFile) throws IOException { // although not documented, we need the same padding as with agile encryption // and instead of calculating the missing bytes for the block size ourselves // we leave it up to the CipherOutputStream, which generates/saves them on close() @@ -147,12 +148,13 @@ public class StandardEncryptor extends Encryptor { super( new CipherOutputStream(new FileOutputStream(fileOut), getCipher(getSecretKey(), "PKCS5Padding")) ); + this.deleteFile = deleteFile; this.fileOut = fileOut; this.dir = dir; } protected StandardCipherOutputStream(DirectoryNode dir) throws IOException { - this(dir, TempFile.createTempFile("encrypted_package", "crypt")); + this(dir, TempFile.createTempFile("encrypted_package", "crypt"), true); } @Override @@ -172,6 +174,11 @@ public class StandardEncryptor extends Encryptor { // the CipherOutputStream adds the padding bytes on close() super.close(); writeToPOIFS(); + if (deleteFile && fileOut != null) { + if (!fileOut.delete()) { + //ignore + } + } } void writeToPOIFS() throws IOException { diff --git a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java index 0155ab0fe1..3e7c1ab2a2 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java @@ -618,7 +618,12 @@ public final class ExcelFileFormatDocFunctionExtractor { // } File tempEFFDocFile = downloadSourceFile(); - processFile(tempEFFDocFile, outFile); - tempEFFDocFile.delete(); + try { + processFile(tempEFFDocFile, outFile); + } finally { + if (!tempEFFDocFile.delete()) { + //ignore + } + } } } |