Search in sources :

Example 16 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestReWrite method assertWritesOutTheSame.

public void assertWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs) throws Exception {
    // Write out to a byte array, and to a temp file
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    hss.write(baos);
    final File file = TempFile.createTempFile("TestHSLF", ".ppt");
    final File file2 = TempFile.createTempFile("TestHSLF", ".ppt");
    hss.write(file);
    hss.write(file2);
    // Build an input stream of it, and read back as a POIFS from the stream
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    POIFSFileSystem npfS = new POIFSFileSystem(bais);
    // And the same on the temp file
    POIFSFileSystem npfF = new POIFSFileSystem(file);
    // And another where we do an in-place write
    POIFSFileSystem npfRF = new POIFSFileSystem(file2, false);
    HSLFSlideShowImpl hssRF = new HSLFSlideShowImpl(npfRF);
    hssRF.write();
    hssRF.close();
    npfRF = new POIFSFileSystem(file2);
    // Check all of them in turn
    for (POIFSFileSystem npf : new POIFSFileSystem[] { npfS, npfF, npfRF }) {
        // Check that the "PowerPoint Document" sections have the same size
        DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
        DocumentEntry nProps = (DocumentEntry) npf.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
        assertEquals(oProps.getSize(), nProps.getSize());
        // Check that they contain the same data
        byte[] _oData = new byte[oProps.getSize()];
        byte[] _nData = new byte[nProps.getSize()];
        pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
        npf.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
        for (int i = 0; i < _oData.length; i++) {
            //System.out.println(i + "\t" + Integer.toHexString(i));
            assertEquals(_oData[i], _nData[i]);
        }
        npf.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)

Example 17 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestReWriteSanity method setUp.

@Before
public void setUp() throws Exception {
    POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
    pfs = new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
    ss = new HSLFSlideShowImpl(pfs);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) POIDataSamples(org.apache.poi.POIDataSamples) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Before(org.junit.Before)

Example 18 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestReWrite method assertSlideShowWritesOutTheSame.

public void assertSlideShowWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs) throws IOException {
    // Create a slideshow covering it
    @SuppressWarnings("resource") HSLFSlideShow ss = new HSLFSlideShow(hss);
    ss.getSlides();
    ss.getNotes();
    // Now write out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    hss.write(baos);
    // Build an input stream of it
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    // Use POIFS to query that lot
    POIFSFileSystem npfs = new POIFSFileSystem(bais);
    // Check that the "PowerPoint Document" sections have the same size
    DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    assertEquals(oProps.getSize(), nProps.getSize());
    // Check that they contain the same data
    byte[] _oData = new byte[oProps.getSize()];
    byte[] _nData = new byte[nProps.getSize()];
    pfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
    npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
    for (int i = 0; i < _oData.length; i++) {
        if (_oData[i] != _nData[i])
            System.out.println(i + "\t" + Integer.toHexString(i));
        assertEquals(_oData[i], _nData[i]);
    }
    npfs.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow)

Example 19 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestReWrite method testWithMacroStreams.

@Test
public void testWithMacroStreams() throws IOException {
    // Check that they're apparently the same
    assertSlideShowWritesOutTheSame(hssC, pfsC);
    // Currently has a Macros stream
    assertNotNull(pfsC.getRoot().getEntry("Macros"));
    // Write out normally, will loose the macro stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    hssC.write(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    POIFSFileSystem pfsNew = new POIFSFileSystem(bais);
    assertFalse(pfsNew.getRoot().hasEntry("Macros"));
    pfsNew.close();
    // But if we write out with nodes preserved, will be there
    baos.reset();
    hssC.write(baos, true);
    bais = new ByteArrayInputStream(baos.toByteArray());
    pfsNew = new POIFSFileSystem(bais);
    assertTrue(pfsNew.getRoot().hasEntry("Macros"));
    pfsNew.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 20 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class TestExtractor method testExtractFromEmbeded.

@Test
public void testExtractFromEmbeded() throws IOException {
    InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls");
    POIFSFileSystem fs = new POIFSFileSystem(is);
    DirectoryNode root = fs.getRoot();
    PowerPointExtractor ppe1 = assertExtractFromEmbedded(root, "MBD0000A3B6", "Sample PowerPoint file\nThis is the 1st file\nNot much too it\n");
    PowerPointExtractor ppe2 = assertExtractFromEmbedded(root, "MBD0000A3B3", "Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n");
    ppe2.close();
    ppe1.close();
    fs.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) 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