Ruby:创建 Gzip 压缩的 Tar 存档
使用 Ruby 创建 gzip 压缩的 tar 存档的最佳方法是什么?
我有一个 Rails 应用程序,需要创建一个压缩存档以响应用户操作。 理想情况下,可以直接写入压缩文件,而无需先生成中间临时文件。 Ruby Zlib 库似乎支持直接 gzip压缩。 如何将其与 tar 输出结合起来?
似乎已经提出了许多准解决方案,并且许多信息似乎已经过时。
例如,“ruby tar”的顶部 Google 搜索结果给出 这个帖子,于 2007 年开始,显然没有解决方案。
另一个排名较高的搜索结果是 这个描述 ruby 的结果焦油。 它可以追溯到 2002 年,这一公告并没有完全激发人们的信心。
我还看到过各种关于 shell 到 unix tar 等的报告。
所以,我知道有很多方法可以做到这一点,但我真的在寻找尝试过几种替代方法的人推荐最可靠和最方便的方法。
有任何想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 UNIX 下运行,您可以将文件写入磁盘,然后运行系统调用来 tar/gzip 它们。
If you're running under unix, you could write the files out to disk, then run a system call to tar/gzip them.
这个 Ruby Minitar 项目于 2009 年更新,似乎可以解决您的问题
This Ruby Minitar project was updated in 2009 and seems like it would solve your problem
以下示例演示如何组合 Ruby Zlib 中的 GzipWriter 类和 RubyGems 中的 TarWriter 类来创建 gzip 压缩的 tar 存档:
The following example shows how to combine the GzipWriter class from Ruby Zlib and the TarWriter class from RubyGems to create a gzipped tar archive: