Search in sources :

Example 51 with POIFSFileSystem

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 52 with POIFSFileSystem

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 53 with POIFSFileSystem

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);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 54 with POIFSFileSystem

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;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 55 with POIFSFileSystem

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6