[bug-65772] stop using deleteOnExit in DefaultTempFileCreationStrategy

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896474 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-12-28 12:36:51 +00:00
parent 57852e0c7d
commit d442ea645c
6 changed files with 35 additions and 8 deletions

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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");
}

View File

@ -267,7 +267,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
throw new IOException(e);
} finally {
if (fileOut != null) {
fileOut.delete();
if (!fileOut.delete()) {
//ignore
}
}
}
}

View File

@ -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 {

View File

@ -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
}
}
}
}