3

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.

2
  • ok. so what is the question? Commented Mar 6, 2013 at 15:14
  • I want to Zip and Encrypt Files in memory in Ruby. Commented Mar 6, 2013 at 15:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

But how do I encrypt this buffer without saving ? As the files are encrypted in 4th Section in the same documentation.
@Harshac: in one of the zipruby blocks, call ar.encrypt('password').
It didn't encrypt, but didn't even raise NoMethodError !
Yes. I'm on ruby 1.8.7 & rails 3.0
From the documentation it seems like this is possible; however, in reality it does not work. When working with archive objects from "open_buffer" the "encrypt" method returns true but, in fact, does not encrypt the ZIP archive.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.