Search in sources :

Example 6 with DocumentNode

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

the class POIFSLister method displayDirectory.

public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) {
    System.out.println(indent + dir.getName() + " -");
    String newIndent = indent + "  ";
    boolean hadChildren = false;
    for (Iterator<Entry> it = dir.getEntries(); it.hasNext(); ) {
        hadChildren = true;
        Entry entry = it.next();
        if (entry instanceof DirectoryNode) {
            displayDirectory((DirectoryNode) entry, newIndent, withSizes);
        } else {
            DocumentNode doc = (DocumentNode) entry;
            String name = doc.getName();
            String size = "";
            if (name.charAt(0) < 10) {
                String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1);
                name = name.substring(1) + " <" + altname + ">";
            }
            if (withSizes) {
                size = " [" + doc.getSize() + " / 0x" + Integer.toHexString(doc.getSize()) + "]";
            }
            System.out.println(newIndent + name + size);
        }
    }
    if (!hadChildren) {
        System.out.println(newIndent + "(no children)");
    }
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode)

Example 7 with DocumentNode

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

the class CryptoAPIDecryptor method getSummaryEntries.

/**
     * Decrypt the Document-/SummaryInformation and other optionally streams.
     * Opposed to other crypto modes, cryptoapi is record based and can't be used
     * to stream-decrypt a whole file
     *
     * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
     */
public POIFSFileSystem getSummaryEntries(DirectoryNode root, String encryptedStream) throws IOException, GeneralSecurityException {
    // HSLF: encryptedStream
    // HSSF: encryption
    DocumentNode es = (DocumentNode) root.getEntry(encryptedStream);
    DocumentInputStream dis = root.createDocumentInputStream(es);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(dis, bos);
    dis.close();
    CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray());
    LittleEndianInputStream leis = new LittleEndianInputStream(sbis);
    POIFSFileSystem fsOut = null;
    try {
        int streamDescriptorArrayOffset = (int) leis.readUInt();
        /* int streamDescriptorArraySize = (int) */
        leis.readUInt();
        long skipN = streamDescriptorArrayOffset - 8L;
        if (sbis.skip(skipN) < skipN) {
            throw new EOFException("buffer underrun");
        }
        sbis.setBlock(0);
        int encryptedStreamDescriptorCount = (int) leis.readUInt();
        StreamDescriptorEntry[] entries = new StreamDescriptorEntry[encryptedStreamDescriptorCount];
        for (int i = 0; i < encryptedStreamDescriptorCount; i++) {
            StreamDescriptorEntry entry = new StreamDescriptorEntry();
            entries[i] = entry;
            entry.streamOffset = (int) leis.readUInt();
            entry.streamSize = (int) leis.readUInt();
            entry.block = leis.readUShort();
            int nameSize = leis.readUByte();
            entry.flags = leis.readUByte();
            // boolean isStream = StreamDescriptorEntry.flagStream.isSet(entry.flags);
            entry.reserved2 = leis.readInt();
            entry.streamName = StringUtil.readUnicodeLE(leis, nameSize);
            leis.readShort();
            assert (entry.streamName.length() == nameSize);
        }
        // NOSONAR
        fsOut = new POIFSFileSystem();
        for (StreamDescriptorEntry entry : entries) {
            sbis.seek(entry.streamOffset);
            sbis.setBlock(entry.block);
            InputStream is = new BoundedInputStream(sbis, entry.streamSize);
            fsOut.createDocument(is, entry.streamName);
            is.close();
        }
    } catch (Exception e) {
        IOUtils.closeQuietly(fsOut);
        if (e instanceof GeneralSecurityException) {
            throw (GeneralSecurityException) e;
        } else if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw new IOException("summary entries can't be read", e);
        }
    } finally {
        IOUtils.closeQuietly(leis);
        IOUtils.closeQuietly(sbis);
    }
    return fsOut;
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ChunkedCipherInputStream(org.apache.poi.poifs.crypt.ChunkedCipherInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) EOFException(java.io.EOFException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) BoundedInputStream(org.apache.poi.util.BoundedInputStream) EOFException(java.io.EOFException)

Example 8 with DocumentNode

use of org.apache.poi.poifs.filesystem.DocumentNode 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 9 with DocumentNode

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

the class HSSFWorkbook method write.

/**
     * Write out this workbook to the currently open {@link File} via the
     *  writeable {@link POIFSFileSystem} it was opened as. 
     *  
     * <p>This will fail (with an {@link IllegalStateException} if the
     *  Workbook was opened read-only, opened from an {@link InputStream}
     *   instead of a File, or if this is not the root document. For those cases, 
     *   you must use {@link #write(OutputStream)} or {@link #write(File)} to 
     *   write to a brand new document.
     */
@Override
public void write() throws IOException {
    validateInPlaceWritePossible();
    final DirectoryNode dir = getDirectory();
    // Update the Workbook stream in the file
    DocumentNode workbookNode = (DocumentNode) dir.getEntry(getWorkbookDirEntryName(dir));
    NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode);
    workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
    // Update the properties streams in the file
    writeProperties();
    // Sync with the File on disk
    dir.getFileSystem().writeFilesystem();
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) FilteringDirectoryNode(org.apache.poi.poifs.filesystem.FilteringDirectoryNode) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) NPOIFSDocument(org.apache.poi.poifs.filesystem.NPOIFSDocument)

Aggregations

DocumentNode (org.apache.poi.poifs.filesystem.DocumentNode)9 IOException (java.io.IOException)6 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)5 Entry (org.apache.poi.poifs.filesystem.Entry)4 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)3 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 NPOIFSDocument (org.apache.poi.poifs.filesystem.NPOIFSDocument)2 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ZipEntry (java.util.zip.ZipEntry)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1