Set compression level when generating a ZIP file using RubyZip
Asked Answered
P

2

5

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?

Phidias answered 18/3, 2010 at 16:9 Comment(0)
P
8

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
Phidias answered 27/4, 2010 at 15:17 Comment(0)
G
0

you'll probably get better traction by calling out to the 'zip' program or what not to do the zipping.

Gca answered 18/3, 2010 at 16:24 Comment(2)
This program is intended to run on many platforms. I don't want to force the user to specify its zipping program and how to use it. I would prefer a Ruby solution. ThanksPhidias
Diving into the source might be your only option then.Gca

© 2022 - 2024 — McMap. All rights reserved.