基本的归档文件创建工作如下(使用rubyzip)
Zip::File.open(zipfile_name, create: true) do |zipfile|
input_filenames.each do |filename|
zipfile.add(filename, File.join(folder, filename))
end
end
好吧,我有一段时间了......但现在我需要创建一个密码保护的zip。为此,文档包括:
enc = Zip::TraditionalEncrypter.new('password')
buffer = Zip::OutputStream.write_buffer(encrypter: enc) do |output|
output.put_next_entry("my_file.txt")
output.write my_data
end
我不太明白如何将合并与第一种方法(迭代文件列表)结合起来。
密码也是可选的,在我的情况下-这是一个用户的选择是否存档将密码保护或没有。我想避免在每种情况下使用完全不同的代码。
1条答案
按热度按时间xdnvmnnf1#
可以看到,默认情况下,编译器为
nil
但是如果需要的话,您可以动态地传递它
我建议这样的方法:
当然,您可以修改它或创建单独的实体为您的需要
现在你可以调用这个方法,传递字符串或文件名数组,传递/不传递密码
正如我所看到的,这是rubyzip 3.0的新特性,你需要指定这个版本(例如在
Gemfile
中)并在该版本的上下文中运行脚本