2015-04-12 178 views
2

我需要打開docx文件作爲zip檔案,從word/document.xml找到文本,比替換一些字符串,並把它放回到docx。如何在使用zip壓縮文件後製作正確的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

回答

0

我使用this存檔模塊。

而它或你的代碼(可能是最後一行,保存檔案)會產生破損的文件。看起來Word能夠恢復文檔的非關鍵部分,所以當您點擊try to recover document時,它就會打開。

                                          7zip archive test