diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index dd855bc74..57078a436 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -15,9 +15,9 @@ class ZipdownController < ApplicationController end send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile - rescue NameError, ActiveRecord::RecordNotFound => e - logger.error "[ZipDown] ===> #{e}" - @error = e + #rescue NameError, ActiveRecord::RecordNotFound => e + #logger.error "[ZipDown] ===> #{e}" + #@error = e end private @@ -31,7 +31,7 @@ class ZipdownController < ApplicationController user_zip_paths = homeattaches.map do |homeattach| zip_homework_by_user homeattach 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 #zipfile = ziping homeworks_attach_path @@ -50,24 +50,28 @@ class ZipdownController < ApplicationController #length = attach.storage_path.length homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1) 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 - def zipping(zip_name_refer, files_paths, output_path) + def zipping(zip_name_refer, files_paths, output_path, is_attachment=false) # 输入待打包的文件列表,已经打包文件定位到ouput_path ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') - #folder = SAVE_FOLDER 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)) - Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| - input_filename.each do |filename| - zipfile.add(ic.iconv(filename_to_real(zip_name_refer+"#{Time.now.to_s}") ), filename) - end - zipfile.get_output_stream('ReadMe') do |os| + Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| + input_filename.each do |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 + zipfile.get_output_stream('ReadMe') do |os| os.write 'Homeworks' end end