嵌入的 OLE PDF 对象比 DOCX 中的原始对象大



我遇到的问题是,当我使用嵌入的(ole(文件类型创建docx文档时.pdf/embeddings文件夹中生成的二进制文件大于原始文档。

我插入了一个大小为 52076 字节的文档。 如果我将.docx重命名为 zip 并打开它,oleObject1.bin 有 55296 字节

现在,当我想使用Apache POI提取文件时,该文件已存在但已损坏。

有什么想法吗?(我首先认为它可能是压缩的?

感谢

好的,我发现了问题:

例如,对于docx,文件之前有一些数据块(RootEntry,ObjInfo,Content,..(。使用十六进制编辑器,您将看到文件从后面的某个地方开始。我通过查看目录的类型来修复我的提取器 - 对于 pdf,您必须查看 CONTENTS 目录条目:

private void writeBinaryPackagePart(PackagePart part, File targetfolder, String extension, String fileName) throws IOException {
if (StringUtils.isEmpty(fileName)) {
fileName = generateUniqueId(OleExtractorUtils.OfficeType.BINARY).concat(".").concat(extension);
}
InputStream inputStream = FileMagic.prepareToCheckMagic(part.getInputStream());
try {
if (FileMagic.valueOf(inputStream) == FileMagic.OLE2) {
try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(inputStream)) {
if (isOle10Native(npoifsFileSystem.getRoot())) {
byte[] dataBuffer = Ole10Native.createFromEmbeddedOleObject(npoifsFileSystem.getRoot()).getDataBuffer();
writeOle10NativeObject(dataBuffer, fileName, targetfolder);
}
else if (npoifsFileSystem.getRoot().getEntryNames().contains("CONTENTS"))
try (DocumentInputStream contents = npoifsFileSystem.createDocumentInputStream("CONTENTS")) {
writeOle10NativeObject(IOUtils.toByteArray(contents), fileName, targetfolder);
}
}
}
}
catch (Exception e) {
LOGGER.warn("Cannot create Ole10Native from Object {}! Writing the following binary: {}", part.getPartName(), fileName);
ServiceUtil.moveUploadedFileToExistingTempFolder(inputStream, fileName, targetfolder);
inputStream.close();
}
}
private boolean isOle10Native(DirectoryNode directoryNode) {
String ole10Native = Ole10Native.OLE10_NATIVE;
Iterator<Entry> entries = directoryNode.getEntries();
while(entries.hasNext()) {
Entry entry = entries.next();
if (entry.getName().contains(ole10Native)) {
return true;
}
}
return false;
}

最新更新