Set compression level when generating a ZIP file using RubyZip

后端 未结 2 1615
半阙折子戏
半阙折子戏 2021-02-10 13:21

I have a Ruby program that zips a directory tree of XML files using the rubyzip gem. My problem is that the file is starting to be heavy and I would like to increase the compres

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 13:34

    Here is the code I created by looking at rubyzip internal.

    level = Zlib::BEST_COMPRESSION
    Zip::ZipOutputStream.open(zip_file) do |zip|
        Dir.glob("**/*") do |filename|
            entry = Zip::ZipEntry.new("", filename)
            entry.gather_fileinfo_from_srcpath(filename)
            zip.put_next_entry(entry, nil, nil, Zip::ZipEntry::DEFLATED, level)
            entry.get_input_stream { |is| IOExtras.copy_stream(zip, is) }
        end
    end
    

提交回复
热议问题