rubyzip と nokogiri を組み合わせて .docx ファイルを編集しています。rubyzip を使用して .docx ファイルを解凍し、nokogiri を使用して word/document.xml ファイルの本文を解析および変更していますが、最後に rubyzip を閉じるたびにファイルが破損し、開くことができません。修理してください。デスクトップで .docx ファイルを解凍し、word/document.xml ファイルを確認すると、コンテンツは変更後の内容に更新されていますが、他のすべてのファイルが台無しになっています。誰かがこの問題について私を助けてくれますか? これが私のコードです:
require 'rubygems'
require 'zip/zip'
require 'nokogiri'
zip = Zip::ZipFile.open("test.docx")
doc = zip.find_entry("word/document.xml")
xml = Nokogiri::XML.parse(doc.get_input_stream)
wt = xml.root.xpath("//w:t", {"w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}).first
wt.content = "New Text"
zip.get_output_stream("word/document.xml") {|f| f << xml.to_s}
zip.close