Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zip archive using Ruby on Rails?

I have a bunch of files in a directory. I want to zip some of them together into a zip archive.

I can see there are various solutions for doing so. I could use a gem such as rubyzip or run exec and just use a command line tool.

This is going to impact an important part of the system I'm working on so I'd love to have some feedback and/or directions on creating zip files with Rails.

like image 784
marcgg Avatar asked Dec 17 '09 14:12

marcgg


People also ask

How do I create an archive zip file?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

What is Rubyzip?

rubyzip is a ruby module for reading and writing zip files.


2 Answers

rubyzip is a good choice. I Have used it to zip attachments in private messaging in a social app that i worked on before. However, if the files you are zipping are of big sizes, then you are advised to do some background processing using delayed_job for example.

like image 105
khelll Avatar answered Nov 03 '22 01:11

khelll


If you are only zipping the files and not doing anything else then I would suggest using exec or system or %x to do the zipping because installing and using a gem for such a minor task doesn't make much sense.

However, if you want to do more than just zipping some directory and utilize other functionality that the gem offers then the obvious choice would be to use the rubyzip gem.

like image 5
nas Avatar answered Nov 03 '22 01:11

nas