Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Create A Gzipped Tar Archive

Tags:

What's the best way to create a gzipped tar archive with Ruby?

I have a Rails app that needs to create a compressed archive in response to user actions. Ideally, it would be possible to write directly to a compressed file without needing to generate intermediate temp files first. The Ruby Zlib library appears to support direct gzip compression. How can I combine this with tar output?

A number of quasi-solutions appear to have been proposed and a lot of information appears to be out of date.

For example, the top Google search result for "ruby tar" gives this thread, which was started in 2007 with apparently no resolution.

Another high-ranking search result is this one describing ruby tar. It dates back to 2002, and the announcement doesn't exactly inspire confidence.

I've also seen various reports of shelling out to unix tar and the like.

So, I know there are a lot of ways to do this, but I'm really looking for a recommendation to the most reliable and convenient one from someone who has tried a few of the alternatives.

Any ideas?

like image 546
Rich Apodaca Avatar asked Jul 11 '09 06:07

Rich Apodaca


People also ask

How do I create an archive with tar?

To create an archive with tar, use the '-c' (“create”) option, and specify the name of the archive file to create with the '-f' option. It's common practice to use a name with a '. tar' extension, such as 'my-backup. tar'.


2 Answers

If you're running under unix, you could write the files out to disk, then run a system call to tar/gzip them.

`tar -czf #{file_name}.tar.gz #{file_name}`
like image 191
Scott Avatar answered Sep 20 '22 17:09

Scott


This Ruby Minitar project was updated in 2009 and seems like it would solve your problem

like image 42
Sam Saffron Avatar answered Sep 21 '22 17:09

Sam Saffron