Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip and Encrypt Files in memory Ruby/Rails

I was trying to zip a few files and password protect them. The catch here is I cannot save the file on the disk.

I tried using Rubyzip. But seems like it doesn't support file encryption.

I tried using Zipruby but it only allows Encrypting the files already on the disk. (I am not sure about this, but I could not find a way to do it in memory).

I want to Zip and Encrypt Files in memory in Ruby.

like image 855
Harshac Avatar asked Jul 29 '26 17:07

Harshac


1 Answers

Zipruby includes facilities to do this:

zipinmem = Zip::Archive.open_buffer(buf, Zip::CREATE) do |ar| #create zip
  ar.add_buffer('bar.txt', 'baz')
end
Zip::Archive.open_buffer(zipinmem) do |ar|
  ar.add_buffer('thing.txt', "We're modifying the archive in memory!")
end

See the documentation in the fifth section.

like image 101
Linuxios Avatar answered Aug 01 '26 08:08

Linuxios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!