我需要打开 docx 文件作为 zip 存档,从 word/document.xml
中找到文本,然后替换一些字符串并将其放回 docx。
此操作后的问题是 Word 声称它是不正确的 docx,但如果我按下“尝试恢复文档”,它会正常打开。
如果我从压缩包中取出 word/document.xml
并用 WinRAR 将其放回原位,则它打开时完全正确。
我想不出原因。我尝试对两个文件进行二进制比较,但差异很大,我无法理解哪个部分是正确的,哪个部分是错误的。
我正在使用 this 存档模块。
这是我的代码(如果有帮助的话):
string doc_xml_content;
string result_xml;
auto archFile = new ZipArchive(std.file.read(zipFullName));
auto document_xml_file = archFile.getFile("word/document.xml");
// getting file content
doc_xml_content = cast(string)document_xml_file.data;
result_xml = doc_xml_content;
// document.xml in memory
auto document_xml_result = new ZipArchive.File("word/document.xml");
document_xml_result.data = result_xml;
//writeln(result_xml);
// remove old
archFile.removeFile("word/document.xml");
archFile.addFile(document_xml_result);
std.file.write("my1.docx", cast(ubyte[])archFile.serialize());
最佳答案
I am using this archive module.
它或您的代码(可能是最后一行,您保存存档的地方)会产生损坏的文件。看起来 Word 能够恢复文档的那些非关键部分,所以当您单击 try to recover document
时它会打开。
关于ms-word - 如何在将其用作 zip 存档后制作正确的 docx?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593676/