使用RubyZip生成ZIP文件时设置压缩级别 [英] Set compression level when generating a ZIP file using RubyZip

查看:366
本文介绍了使用RubyZip生成ZIP文件时设置压缩级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby程序,使用 ruby​​zip gem来压缩XML文件的目录树。我的问题是,该文件开始很重,我想提高压缩级别,因为压缩时间不是一个问题。

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.

我在 ruby​​zip文档一种指定所创建ZIP文件的压缩级别的方法。

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

任何人都知道如何更改此设置?是否有另一个Ruby库允许指定压缩级别?

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

推荐答案

这里是通过rubyzip内部创建的代码。 / p>

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

这篇关于使用RubyZip生成ZIP文件时设置压缩级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆