我正在尝试使用 RubyZip 压缩目录中包含的所有文件。这是我所拥有的:
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 文件,但它没有添加该目录中包含的所有文件。有任何想法吗?