class TestController < ApplicationController def zip homeworks_attach_path = [] homework_id = params[:homework_id] bid = Bid.find_by_id(homework_id) bid.homeworks.each do |homeattach| homeattach.attachments.each do |attach| length = attach.storage_path.length homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1) end end @paths = homeworks_attach_path zipfile = ziping homeworks_attach_path send_file zipfile, :filename => bid.name, :type => detect_content_type(zipfile) end def courselist @courses = Project.course_entities end def ziping files_path folder = "#{Rails.root}/files" input_filename = files_path zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip" Rails.logger.info "================================================================================================================================================================================================================================================================================================================================================================================================#{zipfile_name}" Dir.mkdir(File.dirname(zipfile_name)) if File.exist?(File.dirname(zipfile_name)) Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| input_filename.each do |filename| zipfile.add(File.basename(filename), folder + '/' + filename) end zipfile.get_output_stream("ReadMe"){ |os| os.write "Homeworks" } end zipfile_name end def detect_content_type(name) content_type = Redmine::MimeType.of(name) content_type.to_s end end