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