use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestPOIFSProperties method testOK.
@Test
public void testOK() throws Exception {
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
//set POIFS properties before constructing HSSFWorkbook
SummaryInformation summary1 = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
summary1.setTitle(title);
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
HSSFWorkbook wb = new HSSFWorkbook(fs);
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
out.close();
wb.close();
//read the property
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
SummaryInformation summary2 = (SummaryInformation) PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
assertEquals(title, summary2.getTitle());
fs2.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestPOIFSProperties method testFail.
@Test
public void testFail() throws Exception {
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
HSSFWorkbook wb = new HSSFWorkbook(fs);
//set POIFS properties after constructing HSSFWorkbook
//(a piece of code that used to work up to POI 3.0.2)
SummaryInformation summary1 = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
summary1.setTitle(title);
//write the modified property back to POIFS
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
//save the workbook and read the property
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
out.close();
wb.close();
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
SummaryInformation summary2 = (SummaryInformation) PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
//failing assertion
assertEquals(title, summary2.getTitle());
fs2.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestHSSFWorkbook method closeDoesNotModifyWorkbook.
@Test
public void closeDoesNotModifyWorkbook() throws IOException {
final String filename = "SampleSS.xls";
final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename);
Workbook wb;
// File via POIFileStream (java.io)
wb = new HSSFWorkbook(new POIFSFileSystem(file));
assertCloseDoesNotModifyFile(filename, wb);
// File via NPOIFileStream (java.nio)
wb = new HSSFWorkbook(new NPOIFSFileSystem(file));
assertCloseDoesNotModifyFile(filename, wb);
// InputStream
wb = new HSSFWorkbook(new FileInputStream(file));
assertCloseDoesNotModifyFile(filename, wb);
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class XSSFWorkbook method addOlePackage.
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
int oleId = 0;
do {
try {
pnOLE = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + (++oleId) + ".bin");
} catch (InvalidFormatException e) {
throw new IOException("ole object name not recognized", e);
}
} while (opc.containPart(pnOLE));
PackagePart pp = opc.createPart(pnOLE, "application/vnd.openxmlformats-officedocument.oleObject");
Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length + 500);
ole10.writeOut(bos);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE);
// TODO: generate CombObj stream
OutputStream os = pp.getOutputStream();
poifs.writeFilesystem(os);
os.close();
poifs.close();
return oleId;
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestAgileEncryptionParameters method testAgileEncryptionModes.
@Test
public void testAgileEncryptionModes() throws Exception {
int maxKeyLen = Cipher.getMaxAllowedKeyLength(ca.jceId);
Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files", maxKeyLen >= ca.defaultKeySize);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
POIFSFileSystem fsEnc = new POIFSFileSystem();
EncryptionInfo infoEnc = new EncryptionInfo(EncryptionMode.agile, ca, ha, -1, -1, cm);
Encryptor enc = infoEnc.getEncryptor();
enc.confirmPassword("foobaa");
OutputStream os = enc.getDataStream(fsEnc);
os.write(testData);
os.close();
bos.reset();
fsEnc.writeFilesystem(bos);
fsEnc.close();
POIFSFileSystem fsDec = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
EncryptionInfo infoDec = new EncryptionInfo(fsDec);
Decryptor dec = infoDec.getDecryptor();
boolean passed = dec.verifyPassword("foobaa");
assertTrue(passed);
InputStream is = dec.getDataStream(fsDec);
byte[] actualData = IOUtils.toByteArray(is);
is.close();
fsDec.close();
assertArrayEquals("Failed roundtrip - " + ca + "-" + ha + "-" + cm, testData, actualData);
}
Aggregations