Search in sources :

Example 31 with POIFSFileSystem

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

the class TestCurrentUserAtom method writeNormal.

@Test
public void writeNormal() throws Exception {
    // Get raw contents from a known file
    POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(normalFile));
    DocumentEntry docProps = (DocumentEntry) fs.getRoot().getEntry("Current User");
    byte[] contents = new byte[docProps.getSize()];
    InputStream in = fs.getRoot().createDocumentInputStream("Current User");
    in.read(contents);
    in.close();
    fs.close();
    // Now build up a new one
    CurrentUserAtom cu = new CurrentUserAtom();
    cu.setLastEditUsername("Hogwarts");
    cu.setCurrentEditOffset(0x2942);
    // Check it matches
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    cu.writeOut(baos);
    byte[] out = baos.toByteArray();
    assertArrayEquals(contents, out);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) InputStream(java.io.InputStream) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 32 with POIFSFileSystem

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

the class TestExOleObjStg method testRead.

@Test
public void testRead() throws Exception {
    ExOleObjStg record = new ExOleObjStg(data, 0, data.length);
    assertEquals(RecordTypes.ExOleObjStg.typeID, record.getRecordType());
    int len = record.getDataLength();
    byte[] oledata = readAll(record.getData());
    assertEquals(len, oledata.length);
    POIFSFileSystem fs = new POIFSFileSystem(record.getData());
    assertTrue("Constructed POIFS from ExOleObjStg data", true);
    DocumentEntry doc = (DocumentEntry) fs.getRoot().getEntry("Contents");
    assertNotNull(doc);
    assertTrue("Fetched the Contents stream containing OLE properties", true);
    fs.close();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) Test(org.junit.Test)

Example 33 with POIFSFileSystem

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

the class TestWordExtractor method testBug51686.

/**
     * [RESOLVED FIXED] Bug 51686 - Update to POI 3.8 beta 4 causes
     * ConcurrentModificationException in Tika's OfficeParser
     */
@Test
public void testBug51686() throws IOException {
    InputStream is = docTests.openResourceAsStream("Bug51686.doc");
    POIFSFileSystem fs = new POIFSFileSystem(is);
    is.close();
    String text = null;
    for (Entry entry : fs.getRoot()) {
        if ("WordDocument".equals(entry.getName())) {
            WordExtractor ex = new WordExtractor(fs);
            try {
                text = ex.getText();
            } finally {
                ex.close();
            }
        }
    }
    assertNotNull(text);
    fs.close();
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) 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) Test(org.junit.Test)

Example 34 with POIFSFileSystem

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

the class TestWordExtractor method testExtractorFromWord6Extractor.

@Test
public void testExtractorFromWord6Extractor() throws Exception {
    InputStream is = POIDataSamples.getHPSFInstance().openResourceAsStream("TestMickey.doc");
    POIFSFileSystem fs = new POIFSFileSystem(is);
    is.close();
    Word6Extractor wExt = new Word6Extractor(fs);
    try {
        POITextExtractor ext = wExt.getMetadataTextExtractor();
        try {
            // Now overall
            String text = ext.getText();
            assertContains(text, "TEMPLATE = Normal");
            assertContains(text, "SUBJECT = sample subject");
            assertContains(text, "MANAGER = sample manager");
            assertContains(text, "COMPANY = sample company");
        } finally {
            ext.close();
        }
    } finally {
        wExt.close();
        fs.close();
    }
}
Also used : POITextExtractor(org.apache.poi.POITextExtractor) 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) Test(org.junit.Test)

Example 35 with POIFSFileSystem

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

the class TestWordExtractor method testExtractFromEmbeded.

/**
	 * Test that we can get data from two different embedded word documents
	 */
@Test
public void testExtractFromEmbeded() throws IOException {
    InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls");
    POIFSFileSystem fs = new POIFSFileSystem(is);
    is.close();
    DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B7");
    DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B2");
    // Should have WordDocument and 1Table
    assertNotNull(dirA.getEntry("1Table"));
    assertNotNull(dirA.getEntry("WordDocument"));
    assertNotNull(dirB.getEntry("1Table"));
    assertNotNull(dirB.getEntry("WordDocument"));
    // Check each in turn
    HWPFDocument docA = new HWPFDocument(dirA);
    WordExtractor extractorA = new WordExtractor(docA);
    assertNotNull(extractorA.getText());
    assertTrue(extractorA.getText().length() > 20);
    assertEqualsTrim("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractorA.getText());
    assertEquals("Sample Doc 1", extractorA.getSummaryInformation().getTitle());
    assertEquals("Sample Test", extractorA.getSummaryInformation().getSubject());
    HWPFDocument docB = new HWPFDocument(dirB);
    WordExtractor extractorB = new WordExtractor(docB);
    assertNotNull(extractorB.getText());
    assertTrue(extractorB.getText().length() > 20);
    assertEqualsTrim("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", extractorB.getText());
    assertEquals("Sample Doc 2", extractorB.getSummaryInformation().getTitle());
    assertEquals("Another Sample Test", extractorB.getSummaryInformation().getSubject());
    extractorA.close();
    docA.close();
    extractorB.close();
    docB.close();
    fs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) 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