Search in sources :

Example 1 with TempFile

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.");
            }
        }
    }
}
Also used : InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) TempFile(org.apache.poi.util.TempFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with TempFile

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();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

File (java.io.File)2 TempFile (org.apache.poi.util.TempFile)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ZipFile (java.util.zip.ZipFile)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)1 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)1 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)1 Test (org.junit.Test)1