我对Ruby比较陌生,我必须使用1.8.7版本。
我尝试使用'zip'并在尝试使用文档中的示例时最终得到了一个神秘的堆栈跟踪。我的剧本
#!/usr/bin/env ruby
require 'rubygems'
require 'zip'
require 'zip/zipfilesystem'
Zip::ZipFile.open("myfile.war") do |a|
puts a.file.read("META-INF/context.xml")
end
产生以下内容:
$./rzip.rb
/var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup': can't dup NilClass (TypeError)
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `map'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1373:in `initialize'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `new'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `open'
from ./rzip.rb:7
我的代码出了什么问题?或者它是一些版本不兼容问题?如果不是我的错误,我应该如何使用zip档案操作?
答案 0 :(得分:0)
我无法重现您的错误although it seems you are not alone in this。我可以建议使用Zip :: ZipFile。这在我的系统上运行(Ubuntu 12.04 LTS):
require 'zip/zip'
Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
|zipfile|
zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
zipfile.mkdir("a_dir")
}
您还可以看到see some good examples/usage of Zip::ZipFile on GitHub