Search in sources :

Example 1 with DocumentEntry

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

the class PropertySetFactory method create.

/**
     * Creates the most specific {@link PropertySet} from an entry
     *  in the specified POIFS Directory. This is preferrably a {@link
     * DocumentSummaryInformation} or a {@link SummaryInformation}. If
     * the specified entry does not contain a property set stream, an 
     * exception is thrown. If no entry is found with the given name,
     * an exception is thrown.
     *
     * @param dir The directory to find the PropertySet in
     * @param name The name of the entry containing the PropertySet
     * @return The created {@link PropertySet}.
     * @throws FileNotFoundException if there is no entry with that name
     * @throws NoPropertySetStreamException if the stream does not
     * contain a property set.
     * @throws IOException if some I/O problem occurs.
     * @exception UnsupportedEncodingException if the specified codepage is not
     * supported.
     */
public static PropertySet create(final DirectoryEntry dir, final String name) throws FileNotFoundException, NoPropertySetStreamException, IOException, UnsupportedEncodingException {
    InputStream inp = null;
    try {
        DocumentEntry entry = (DocumentEntry) dir.getEntry(name);
        inp = new DocumentInputStream(entry);
        try {
            return create(inp);
        } catch (MarkUnsupportedException e) {
            return null;
        }
    } finally {
        if (inp != null) {
            inp.close();
        }
    }
}
Also used : LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) InputStream(java.io.InputStream) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 2 with DocumentEntry

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

the class CopyCompare method equal.

/**
     * <p>Compares two {@link DirectoryEntry} instances of a POI file system.
     * The directories must contain the same streams with the same names and
     * contents.</p>
     *
     * @param d1 The first directory.
     * @param d2 The second directory.
     * @param msg The method may append human-readable comparison messages to
     * this string buffer. 
     * @return <code>true</code> if the directories are equal, else
     * <code>false</code>.
     * @exception MarkUnsupportedException if a POI document stream does not
     * support the mark() operation.
     * @exception NoPropertySetStreamException if the application tries to
     * create a property set from a POI document stream that is not a property
     * set stream.
     * @throws UnsupportedEncodingException 
     * @exception IOException if any I/O exception occurs.
     */
private static boolean equal(final DirectoryEntry d1, final DirectoryEntry d2, final StringBuffer msg) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
    boolean equal = true;
    /* Iterate over d1 and compare each entry with its counterpart in d2. */
    for (final Entry e1 : d1) {
        final String n1 = e1.getName();
        if (!d2.hasEntry(n1)) {
            msg.append("Document \"" + n1 + "\" exists only in the source.\n");
            equal = false;
            break;
        }
        Entry e2 = d2.getEntry(n1);
        if (e1.isDirectoryEntry() && e2.isDirectoryEntry()) {
            equal = equal((DirectoryEntry) e1, (DirectoryEntry) e2, msg);
        } else if (e1.isDocumentEntry() && e2.isDocumentEntry()) {
            equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg);
        } else {
            msg.append("One of \"" + e1 + "\" and \"" + e2 + "\" is a " + "document while the other one is a directory.\n");
            equal = false;
        }
    }
    /* Iterate over d2 just to make sure that there are no entries in d2
         * that are not in d1. */
    for (final Entry e2 : d2) {
        final String n2 = e2.getName();
        Entry e1 = null;
        try {
            e1 = d1.getEntry(n2);
        } catch (FileNotFoundException ex) {
            msg.append("Document \"" + e2 + "\" exitsts, document \"" + e1 + "\" does not.\n");
            equal = false;
            break;
        }
    }
    return equal;
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) FileNotFoundException(java.io.FileNotFoundException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry)

Example 3 with DocumentEntry

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

the class HSLFSlideShowImpl method readPowerPointStream.

/**
     * Extracts the main PowerPoint document stream from the
     * POI file, ready to be passed
     *
     * @throws IOException
     */
private void readPowerPointStream() throws IOException {
    // Get the main document stream
    DocumentEntry docProps = (DocumentEntry) getDirectory().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    // Grab the document stream
    int len = docProps.getSize();
    InputStream is = getDirectory().createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
    try {
        _docstream = IOUtils.toByteArray(is, len);
    } finally {
        is.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) InputStream(java.io.InputStream) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry)

Example 4 with DocumentEntry

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

use of org.apache.poi.poifs.filesystem.DocumentEntry 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)

Aggregations

DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)14 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileNotFoundException (java.io.FileNotFoundException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)3 Entry (org.apache.poi.poifs.filesystem.Entry)3 IOException (java.io.IOException)2 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 Ole10Native (org.apache.poi.poifs.filesystem.Ole10Native)2 Ole10NativeException (org.apache.poi.poifs.filesystem.Ole10NativeException)2 TikaException (org.apache.tika.exception.TikaException)2 TikaInputStream (org.apache.tika.io.TikaInputStream)2 POIFSDocumentType (org.apache.tika.parser.microsoft.OfficeParser.POIFSDocumentType)2 File (java.io.File)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1