如何使用 rubyzip 解压压缩文件夹
我知道如何使用 rubyzip 检索普通 zip 文件的内容。但我在解压缩压缩文件夹的内容时遇到了麻烦,我希望你们中的任何人都可以帮助我。
这是我用来解压缩的代码:
Zip::ZipFile::open(@file_location) do |zip|
zip.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
logger.debug "#{entry.name}"
@data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
end
end
entry.name 为我提供了 zip 文件内文件的名称。这与普通的 zip 文件完美配合。但是,当从文件夹创建 zip 文件时,条目的名称类似于:test-folder/test.pdf。当我尝试创建该文件时,它告诉我找不到该文件。这可能是因为它位于 zip 内的“test”文件夹内。
如果我检查条目是文件夹,则找不到文件夹。所以我认为解决方案是将条目作为流读取,然后将其保存为文件。获取入口流很容易,但是如何将其保存为文件呢?这就是我到目前为止所得到的。
Zip::ZipFile::open(@file_location) do |zip|
zip.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
logger.debug "#{entry.name}"
@data = entry.get_input_stream.read
# How do i create a file from a stream?
end
end
基本上我的问题是:如何从流创建文件?或者有比我更简单的方法吗?
===编辑=== 我使用回形针来存储文件。
I know how to retrieve the contents of a normal zip-file with rubyzip. But i got trouble unzipping the contents of a zipped folder and i hope any of u guys can help me out.
this is the code i use to unzip:
Zip::ZipFile::open(@file_location) do |zip|
zip.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
logger.debug "#{entry.name}"
@data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
end
end
entry.name gives me the name of the file inside the zip-file. This works perfectly with a normal zipfile. But when the zipfile is created from a folder, then the name of the entries are something like: test-folder/test.pdf. When i then try to create the file, it tells me the file can not be found. This is probably because it is inside the "test"-folder that is inside the zip.
If i check the entry to be a folder, no folder can be found. So i thought the solution to be to read the entry as a stream and then save it as a file. It is easy to get the entry-stream, but how do I save it as a file? This is what i got so far.
Zip::ZipFile::open(@file_location) do |zip|
zip.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
logger.debug "#{entry.name}"
@data = entry.get_input_stream.read
# How do i create a file from a stream?
end
end
Basically my question is: how can i create a file from a stream? Or is there an easier approach to this than mine?
===EDIT===
I use paperclip to store the files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现基于 jhwist 的更简单的方法工作正常:
条件显然是可选的,但如果没有它,如果代码尝试覆盖现有文件,则会引发错误。
I found that a simpler approach based on jhwist's worked ok:
The conditional is obviously optional, but without it the code will raise an error if it tries to overwrite an existing file.
我认为你的问题不在于你是否需要从流中写入文件。基本上,如果你调用
File.new
它将会创建一个新的 IO-Stream(File
是IO
的子类)。因此,无论您想对 zip 文件中的流执行什么操作,也应该对常规文件进行操作。当你说
我认为发生的情况是您要创建的文件的父目录不存在(在您的情况下
test-folder< /代码>)。你想做的是类似的事情(未经测试):
I think your problem is not whether you need to write a file from a stream or not. Basically, if you call
File.new
it will create a new IO-Stream (File
is a subclass ofIO
). Therefore whatever you want to do with the stream from the zipfile should also work with a regular file.When you say
I think what happens is that the parent-directory for the file you want to create does not exist (in your case the
test-folder
). What you want to do is something like that (not tested):我通过使用流并创建 StringIO 解决了这个问题。这是代码
I solved it by using a stream and creating a StringIO. Here is the code