zip factory

This commit is contained in:
xx 2014-04-18 05:18:15 +08:00
parent 5ee1c8e2f2
commit f30513217e
1 changed files with 17 additions and 13 deletions

View File

@ -15,9 +15,9 @@ class ZipdownController < ApplicationController
end end
send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile
rescue NameError, ActiveRecord::RecordNotFound => e #rescue NameError, ActiveRecord::RecordNotFound => e
logger.error "[ZipDown] ===> #{e}" #logger.error "[ZipDown] ===> #{e}"
@error = e #@error = e
end end
private private
@ -31,7 +31,7 @@ class ZipdownController < ApplicationController
user_zip_paths = homeattaches.map do |homeattach| user_zip_paths = homeattaches.map do |homeattach|
zip_homework_by_user homeattach zip_homework_by_user homeattach
end end
zipping bid.name, user_zip_paths, OUTPUT_FOLDER zipping "#{Time.now.to_i}_#{bid.name}.zip", user_zip_paths, OUTPUT_FOLDER
#@paths = homeworks_attach_path #@paths = homeworks_attach_path
#zipfile = ziping homeworks_attach_path #zipfile = ziping homeworks_attach_path
@ -50,22 +50,26 @@ class ZipdownController < ApplicationController
#length = attach.storage_path.length #length = attach.storage_path.length
homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1) homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1)
end end
zipping homeattach.user.name, user_attaches_paths, OUTPUT_FOLDER zipping "#{Time.now.to_i}_#{homeattach.user.name.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true
end end
def zipping(zip_name_refer, files_paths, output_path) def zipping(zip_name_refer, files_paths, output_path, is_attachment=false)
# 输入待打包的文件列表已经打包文件定位到ouput_path # 输入待打包的文件列表已经打包文件定位到ouput_path
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
#folder = SAVE_FOLDER
input_filename = files_paths input_filename = files_paths
zipfile_name = "#{output_path}/archive_#{Time.now.to_i}.zip"
rename_zipfile = zip_name_refer ||= "archive_#{Time.now.to_i}.zip"
zipfile_name = "#{output_path}/#{rename_zipfile}"
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filename.each do |filename| input_filename.each do |filename|
zipfile.add(ic.iconv(filename_to_real(zip_name_refer+"#{Time.now.to_s}") ), filename) rename_file = Time.now.to_i.to_s+ ic.iconv( (File.basename(filename)) ).to_s
rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment
zipfile.add(rename_file, filename)
end end
zipfile.get_output_stream('ReadMe') do |os| zipfile.get_output_stream('ReadMe') do |os|
os.write 'Homeworks' os.write 'Homeworks'