1.文件下载

io = File.open('public/code/tlt.txt')
     io.binmode
     send_data(io.read,:filename => 'code',:disposition => 'attachment')
     io.close

 2.文件新建,并输出

f=File.new(File.join('public/code/section_'+params[:section], 'text_course_'+num+'.json'), "w+")
      f << 'dddd'
      f.close

 3.文件打包多个文件

gem 'rubyzip', '< 1.0.0'
  require 'zip/zip'
    require 'zip/zipfilesystem'
    path = 'public/code'
    path.sub!(%r[/$],'')
    archive = File.join(path,File.basename(path))+'.zip'
    FileUtils.rm archive, :force=>true

    Zip::ZipFile.open(archive, 'w') do |zipfile|
      Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file|
        zipfile.add(file.sub(path+'/',''),file)
      end
    end

 4.打包单个文件

require 'zlib'
    
    file = 'public/code.gz'
    Zlib::GzipWriter.open(file) do |gzip|
      gzip.write  'this is a test'
      gzip.close
    end
    
    open(file, 'rb') { |f| f.read(10) }
    
    Zlib::GzipReader.open(file) { |gzip| puts gzip.read }