我想用RubyZip压缩目录中包含的所有文件。下面是我有:用Ruby on Rails压缩目录中的文件
def bundle
#create the ZIPfile with the title of (:id).zip
bundle_filename = "public/attachments/#{self.id}/#{self.id}.zip"
#open the ZIPfile in order to add items in
Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
|zipfile|
Dir.foreach("public/attachments/#{self.id}") do |item|
t = File.open(item)
zipfile.add(t, "public/attachments/#{self.id}")
end
}
#change permissions on ZIPfile
File.chmod(0644, bundle_filename)
self.save
end
这成功地执行了第一行,并创建具有正确名称的ZIP文件,但它不加入包含在该目录中的所有文件。有任何想法吗?
你确定'Dir.foreach'调用是添加项目吗?你可以使用'Rails.logger'来调试。 – tadman 2012-07-18 18:21:23
看看这个,http://stackoverflow.com/questions/11525484/zipping-all-files-in-a-dir – PriteshJ 2012-07-18 18:23:36
Wohooo,是来自同一用户的同一个问题;) – PriteshJ 2012-07-18 18:26:07