Search in sources :

Example 81 with NPOIFSFileSystem

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

the class TestXWPFBugs method bug53475NoCSPName.

/**
     * A word document that's encrypted with non-standard
     * Encryption options, and no cspname section. See bug 53475
     */
@Test
public void bug53475NoCSPName() throws Exception {
    File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");
    NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
    // Check the encryption details
    EncryptionInfo info = new EncryptionInfo(filesystem);
    assertEquals(128, info.getHeader().getKeySize());
    assertEquals(CipherAlgorithm.aes128, info.getHeader().getCipherAlgorithm());
    assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
    // Check it can be decoded
    Decryptor d = Decryptor.getInstance(info);
    assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));
    // Check we can read the word document in that
    InputStream dataStream = d.getDataStream(filesystem);
    OPCPackage opc = OPCPackage.open(dataStream);
    XWPFDocument doc = new XWPFDocument(opc);
    XWPFWordExtractor ex = new XWPFWordExtractor(doc);
    String text = ex.getText();
    assertNotNull(text);
    assertEquals("This is password protected Word document.", text.trim());
    ex.close();
    filesystem.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) ZipFile(java.util.zip.ZipFile) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 82 with NPOIFSFileSystem

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

the class TestXWPFBugs method bug53475_aes256.

/**
     * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
     * but the key can be 128/192/256 bits
     */
@Test
public void bug53475_aes256() throws Exception {
    int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
    Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
    File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
    NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
    // Check the encryption details
    EncryptionInfo info = new EncryptionInfo(filesystem);
    assertEquals(16, info.getHeader().getBlockSize());
    assertEquals(256, info.getHeader().getKeySize());
    assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
    assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
    // Check it can be decoded
    Decryptor d = Decryptor.getInstance(info);
    assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
    // Check we can read the word document in that
    InputStream dataStream = d.getDataStream(filesystem);
    OPCPackage opc = OPCPackage.open(dataStream);
    XWPFDocument doc = new XWPFDocument(opc);
    XWPFWordExtractor ex = new XWPFWordExtractor(doc);
    String text = ex.getText();
    assertNotNull(text);
    // I know ... a stupid typo, maybe next time ...
    assertEquals("The is a password protected document.", text.trim());
    ex.close();
    filesystem.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) InputStream(java.io.InputStream) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) ZipFile(java.util.zip.ZipFile) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 83 with NPOIFSFileSystem

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

the class TestReadAllFiles method readDocumentSummaryInformation.

/**
     * <p>This test method checks whether DocumentSummary information streams
     * can be read. This is done by opening all "Test*" files in the 'poifs' directrory
     * pointed to by the "POI.testdata.path" system property, trying to extract
     * the document summary information stream in the root directory and calling
     * its get... methods.</p>
     * @throws Exception 
     */
@Test
public void readDocumentSummaryInformation() throws Exception {
    /* Read a test document <em>doc</em> into a POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(file, true);
    try {
        final DirectoryEntry dir = poifs.getRoot();
        /*
             * If there is a document summry information stream, read it from
             * the POI filesystem.
             */
        if (dir.hasEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)) {
            final DocumentSummaryInformation dsi = TestWriteWellKnown.getDocumentSummaryInformation(poifs);
            /* Execute the get... methods. */
            dsi.getByteCount();
            dsi.getByteOrder();
            dsi.getCategory();
            dsi.getCompany();
            dsi.getCustomProperties();
            // FIXME dsi.getDocparts();
            // FIXME dsi.getHeadingPair();
            dsi.getHiddenCount();
            dsi.getLineCount();
            dsi.getLinksDirty();
            dsi.getManager();
            dsi.getMMClipCount();
            dsi.getNoteCount();
            dsi.getParCount();
            dsi.getPresentationFormat();
            dsi.getScale();
            dsi.getSlideCount();
        }
    } finally {
        poifs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) Test(org.junit.Test)

Example 84 with NPOIFSFileSystem

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

the class TestWrite method inPlaceNPOIFSWrite.

/**
     * Tests that when using NPOIFS, we can do an in-place write
     *  without needing to stream in + out the whole kitchen sink
     */
@Test
public void inPlaceNPOIFSWrite() throws Exception {
    NPOIFSFileSystem fs = null;
    DirectoryEntry root = null;
    DocumentNode sinfDoc = null;
    DocumentNode dinfDoc = null;
    SummaryInformation sinf = null;
    DocumentSummaryInformation dinf = null;
    // We need to work on a File for in-place changes, so create a temp one
    final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
    copy.deleteOnExit();
    // Copy a test file over to our temp location
    InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc");
    FileOutputStream out = new FileOutputStream(copy);
    IOUtils.copy(inp, out);
    inp.close();
    out.close();
    // Open the copy in read/write mode
    fs = new NPOIFSFileSystem(copy, false);
    root = fs.getRoot();
    // Read the properties in there
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream);
    sinfStream.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream);
    dinfStream.close();
    assertEquals(131077, dinf.getOSVersion());
    // Check they start as we expect
    assertEquals("Reiichiro Hori", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("第1章", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals(null, dinf.getManager());
    // Do an in-place replace via an InputStream
    new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
    new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
    // Check it didn't get changed
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream2 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream2);
    sinfStream2.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream2 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream2);
    dinfStream2.close();
    assertEquals(131077, dinf.getOSVersion());
    // Start again!
    fs.close();
    inp = _samples.openResourceAsStream("TestShiftJIS.doc");
    out = new FileOutputStream(copy);
    IOUtils.copy(inp, out);
    inp.close();
    out.close();
    fs = new NPOIFSFileSystem(copy, false);
    root = fs.getRoot();
    // Read the properties in once more
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream3 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream3);
    sinfStream3.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream3 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream3);
    dinfStream3.close();
    assertEquals(131077, dinf.getOSVersion());
    // Have them write themselves in-place with no changes, as an OutputStream
    OutputStream soufStream = new NDocumentOutputStream(sinfDoc);
    sinf.write(soufStream);
    soufStream.close();
    OutputStream doufStream = new NDocumentOutputStream(dinfDoc);
    dinf.write(doufStream);
    doufStream.close();
    // And also write to some bytes for checking
    ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
    sinf.write(sinfBytes);
    ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
    dinf.write(dinfBytes);
    // Check that the filesystem can give us back the same bytes
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream4 = new NDocumentInputStream(sinfDoc);
    byte[] sinfData = IOUtils.toByteArray(sinfStream4);
    sinfStream4.close();
    InputStream dinfStream4 = new NDocumentInputStream(dinfDoc);
    byte[] dinfData = IOUtils.toByteArray(dinfStream4);
    dinfStream4.close();
    assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
    assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
    // Read back in as-is
    InputStream sinfStream5 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream5);
    sinfStream5.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream5 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream5);
    dinfStream5.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Reiichiro Hori", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("第1章", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals(null, dinf.getManager());
    // Now alter a few of them
    sinf.setAuthor("Changed Author");
    sinf.setTitle("Le titre était changé");
    dinf.setManager("Changed Manager");
    // Save this into the filesystem
    OutputStream soufStream2 = new NDocumentOutputStream(sinfDoc);
    sinf.write(soufStream2);
    soufStream2.close();
    OutputStream doufStream2 = new NDocumentOutputStream(dinfDoc);
    dinf.write(doufStream2);
    doufStream2.close();
    // Read them back in again
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream6 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream6);
    sinfStream6.close();
    assertEquals(131077, sinf.getOSVersion());
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream dinfStream6 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream6);
    dinfStream6.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Changed Author", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("Le titre était changé", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals("Changed Manager", dinf.getManager());
    // Close the whole filesystem, and open it once more
    fs.writeFilesystem();
    fs.close();
    fs = new NPOIFSFileSystem(copy);
    root = fs.getRoot();
    // Re-check on load
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream7 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream7);
    sinfStream7.close();
    assertEquals(131077, sinf.getOSVersion());
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream dinfStream7 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream7);
    dinfStream7.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Changed Author", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("Le titre était changé", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals("Changed Manager", dinf.getManager());
    // Tidy up
    fs.close();
    copy.delete();
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) NPOIFSDocument(org.apache.poi.poifs.filesystem.NPOIFSDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 85 with NPOIFSFileSystem

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

the class TestPOIDocumentMain method WriteReadProperties.

@Test
public void WriteReadProperties() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Write them out
    NPOIFSFileSystem outFS = new NPOIFSFileSystem();
    doc.readProperties();
    doc.writeProperties(outFS);
    outFS.writeFilesystem(baos);
    // Create a new version
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    OPOIFSFileSystem inFS = new OPOIFSFileSystem(bais);
    // Check they're still there
    POIDocument doc3 = new HPSFPropertiesOnlyDocument(inFS);
    doc3.readProperties();
    // Delegate test
    readPropertiesHelper(doc3);
    doc3.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) HPSFPropertiesOnlyDocument(org.apache.poi.hpsf.HPSFPropertiesOnlyDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) Test(org.junit.Test)

Aggregations

NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)101 Test (org.junit.Test)57 File (java.io.File)35 InputStream (java.io.InputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)14 FileOutputStream (java.io.FileOutputStream)12 TempFile (org.apache.poi.util.TempFile)12 FileInputStream (java.io.FileInputStream)11 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)10 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)10 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)9 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)8 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)7 TikaInputStream (org.apache.tika.io.TikaInputStream)6 AgileDecryptor (org.apache.poi.poifs.crypt.agile.AgileDecryptor)5 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)5