Search in sources :

Example 1 with ZipEntrySource

use of org.apache.poi.openxml4j.util.ZipEntrySource in project poi by apache.

the class SXSSFWorkbook method write.

/**
     * Write out this workbook to an OutputStream.
     *
     * @param stream - the java OutputStream you wish to write to
     * @exception IOException if anything can't be written.
     */
@Override
public void write(OutputStream stream) throws IOException {
    flushSheets();
    //Save the template
    File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
    boolean deleted;
    try {
        FileOutputStream os = new FileOutputStream(tmplFile);
        try {
            _wb.write(os);
        } finally {
            os.close();
        }
        //Substitute the template entries with the generated sheet data files
        final ZipEntrySource source = new ZipFileZipEntrySource(new ZipFile(tmplFile));
        injectData(source, stream);
    } finally {
        deleted = tmplFile.delete();
    }
    if (!deleted) {
        throw new IOException("Could not delete temporary file after processing: " + tmplFile);
    }
}
Also used : ZipFileZipEntrySource(org.apache.poi.openxml4j.util.ZipFileZipEntrySource) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) TempFile(org.apache.poi.util.TempFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFileZipEntrySource(org.apache.poi.openxml4j.util.ZipFileZipEntrySource) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource)

Example 2 with ZipEntrySource

use of org.apache.poi.openxml4j.util.ZipEntrySource in project poi by apache.

the class TestSXSSFWorkbookWithCustomZipEntrySource method customZipEntrySourceForWriteAndRead.

// write an encrypted workbook to disk, and encrypt any temporary files as well
@Test
public void customZipEntrySourceForWriteAndRead() throws IOException, GeneralSecurityException, InvalidFormatException {
    SXSSFWorkbookWithCustomZipEntrySource workbook = new SXSSFWorkbookWithCustomZipEntrySource();
    SXSSFSheet sheet1 = workbook.createSheet(sheetName);
    SXSSFRow row1 = sheet1.createRow(1);
    SXSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    EncryptedTempData tempData = new EncryptedTempData();
    workbook.write(tempData.getOutputStream());
    workbook.close();
    workbook.dispose();
    ZipEntrySource zipEntrySource = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
    tempData.dispose();
    OPCPackage opc = OPCPackage.open(zipEntrySource);
    XSSFWorkbook xwb = new XSSFWorkbook(opc);
    zipEntrySource.close();
    XSSFSheet xs1 = xwb.getSheetAt(0);
    assertEquals(sheetName, xs1.getSheetName());
    XSSFRow xr1 = xs1.getRow(1);
    XSSFCell xc1 = xr1.getCell(1);
    assertEquals(cellValue, xc1.getStringCellValue());
    xwb.close();
    opc.close();
}
Also used : EncryptedTempData(org.apache.poi.poifs.crypt.temp.EncryptedTempData) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 3 with ZipEntrySource

use of org.apache.poi.openxml4j.util.ZipEntrySource in project poi by apache.

the class SXSSFWorkbookWithCustomZipEntrySource method write.

@Override
public void write(OutputStream stream) throws IOException {
    flushSheets();
    EncryptedTempData tempData = new EncryptedTempData();
    ZipEntrySource source = null;
    try {
        OutputStream os = tempData.getOutputStream();
        try {
            getXSSFWorkbook().write(os);
        } finally {
            IOUtils.closeQuietly(os);
        }
        // provide ZipEntrySource to poi which decrypts on the fly
        source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
        injectData(source, stream);
    } catch (GeneralSecurityException e) {
        throw new IOException(e);
    } finally {
        tempData.dispose();
        IOUtils.closeQuietly(source);
    }
}
Also used : OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource)

Example 4 with ZipEntrySource

use of org.apache.poi.openxml4j.util.ZipEntrySource in project poi by apache.

the class TestSecureTempZip method protectedTempZip.

/**
     * Test case for #59841 - this is an example on how to use encrypted temp files,
     * which are streamed into POI opposed to having everything in memory
     */
@Test
public void protectedTempZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException {
    File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsx");
    FileInputStream fis = new FileInputStream(tikaProt);
    POIFSFileSystem poifs = new POIFSFileSystem(fis);
    EncryptionInfo ei = new EncryptionInfo(poifs);
    Decryptor dec = ei.getDecryptor();
    boolean passOk = dec.verifyPassword("tika");
    assertTrue(passOk);
    // extract encrypted ooxml file and write to custom encrypted zip file 
    InputStream is = dec.getDataStream(poifs);
    // provide ZipEntrySource to poi which decrypts on the fly
    ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is);
    // test the source
    OPCPackage opc = OPCPackage.open(source);
    String expected = "This is an Encrypted Excel spreadsheet.";
    XSSFEventBasedExcelExtractor extractor = new XSSFEventBasedExcelExtractor(opc);
    extractor.setIncludeSheetNames(false);
    String txt = extractor.getText();
    assertEquals(expected, txt.trim());
    XSSFWorkbook wb = new XSSFWorkbook(opc);
    txt = wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue();
    assertEquals(expected, txt);
    extractor.close();
    wb.close();
    opc.close();
    source.close();
    poifs.close();
    fis.close();
}
Also used : XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with ZipEntrySource

use of org.apache.poi.openxml4j.util.ZipEntrySource in project poi by apache.

the class TestSecureTempZip method protectedXLSBZip.

/**
     * Now try with xlsb.
     */
@Test
public void protectedXLSBZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException {
    //The test file requires that JCE unlimited be installed.
    //If it isn't installed, skip this test.
    int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
    Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
    File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsb");
    FileInputStream fis = new FileInputStream(tikaProt);
    POIFSFileSystem poifs = new POIFSFileSystem(fis);
    EncryptionInfo ei = new EncryptionInfo(poifs);
    Decryptor dec = ei.getDecryptor();
    boolean passOk = dec.verifyPassword("tika");
    assertTrue(passOk);
    // extract encrypted ooxml file and write to custom encrypted zip file
    InputStream is = dec.getDataStream(poifs);
    // provide ZipEntrySource to poi which decrypts on the fly
    ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is);
    // test the source
    OPCPackage opc = OPCPackage.open(source);
    String expected = "You can't see me";
    XSSFBEventBasedExcelExtractor extractor = new XSSFBEventBasedExcelExtractor(opc);
    extractor.setIncludeSheetNames(false);
    String txt = extractor.getText();
    assertEquals(expected, txt.trim());
    extractor.close();
    opc.close();
    poifs.close();
    fis.close();
}
Also used : XSSFBEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

ZipEntrySource (org.apache.poi.openxml4j.util.ZipEntrySource)5 File (java.io.File)3 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)3 AesZipFileZipEntrySource (org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource)3 Test (org.junit.Test)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ZipFile (java.util.zip.ZipFile)1 ZipFileZipEntrySource (org.apache.poi.openxml4j.util.ZipFileZipEntrySource)1 EncryptedTempData (org.apache.poi.poifs.crypt.temp.EncryptedTempData)1 SXSSFWorkbookWithCustomZipEntrySource (org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource)1 TempFile (org.apache.poi.util.TempFile)1 XSSFBEventBasedExcelExtractor (org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor)1 XSSFEventBasedExcelExtractor (org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor)1