use of org.apache.poi.poifs.filesystem.DirectoryEntry in project tika by apache.
the class POIFSContainerDetector method processCompObjFormatType.
/**
* Is this one of the kinds of formats which uses CompObj to
* store all of their data, eg Star Draw, Star Impress or
* (older) Works?
* If not, it's likely an embedded resource
*/
private static MediaType processCompObjFormatType(DirectoryEntry root) {
try {
Entry e = root.getEntry("CompObj");
if (e != null && e.isDocumentEntry()) {
DocumentNode dn = (DocumentNode) e;
DocumentInputStream stream = new DocumentInputStream(dn);
byte[] bytes = IOUtils.toByteArray(stream);
/*
* This array contains a string with a normal ASCII name of the
* application used to create this file. We want to search for that
* name.
*/
if (arrayContains(bytes, MS_GRAPH_CHART_BYTES)) {
return MS_GRAPH_CHART;
} else if (arrayContains(bytes, STAR_DRAW)) {
return SDA;
} else if (arrayContains(bytes, STAR_IMPRESS)) {
return SDD;
} else if (arrayContains(bytes, WORKS_QUILL96)) {
return WPS;
}
}
} catch (Exception e) {
/*
* "root.getEntry" can throw FileNotFoundException. The code inside
* "if" can throw IOExceptions. Theoretically. Practically no
* exceptions will likely ever appear.
*
* Swallow all of them. If any occur, we just assume that we can't
* distinguish between Draw and Impress and return something safe:
* x-tika-msoffice
*/
}
return OLE;
}
use of org.apache.poi.poifs.filesystem.DirectoryEntry in project CodeUtils by boredream.
the class TempUtils method setPPT.
public static void setPPT() {
try {
PowerPointExtractor ppe = new PowerPointExtractor("temp" + File.separator + "office" + File.separator + "ppt2007.ppt");
DocumentSummaryInformation dsi = ppe.getDocSummaryInformation();
DirectoryEntry root = ppe.getRoot();
System.out.println(dsi.getSlideCount());
System.out.println(root.getName());
} catch (Exception e) {
e.printStackTrace();
}
// SlideShow _slideShow = new SlideShow();
// Slide slide = _slideShow.createSlide();
//
// // 创建并置入简单文本
// TextBox _text = new TextBox();
// TextRun _textRun = _text.createTextRun();
// _textRun.setRawText("杜磊米");
// _text.setAnchor(new Rectangle(10,10,100,100));
//
// // 创建并置入带有样式的文本
// AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); //设置形状
// TextRun _autoText = _autoShape.createTextRun();
// _autoText.setRawText("杜磊米");
// _autoShape.setAnchor(new Rectangle(200,200,100,100));
// _autoShape.setFillColor(new Color(170,215,255));
// _autoShape.setLineWidth(5.0);
// _autoShape.setLineStyle(Line.LINE_DOUBLE);
//
// // AutoShape 对象可以设置多个不同样式文本
// TextRun _autoText2 = _autoShape.createTextRun();
// RichTextRun _richText = _autoText2.appendText("杜");
// _richText.setFontColor(new Color(255,255,255));
// RichTextRun _richText2 = _autoText2.appendText("磊米");
// _richText2.setFontColor(new Color(255,0,0));
// _richText2.setFontSize(12);
//
// // 将文本对象置入幻灯片
// slide.addShape(_text);
// slide.addShape(_autoShape);
//
//
//
// // 输出文件
// try {
// _slideShow.write(new FileOutputStream("temp\\office\\test.pptx"));
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
use of org.apache.poi.poifs.filesystem.DirectoryEntry in project poi by apache.
the class OLE2ExtractorFactory method getEmbededDocsTextExtractors.
/**
* Returns an array of text extractors, one for each of
* the embedded documents in the file (if there are any).
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*/
public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtractor ext) throws IOException {
// All the embedded directories we spotted
List<Entry> dirs = new ArrayList<Entry>();
// For anything else not directly held in as a POIFS directory
List<InputStream> nonPOIFS = new ArrayList<InputStream>();
// Find all the embedded directories
DirectoryEntry root = ext.getRoot();
if (root == null) {
throw new IllegalStateException("The extractor didn't know which POIFS it came from!");
}
if (ext instanceof ExcelExtractor) {
// These are in MBD... under the root
Iterator<Entry> it = root.getEntries();
while (it.hasNext()) {
Entry entry = it.next();
if (entry.getName().startsWith("MBD")) {
dirs.add(entry);
}
}
} else {
// Ask Scratchpad, or fail trying
Class<?> cls = getScratchpadClass();
try {
Method m = cls.getDeclaredMethod("identifyEmbeddedResources", POIOLE2TextExtractor.class, List.class, List.class);
m.invoke(null, ext, dirs, nonPOIFS);
} catch (Exception e) {
throw new IllegalArgumentException("Error checking for Scratchpad embedded resources", e);
}
}
// Create the extractors
if (dirs.size() == 0 && nonPOIFS.size() == 0) {
return new POITextExtractor[0];
}
ArrayList<POITextExtractor> e = new ArrayList<POITextExtractor>();
for (Entry dir : dirs) {
e.add(createExtractor((DirectoryNode) dir));
}
for (InputStream nonPOIF : nonPOIFS) {
try {
e.add(createExtractor(nonPOIF));
} catch (IllegalArgumentException ie) {
// Ignore, just means it didn't contain
// a format we support as yet
LOGGER.log(POILogger.WARN, ie);
} catch (Exception xe) {
// Ignore, invalid format
LOGGER.log(POILogger.WARN, xe);
}
}
return e.toArray(new POITextExtractor[e.size()]);
}
use of org.apache.poi.poifs.filesystem.DirectoryEntry in project poi by apache.
the class PropertySet method write.
/**
* Writes a property set to a document in a POI filesystem directory.
*
* @param dir The directory in the POI filesystem to write the document to.
* @param name The document's name. If there is already a document with the
* same name in the directory the latter will be overwritten.
*
* @throws WritingNotSupportedException if the filesystem doesn't support writing
* @throws IOException if the old entry can't be deleted or the new entry be written
*/
public void write(final DirectoryEntry dir, final String name) throws WritingNotSupportedException, IOException {
/* If there is already an entry with the same name, remove it. */
if (dir.hasEntry(name)) {
final Entry e = dir.getEntry(name);
e.delete();
}
/* Create the new entry. */
dir.createDocument(name, toInputStream());
}
use of org.apache.poi.poifs.filesystem.DirectoryEntry in project poi by apache.
the class CopyCompare method main.
/**
* <p>Runs the example program. The application expects one or two
* arguments:</p>
*
* <ol>
*
* <li><p>The first argument is the disk file name of the POI filesystem to
* copy.</p></li>
*
* <li><p>The second argument is optional. If it is given, it is the name of
* a disk file the copy of the POI filesystem will be written to. If it is
* not given, the copy will be written to a temporary file which will be
* deleted at the end of the program.</p></li>
*
* </ol>
*
* @param args Command-line arguments.
* @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.
* @exception IOException if any I/O exception occurs.
* @exception UnsupportedEncodingException if a character encoding is not
* supported.
*/
public static void main(final String[] args) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
String originalFileName = null;
String copyFileName = null;
/* Check the command-line arguments. */
if (args.length == 1) {
originalFileName = args[0];
File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");
f.deleteOnExit();
copyFileName = f.getAbsolutePath();
} else if (args.length == 2) {
originalFileName = args[0];
copyFileName = args[1];
} else {
System.err.println("Usage: " + CopyCompare.class.getName() + "originPOIFS [copyPOIFS]");
System.exit(1);
}
/* Read the origin POIFS using the eventing API. The real work is done
* in the class CopyFile which is registered here as a POIFSReader. */
final POIFSReader r = new POIFSReader();
final CopyFile cf = new CopyFile(copyFileName);
r.registerListener(cf);
r.setNotifyEmptyDirectories(true);
FileInputStream fis = new FileInputStream(originalFileName);
r.read(fis);
fis.close();
/* Write the new POIFS to disk. */
cf.close();
/* Read all documents from the original POI file system and compare them
* with the equivalent document from the copy. */
POIFSFileSystem opfs = null, cpfs = null;
try {
opfs = new POIFSFileSystem(new File(originalFileName));
cpfs = new POIFSFileSystem(new File(copyFileName));
final DirectoryEntry oRoot = opfs.getRoot();
final DirectoryEntry cRoot = cpfs.getRoot();
final StringBuffer messages = new StringBuffer();
if (equal(oRoot, cRoot, messages)) {
System.out.println("Equal");
} else {
System.out.println("Not equal: " + messages);
}
} finally {
IOUtils.closeQuietly(cpfs);
IOUtils.closeQuietly(opfs);
}
}
Aggregations