使用zip存档后如何制作正确的docx?

时间:2015-04-12 19:26:45

标签: ms-word zip d docx winrar

我需要将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());

docx files

1 个答案:

答案 0 :(得分:0)

  

我正在使用this存档模块。

它或您的代码(可能是保存存档的最后一行)会产生损坏的文件。看起来Word能够恢复文档的那些非关键部分,这就是当您点击try to recover document时它打开的原因。

7zip archive test