Search in sources :

Example 1 with DocumentInputStream

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

the class HPSFFileHandler method hasPropertyStream.

private static boolean hasPropertyStream(POIFSFileSystem poifs, String streamName) throws IOException, MarkUnsupportedException {
    DirectoryNode root = poifs.getRoot();
    if (!root.hasEntry(streamName)) {
        return false;
    }
    DocumentInputStream dis = root.createDocumentInputStream(streamName);
    try {
        return PropertySet.isPropertySetStream(dis);
    } finally {
        dis.close();
        ;
    }
}
Also used : DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 2 with DocumentInputStream

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

the class VBAMacroReader method readMacros.

/**
     * Reads VBA Project modules from a VBA Project directory located at
     * <tt>macroDir</tt> into <tt>modules</tt>.
     *
     * @since 3.15-beta2
     */
protected void readMacros(DirectoryNode macroDir, ModuleMap modules) throws IOException {
    for (Entry entry : macroDir) {
        if (!(entry instanceof DocumentNode)) {
            continue;
        }
        String name = entry.getName();
        DocumentNode document = (DocumentNode) entry;
        DocumentInputStream dis = new DocumentInputStream(document);
        try {
            if ("dir".equalsIgnoreCase(name)) {
                // process DIR
                RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
                String streamName = null;
                int recordId = 0;
                try {
                    while (true) {
                        recordId = in.readShort();
                        if (EOF == recordId || VERSION_INDEPENDENT_TERMINATOR == recordId) {
                            break;
                        }
                        int recordLength = in.readInt();
                        switch(recordId) {
                            case PROJECTVERSION:
                                trySkip(in, 6);
                                break;
                            case PROJECTCODEPAGE:
                                int codepage = in.readShort();
                                modules.charset = Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
                                break;
                            case STREAMNAME:
                                streamName = readString(in, recordLength, modules.charset);
                                int reserved = in.readShort();
                                if (reserved != STREAMNAME_RESERVED) {
                                    throw new IOException("Expected x0032 after stream name before Unicode stream name, but found: " + Integer.toHexString(reserved));
                                }
                                int unicodeNameRecordLength = in.readInt();
                                readUnicodeString(in, unicodeNameRecordLength);
                                // do something with this at some point
                                break;
                            case MODULEOFFSET:
                                readModule(in, streamName, modules);
                                break;
                            default:
                                trySkip(in, recordLength);
                                break;
                        }
                    }
                } catch (final IOException e) {
                    throw new IOException("Error occurred while reading macros at section id " + recordId + " (" + HexDump.shortToHex(recordId) + ")", e);
                } finally {
                    in.close();
                }
            } else if (!startsWithIgnoreCase(name, "__SRP") && !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
                // process module, skip __SRP and _VBA_PROJECT since these do not contain macros
                readModule(dis, name, modules);
            }
        } finally {
            dis.close();
        }
    }
}
Also used : RLEDecompressingInputStream(org.apache.poi.util.RLEDecompressingInputStream) Entry(org.apache.poi.poifs.filesystem.Entry) ZipEntry(java.util.zip.ZipEntry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 3 with DocumentInputStream

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

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

the class POIFSDump method dump.

public static void dump(DirectoryEntry root, File parent) throws IOException {
    for (Iterator<Entry> it = root.getEntries(); it.hasNext(); ) {
        Entry entry = it.next();
        if (entry instanceof DocumentNode) {
            DocumentNode node = (DocumentNode) entry;
            DocumentInputStream is = new DocumentInputStream(node);
            byte[] bytes = IOUtils.toByteArray(is);
            is.close();
            OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
            try {
                out.write(bytes);
            } finally {
                out.close();
            }
        } else if (entry instanceof DirectoryEntry) {
            DirectoryEntry dir = (DirectoryEntry) entry;
            File file = new File(parent, entry.getName());
            if (!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(dir, file);
        } else {
            System.err.println("Skipping unsupported POIFS entry: " + entry);
        }
    }
}
Also used : Entry(org.apache.poi.poifs.filesystem.Entry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File)

Example 5 with DocumentInputStream

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

the class POIFSReader method processProperties.

private void processProperties(final BlockList small_blocks, final BlockList big_blocks, final Iterator<Property> properties, final POIFSDocumentPath path) throws IOException {
    if (!properties.hasNext() && notifyEmptyDirectories) {
        Iterator<POIFSReaderListener> listeners = registry.getListeners(path, ".");
        while (listeners.hasNext()) {
            POIFSReaderListener pl = listeners.next();
            POIFSReaderEvent pe = new POIFSReaderEvent(null, path, null);
            pl.processPOIFSReaderEvent(pe);
        }
        return;
    }
    while (properties.hasNext()) {
        Property property = properties.next();
        String name = property.getName();
        if (property.isDirectory()) {
            POIFSDocumentPath new_path = new POIFSDocumentPath(path, new String[] { name });
            DirectoryProperty dp = (DirectoryProperty) property;
            processProperties(small_blocks, big_blocks, dp.getChildren(), new_path);
        } else {
            int startBlock = property.getStartBlock();
            Iterator<POIFSReaderListener> listeners = registry.getListeners(path, name);
            if (listeners.hasNext()) {
                int size = property.getSize();
                OPOIFSDocument document = null;
                if (property.shouldUseSmallBlocks()) {
                    document = new OPOIFSDocument(name, small_blocks.fetchBlocks(startBlock, -1), size);
                } else {
                    document = new OPOIFSDocument(name, big_blocks.fetchBlocks(startBlock, -1), size);
                }
                while (listeners.hasNext()) {
                    POIFSReaderListener listener = listeners.next();
                    listener.processPOIFSReaderEvent(new POIFSReaderEvent(new DocumentInputStream(document), path, name));
                }
            } else {
                // consume the document's data and discard it
                if (property.shouldUseSmallBlocks()) {
                    small_blocks.fetchBlocks(startBlock, -1);
                } else {
                    big_blocks.fetchBlocks(startBlock, -1);
                }
            }
        }
    }
}
Also used : POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) OPOIFSDocument(org.apache.poi.poifs.filesystem.OPOIFSDocument) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty)

Aggregations

DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)22 IOException (java.io.IOException)10 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)5 DocumentNode (org.apache.poi.poifs.filesystem.DocumentNode)5 InputStream (java.io.InputStream)4 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)4 PropertySet (org.apache.poi.hpsf.PropertySet)4 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)4 Entry (org.apache.poi.poifs.filesystem.Entry)4 FileNotFoundException (java.io.FileNotFoundException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 BoundedInputStream (org.apache.poi.util.BoundedInputStream)2 RLEDecompressingInputStream (org.apache.poi.util.RLEDecompressingInputStream)2 TikaException (org.apache.tika.exception.TikaException)2 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1