简体   繁体   中英

Set compression level when generating a ZIP file using RubyZip

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 compression level, since compression time is not an issue.

I could not find in the rubyzip documentation a way to specify the compression level for the created ZIP file.

Anyone know how to change this setting? Is there another Ruby library that allows to specify compression level?

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

你可能会通过调用'zip'程序或不做拉链的方式获得更好的牵引力。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM