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();
;
}
}
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();
}
}
}
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();
}
}
}
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);
}
}
}
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);
}
}
}
}
}
Aggregations