我正在使用 RubyZip 尝试压缩目录中的所有文件。这是我的课:
require 'zip/zip'
require 'zip/zipfilesystem'
class Lesson < ActiveRecord::Base
attr_accessible :attachment, :course_id, :goal, :title, :assets_attributes
belongs_to :course
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :reject_if => lambda { |a| a[:asset_file_name].blank? }
def bundle
bundle_filename = "public/attachments/#{self.id}/#{self.id}.zip"
Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
|zipfile|
Dir.foreach("public/attachments/#{self.id}") do |item|
zipfile.add( item, "public/attachments/")
end
}
File.chmod(0644, bundle_filename)
self.save
end
end
目前,这是使用我想要的名称创建一个 zipfile,但目录中的文件没有被正确压缩。它们不是作为文件压缩,而是作为目录压缩。