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?
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'.
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}`
This Ruby Minitar project was updated in 2009 and seems like it would solve your problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With