This document describes how to use the POIFS APIs to read, write, and modify files that employ a POIFS-compatible data structure to organize their content.
This document is intended for Java developers who need to use the POIFS APIs to read, write, or modify files that employ a POIFS-compatible data structure to organize their content. It is not necessary for developers to understand the POIFS data structures, and an explanation of those data structures is beyond the scope of this document. It is expected that the members of the target audience will understand the rudiments of a hierarchical file system, and familiarity with the event pattern employed by Java APIs such as AWT would be helpful.
This document attempts to be consistent in its terminology, which is defined here:
The POIFS API provides ways to read, modify and write files and streams that employ a POIFS-compatible data structure to organize their content. The following use cases are covered:
This section covers reading a file system. There are two ways to read a file system; these techniques are sketched out in the following table, and then explained in greater depth in the sections following the table.
In this technique for reading, certain key structures are loaded into memory, and the entire directory tree can be walked by the application, reading specific documents at leisure.
If you create a POIFSFileSystem instance from a File, the memory footprint is very small. However, if you createa a POIFSFileSystem instance from an input stream, then the whole contents must be buffered into memory to allow random access. As such, you should budget on memory use of up to 20% of the file size when using a File, or up to 120% of the file size when using an InputStream.
Before an application can read a file from the file system, the file system needs to be opened
and core parts processed. This is done using the
org.apache.poi.poifs.filesystem.POIFSFileSystem
class. Once the file system has been loaded into memory, the application may need the root
directory. The following code fragment will accomplish this preparation stage:
Assuming no exception was thrown, the file system can then be read.
Once the file system has been loaded into memory and the root directory has been obtained, the
root directory can be read. The following code fragment shows how to read the entries in an
org.apache.poi.poifs.filesystem.DirectoryEntry
instance:
There are a couple of ways to read a document, depending on whether the document resides in the
root directory or in another directory. Either way, you will obtain an
org.apache.poi.poifs.filesystem.DocumentInputStream
instance.
The DocumentInputStream class is a simple implementation of InputStream that makes a few guarantees worth noting:
available()
always returns the number of bytes in the document from your current position in the
document.
markSupported()
returns true
.
mark(int limit)
ignores the limit parameter; basically the method marks the current position in the
document.
reset()
takes you back to the position when mark()
was last called, or to the
beginning of the document if mark()
has not been called.
skip(long n)
will take you to your current position + n (but not past the end of the document).
The behavior of available
means you can read in a document in a single read call
like this:
The combination of mark
, reset
, and skip
provide the
basic mechanisms needed for random access of the document contents.
If the document resides in the root directory, you can obtain a DocumentInputStream
like this:
A more generic technique for reading a document is to obtain an
org.apache.poi.poifs.filesystem.DirectoryEntry
instance for the directory containing the desired document (recall that you can use
getRoot()
to obtain the root directory from its file system). From that DirectoryEntry, you can
then obtain a DocumentInputStream
like this:
The event-driven API for reading documents is a little more complicated and requires that your application know, in advance, which files it wants to read. The benefit of using this API is that each document is in memory just long enough for your application to read it, and documents that you never read at all are not in memory at all. When you're finished reading the documents you wanted, the file system has no data structures associated with it at all and can be discarded.
The preparation phase involves creating an instance of
org.apache.poi.poifs.eventfilesystem.POIFSReader
and to then register one or more
org.apache.poi.poifs.eventfilesystem.POIFSReaderListener
instances with the POIFSReader
.
org.apache.poi.poifs.eventfilesystem.POIFSReaderListener
is an interface used to register for documents. When a matching document is read by the
org.apache.poi.poifs.eventfilesystem.POIFSReader
, the POIFSReaderListener
instance
receives an org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent
instance, which
contains an open DocumentInputStream
and information about the document.
A POIFSReaderListener
instance can register for individual documents, or it can
register for all documents; once it has registered for all documents, subsequent (and previous!)
registration requests for individual documents are ignored. There is no way to unregister
a POIFSReaderListener
.
Thus, it is possible to register a single POIFSReaderListener
for multiple documents
- one, some, or all documents. It is guaranteed that a single POIFSReaderListener
will
receive exactly one notification per registered document. There is no guarantee as to the order
in which it will receive notification of its documents, as future implementations of
POIFSReader
are free to change the algorithm for walking the file system's directory structure.
It is also permitted to register more than one POIFSReaderListener
for the same
document. There is no guarantee of ordering for notification of POIFSReaderListener
instances
that have registered for the same document when POIFSReader
processes that
document.
It is guaranteed that all notifications occur in the same thread. A future enhancement may be
made to provide multi-threaded notifications, but such an enhancement would very probably be
made in a new reader class, a ThreadedPOIFSReader
perhaps.
The following describes the three ways to register a POIFSReaderListener
for a
document or set of documents:
The org.apache.poi.poifs.filesystem.POIFSDocumentPath
class is used to describe a
directory in a POIFS file system. Since there are no reserved characters in the name of a file
in a POIFS file system, a more traditional string-based solution for describing a directory,
with special characters delimiting the components of the directory name, is not feasible. The
constructors for the class are used as follows:
Constructor example | Directory described |
new POIFSDocumentPath() | The root directory. |
new POIFSDocumentPath(null) | The root directory. |
new POIFSDocumentPath(new String[ 0 ]) | The root directory. |
new POIFSDocumentPath(new String[ ] { "foo", "bar"} ) | in Unix terminology, "/foo/bar". |
new POIFSDocumentPath(new POIFSDocumentPath(new String[] { "foo" }), new String[ ] { "fu", "bar"} ) | in Unix terminology, "/foo/fu/bar". |
Processing org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent
events is
relatively easy. After all of the POIFSReaderListener
instances have been
registered with POIFSReader
, the POIFSReader.read(InputStream stream)
method
is called.
Assuming that there are no problems with the data, as the POIFSReader
processes the
documents in the specified InputStream
's data, it calls registered
POIFSReaderListener
instances' processPOIFSReaderEvent
method with a POIFSReaderEvent
instance.
The POIFSReaderEvent
instance contains information to identify the document (a
POIFSDocumentPath
object to identify the directory that the document is in, and the document name), and an
open DocumentInputStream
instance from which to read the document.
Writing a file system is very much like reading a file system in that there are multiple ways to do so. You can load an existing file system into memory and modify it (removing files, renaming files) and/or add new files to it, and write it, or you can start with a new, empty file system:
There are two restrictions on the names of files in a file system that must be considered when creating files:
A document can be created by acquiring a DirectoryEntry
and calling one of the two
createDocument
methods:
Unlike reading, you don't have to choose between the in-memory and event-driven writing models; both can co-exist in the same file system.
Writing is initiated when the POIFSFileSystem
instance's writeFilesystem()
method
is called with an OutputStream
to write to.
The event-driven model is quite similar to the event-driven model for reading, in that the file
system calls your org.apache.poi.poifs.filesystem.POIFSWriterListener
when it's time to
write your document, just as the POIFSReader
calls your POIFSReaderListener
when it's time to read your document. Internally, when writeFilesystem()
is
called, the final POIFS data structures are created and are written to the specified
OutputStream
. When the file system needs to write a document out that was created with
the event-driven model, it calls the POIFSWriterListener
back, calling its
processPOIFSWriterEvent()
method, passing an org.apache.poi.poifs.filesystem.POIFSWriterEvent
instance.
This object contains the POIFSDocumentPath
and name of the document, its size, and an
open org.apache.poi.poifs.filesystem.DocumentOutputStream
to which to write. A
DocumentOutputStream
is a wrapper over the OutputStream
that was provided to the
POIFSFileSystem
to write to, and has the responsibility of making sure that the document your application
writes fits within the size you specified for it.
If you are using a POIFSFileSystem
loaded from a
File
with readOnly
set to false, it is also possible to do an in-place write. Simply call
writeFilesystem()
to have the (limited) in-memory structures synced with the disk, then close()
to
finish.
Creating a directory is similar to creating a document, except that there's only one way to do so:
As with reading documents, it is possible to create a new document or directory in the root directory by using convenience methods of POIFSFileSystem.
DirectoryEntry Method Signature | POIFSFileSystem Method Signature |
createDocument(String name, InputStream stream) | createDocument(InputStream stream, String name) |
createDocument(String name, int size, POIFSWriterListener writer) | createDocument(String name, int size, POIFSWriterListener writer) |
createDirectory(String name) | createDirectory(String name) |
It is possible to modify an existing POIFS file system, whether it's one your application has loaded into memory, or one which you are creating on the fly.
Removing a document is simple: you get the Entry
corresponding to the document and call
its delete()
method. This is a boolean method, but should always return
true
, indicating that the operation succeeded.
Removing a directory is also simple: you get the Entry
corresponding to the directory
and call its delete()
method. This is a boolean method, but, unlike deleting a
document, may not always return true
, indicating that the operation succeeded. Here are
the reasons why the operation may fail:
isEmpty()
on its
DirectoryEntry; is the return value false
?)
There are two ways available to change the contents of an existing file within a POIFS file system.
One is using a DocumentOutputStream
, the other is with
POIFSDocument.replaceContents
If you have available to you an InputStream
to read the new File contents from, then the
easiest way is via
POIFSDocument.replaceContents
. You would do something like:
Alternately, if you either have a byte array, or you need to write as you go along, then the
OutputStream interface provided by
DocumentOutputStream
will likely be a better bet. Your code would want to look somewhat like:
For an example of an in-place change to one stream within a file, you can see the example org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
Regardless of whether the file is a directory or a document, it can be renamed, with one exception -
the root directory has a special name that is expected by the components of a major software
vendor's office suite, and the POIFS API will not let that name be changed. Renaming is done by
acquiring the file's corresponding Entry
instance and calling its renameTo
method,
passing in the new name.
Like delete
, renameTo
returns true
if the operation succeeded,
otherwise false
. Reasons for failure include these: