use of org.apache.poi.util.TempFile in project poi by apache.
the class ZipPackage method closeImpl.
/**
* Close and save the package.
*
* @see #close()
*/
@Override
protected void closeImpl() throws IOException {
// Flush the package
flush();
if (this.originalPackagePath == null || "".equals(this.originalPackagePath)) {
return;
}
// Save the content
File targetFile = new File(this.originalPackagePath);
if (!targetFile.exists()) {
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");
// Save the final package to a temporary file
try {
save(tempFile);
} finally {
// Close the current zip file, so we can overwrite it on all platforms
IOUtils.closeQuietly(this.zipArchive);
try {
// Copy the new file over the old one
FileHelper.copyFile(tempFile, targetFile);
} finally {
// Either the save operation succeed or not, we delete the temporary file
if (!tempFile.delete()) {
LOG.log(POILogger.WARN, "The temporary file: '" + targetFile.getAbsolutePath() + "' cannot be deleted ! Make sure that no other application use it.");
}
}
}
}
use of org.apache.poi.util.TempFile in project poi by apache.
the class TestDocumentProtection method testIntegration.
@Test
public void testIntegration() throws IOException {
XWPFDocument doc1 = new XWPFDocument();
XWPFParagraph p1 = doc1.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText("Lorem ipsum dolor sit amet.");
doc1.enforceCommentsProtection();
File tempFile = TempFile.createTempFile("documentProtectionFile", ".docx");
FileOutputStream out = new FileOutputStream(tempFile);
doc1.write(out);
out.close();
FileInputStream inputStream = new FileInputStream(tempFile);
XWPFDocument doc2 = new XWPFDocument(inputStream);
inputStream.close();
assertTrue(doc2.isEnforcedCommentsProtection());
doc2.close();
doc1.close();
}
Aggregations