2012-04-06 47 views
1

試圖使用rubyzip添加評論到一個zip文件,否則我根本不會修改。保存與rubyzip zip的評論

zf = Zip::ZipFile.open 'Archive.zip' 
zf.comment = "blah blah blah" 

我試過zf.closezf.commit沒有運氣。我正在閱讀文檔,但似乎無法找到解決方案。

以前有人做過這個嗎?

+1

如果壓縮文件的實際內容發生變化,Rubyzip只更新註釋,所以唯一的解決方法是@ knut的答案在下面。我只是修復了這個bug,並在GitHub上發送了一個pull請求:https://github.com/aussiegeek/rubyzip/pull/41 – 2012-04-06 23:45:17

回答

1

升級到0.9.7 RubyZip:

require 'zip/zipfilesystem' 

zf = Zip::ZipFile.open 'Archive.zip' 
zf.comment = "CHANGED COMMENT" 
zf.get_output_stream("second.txt") { |f| f.puts "Hello from ZipFile" } 

zf.close 

在此基礎上,你可以做以下),它修復了這個錯誤。

+0

太棒了,作品像一個......呃......寶石!謝謝 – Jakanapes 2012-04-09 13:39:40

1

我用下面的代碼試了成功:

require 'zip/zipfilesystem' 

zf = Zip::ZipFile.open 'Archive.zip', 'w' 
zf.comment = "blah blah blah" 

zf.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" } 
zf.close 

我添加了至少一個文件來創建壓縮文件。沒有內容,沒有zip文件(單獨評論似乎不是內容)。

您不需要創建zip文件,您想修改一個zip文件。

這工作還可以,但它也改變了zip文件:(今日發佈

require 'zip/zipfilesystem' 

zf = Zip::ZipFile.open 'Archive.zip' 
zf.comment = "CHANGED COMMENT" 
zf.get_output_stream("second.txt") { |f| f.puts "Hello from ZipFile" } 
zf.commit #write the data and change the commen 
zf.remove("second.txt") #remove the data again - the comment changed 

zf.close 
+0

不完全是我想要做的,但那是有效的。直到更新寶石,我總是可以添加和刪除更新評論。謝謝! – Jakanapes 2012-04-07 17:09:05