3

I need to open docx file as zip archive, find text from word/document.xml, than replace some strings and put it's back to docx.

The problem that after this manipulation Word is claim that it's incorrect docx, but if I push "try to recover document" it's open normally.

If I take word/document.xml from archive and put it's back with WinRAR it's open totally correct.

I can't figure the reasons. I tried to make binary diff of two files, but there are a lot of difference and I can't understand which section is ok and witch is wrong.

I am using this archive module.

Here is my code (if it's help):

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

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

1 Answers1

0

I am using this archive module.

And it or your code (probably last line, where you save archive) produces broken files. Looks like Word is able to restore those non-critical parts of the document, so that's why it opens when you click try to recover document.

                     7zip archive test

beatcracker
  • 6,714
  • 1
  • 18
  • 41