我正在使用Rubyzip解压缩用户上传的文件。该文件包含一堆图像,这些图像被解压缩并放入文件夹中。这在mac上工作正常,但在Windows上它不会解压缩zip文件。这是我使用的模型:
require "zip/zip"
class Photo < ActiveRecord::Base
validates_presence_of :image_file_name, :message => "Er is geen bestand bijgevoegd!"
belongs_to :album
has_attached_file :image, :styles => {
:original => ["1024x1024>", :jpg],
:medium => "300x250#",
:thumb => "150x100#"
}, :url => "/uploads/photos/:id/:style.:extension"
def zip?
image.content_type == "application/zip"
end
def save_photo
if zip?
extract_zip
true
else
self.save
end
end
def extract_zip
Zip::ZipFile.foreach(image.queued_for_write[:original].path) do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
filename = entry.name
basename = File.basename(filename)
tempfile = Tempfile.new(basename)
tempfile.binmode
tempfile.write(entry.get_input_stream.read)
photo = Photo.create(:image => tempfile, :album_id => album_id)
end
end
end
因为它在mac上工作正常,我认为这是Windows拉链文件的方式。也许与标题结构或某事有关?非常感谢任何帮助!