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