使用 rubyzip 将文件和嵌套目录添加到 zipoutputstream
我正在努力让 rubyzip 将目录附加到 zipoutputstream。 (我想要输出流,这样我就可以从 Rails 控制器发送它)。我的代码遵循以下示例:
http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/
修改为包含目录时在要添加的文件列表中我收到以下错误:
任何帮助将不胜感激。
更新
在尝试了多种解决方案后,我在 zipruby 上取得了最大的成功,它有一个干净的 api 和很好的例子: http://zipruby.rubyforge.org/。
I'm struggling with getting rubyzip to append directories to a zipoutputstream. (I want the output stream so I can send it from a rails controller). My code follows this example:
http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/
When modified to include directories in the list of files to add I get the following error:
Any help would be greatly appreciated.
UPDATE
After trying a number of solutions I had best success with zipruby which has a clean api and good examples: http://zipruby.rubyforge.org/.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OOOOuuuhh...你肯定想要 ZipPY。它是一个 Rails 插件,抽象了 rubyzip 中的大量复杂性,并允许您创建您正在谈论的内容,包括目录(据我记得)。
在这里:
http://github.com/toretore/zippy
直接从 zippy 站点:
编辑:根据用户评论进行修改:
嗯...使用 Zippy 的全部目标是让 ruby zip 的使用变得更加容易。
您可能想再看一下(或第一次)...
以下是使用目录创建目录的方法:
OOOOOuuuhh...you DEFINITELY want ZIPPY. It's a Rails plugin that abstracts a lot of the complexity in rubyzip, and lets you create what you're talking about, including directories (from what I recall).
Here you go:
http://github.com/toretore/zippy
And direct from the zippy site:
edit: Amending per user comment:
Hmm...the whole objective of using Zippy is to make it a whole lot easier to use ruby zip.
Ya might want to take a second (or first) look...
Here's how to make a directory with directories:
Zippy 将为此工作。可能有一种更酷的方法来做到这一点,但由于基本上没有文档,以下是我在 Rakefile 中使用 Zippy 递归复制目录的方法。这个 Rakefile 在 Rails 环境中使用,所以我将 gem 要求放入 Gemfile 中:
这就是 Rakefile
现在我可以像这样使用它:
它将生成
my.zip
,其中包含一个名为的内部目录'my' 包含所选文件和目录的副本。Zippy will work for this. There may be a more cool way to do this but since there are essentially no docs, here's what I came up with for recursively copying directories with Zippy in a Rakefile. This Rakefile is used in a Rails environment so I put gem requirements in my Gemfile:
And this is the Rakefile
Now I can use it like this:
and it will produce
my.zip
which contains an inner directory called 'my' with copies of selected files and directories.我能够让目录使用 原始文章。
我所要做的就是在调用 zos.put_next_entry 时添加目录。
例如:
我刚刚将 zos.put_next_entry('some-funny-name.jpg') 更改为
zos.put_next_entry('myfolder/some-funny-name.jpg')
,生成的 zip 文件有一个名为myfolder
的嵌套文件夹,其中包含这些文件。I was able to get directories working with the same
ZipOutputStream
used in the original article.All I had to do was add the directory when calling
zos.put_next_entry
.For example:
I just changed
zos.put_next_entry('some-funny-name.jpg')
tozos.put_next_entry('myfolder/some-funny-name.jpg')
, and the resulting zipfile had a nested folder calledmyfolder
that contained the files.