use of org.apache.poi.hmef.Attachment in project poi by apache.
the class HMEFContentsExtractor method extractAttachments.
/**
* Extracts all the message attachments to the supplied directory
*/
public void extractAttachments(File dir) throws IOException {
int count = 0;
for (Attachment att : message.getAttachments()) {
count++;
// Decide what to call it
String filename = att.getLongFilename();
if (filename == null || filename.length() == 0) {
filename = att.getFilename();
}
if (filename == null || filename.length() == 0) {
filename = "attachment" + count;
if (att.getExtension() != null) {
filename += att.getExtension();
}
}
// Save it
File file = new File(dir, filename);
OutputStream fout = new FileOutputStream(file);
try {
fout.write(att.getContents());
} finally {
fout.close();
}
}
}
use of org.apache.poi.hmef.Attachment in project tika by apache.
the class TNEFParser method parse.
/**
* Extracts properties and text from an MS Document input stream
*/
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
// We work by recursing, so get the appropriate bits
EmbeddedDocumentExtractor embeddedExtractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
// Ask POI to process the file for us
HMEFMessage msg = new HMEFMessage(stream);
// Set the message subject if known
String subject = msg.getSubject();
if (subject != null && subject.length() > 0) {
// TODO: Move to title in Tika 2.0
metadata.set(TikaCoreProperties.TRANSITION_SUBJECT_TO_DC_TITLE, subject);
}
// Recurse into the message body RTF
MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
if (attr != null && attr instanceof MAPIRtfAttribute) {
MAPIRtfAttribute rtf = (MAPIRtfAttribute) attr;
handleEmbedded("message.rtf", "application/rtf", rtf.getData(), embeddedExtractor, handler);
}
// Recurse into each attachment in turn
for (Attachment attachment : msg.getAttachments()) {
String name = attachment.getLongFilename();
if (name == null || name.length() == 0) {
name = attachment.getFilename();
}
if (name == null || name.length() == 0) {
String ext = attachment.getExtension();
if (ext != null) {
name = "unknown" + ext;
}
}
handleEmbedded(name, null, attachment.getContents(), embeddedExtractor, handler);
}
}
Aggregations