Basic archive creation works like this (using rubyzip)
Zip::File.open(zipfile_name, create: true) do |zipfile|
input_filenames.each do |filename|
zipfile.add(filename, File.join(folder, filename))
end
end
Okay I got that to work a while back... But now I need to create a password-protected zip. For this the documentation has:
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
Which I don't quite understand how to combine with the first approach (iterating over a list of files).
Also password is optional in my case - it's a user choice whether the archive will be password protected or not. I would like to avoid having to use a completely different code in each case.
As you can see by default encrypter is
nilBut you can pass it dynamically if needed
I suggest such method:
Of course you can modify it or create separate entity for you needs
Now you can call this method passing string or array of filenames, passing/not passing password
As I see this is pretty new feature of rubyzip 3.0 and you need to specify this version (for example in the
Gemfile)And run your script in the context of that version, let's say