RTF 형식 MSG에서 내장된 첨부 파일 식별 및 추출
Contents
[
Hide
]
RTF 형식 본문을 가진 이메일 메시지는 전체 객체로 내장되었거나 아이콘으로 내장된 inline 첨부 파일을 포함할 수 있습니다. 이 두 종류의 첨부 파일을 구분하려면 먼저 첨부 파일의 특정 속성을 조사해야 합니다. 첨부 파일 속성을 기반으로 특정 기준을 충족한 후, ObjectData에서 추출하여 첨부 파일을 저장할 수 있습니다.
이 문서는 RTF 형식 MSG 파일에서 내장된 첨부 파일을 식별하고 추출합니다.
Java
static void ExtractInlineAttachments()
{
MapiMessage message = MapiMessage.fromFile("Test.msg");
MapiAttachmentCollection attachments = message.getAttachments();
for (Object untypedAttachment : attachments)
{
MapiAttachment attachment = (MapiAttachment) untypedAttachment;
if(IsAttachmentInline(attachment))
{
try
{
SaveAttachment(attachment, UUID.randomUUID().toString());
}
catch (IOException | FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static boolean IsAttachmentInline(MapiAttachment attachment)
{
MapiObjectProperty objectData = attachment.getObjectData();
if (objectData == null)
return false;
for (Object prop : attachment.getObjectData().getProperties().getValues())
{
MapiProperty property = (MapiProperty)prop;
if ("\u0003ObjInfo".equals(property.getName()))
{
byte[] data = property.getData();
int odtPersist1 = data[1] << 8 | data[0];
return (odtPersist1 & 0x40) == 0;
}
}
return false;
}
static void SaveAttachment(MapiAttachment attachment, String fileName) throws IOException, FileNotFoundException
{
for (Object prop : attachment.getObjectData().getProperties().getValues())
{
MapiProperty property = (MapiProperty)prop;
if ("Package".equals(property.getName()))
{
FileOutputStream fs;
try
{
fs = new FileOutputStream(fileName);
fs.write(property.getData(), 0, property.getData().length);
}
catch (java.io.IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}