Ms word 如何在将docx作为zip存档使用后制作正确的docx?

Ms word 如何在将docx作为zip存档使用后制作正确的docx?,ms-word,zip,d,docx,winrar,Ms Word,Zip,D,Docx,Winrar,我需要打开docx文件作为zip存档,从word/document.xml中查找文本,然后替换一些字符串并将其放回docx 问题是,在这个操作之后,单词声称它是不正确的docx,但如果我按下“尝试恢复文档”,它会正常打开 如果我从归档文件中取出word/document.xml,并将其与WinRAR一起放回,那么它是完全正确的 我想不出原因。我试着对两个文件进行二进制区分,但是有很多不同,我不知道哪个部分是正确的,哪个部分是错误的 我正在使用归档模块 这是我的代码(如果有帮助): 我正在使用归

我需要打开docx文件作为zip存档,从
word/document.xml
中查找文本,然后替换一些字符串并将其放回docx

问题是,在这个操作之后,单词声称它是不正确的docx,但如果我按下“尝试恢复文档”,它会正常打开

如果我从归档文件中取出
word/document.xml
,并将其与WinRAR一起放回,那么它是完全正确的

我想不出原因。我试着对两个文件进行二进制区分,但是有很多不同,我不知道哪个部分是正确的,哪个部分是错误的

我正在使用归档模块

这是我的代码(如果有帮助):

我正在使用归档模块

它或您的代码(可能是最后一行,您保存归档文件的地方)会生成损坏的文件。看起来Word能够恢复文档的非关键部分,所以当您单击“尝试恢复文档”时,它会打开

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());