如何使用 rubyzip 库获取压缩文件的内容?
我正在尝试提取上传的 zip 文件并将其内容存储在数据库中,每个文件一个条目。 rubyzip 库几乎没有有用的文档。
有一个资产表,其中包含键:string(文件名)和数据:binary(文件内容)。
我正在使用 rubyzip 库,并且已经做到了这一点:
Zip::ZipFile.open(@file_data.local_path) do |zipfile|
zipfile.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
asset = self.assets.build
asset.key = entry.name
asset.data = ?? # what goes here?
end
end
How can I set the data from a ZipEntry? 我必须使用临时文件吗?
I'm trying to extract an uploaded zip file and store its contents in the database, one entry per file. The rubyzip library has nearly no useful documentation.
There is an assets table that has key :string (file name) and data :binary (file contents).
I'm using the rubyzip library, and have made it as far as this:
Zip::ZipFile.open(@file_data.local_path) do |zipfile|
zipfile.each do |entry|
next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
asset = self.assets.build
asset.key = entry.name
asset.data = ?? # what goes here?
end
end
How can I set the data from a ZipEntry? Do I have to use a temp file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发现了一个更简单的方法:
Found an even more simple way:
看来您可以使用这样的 read_local_entry 方法:
或者,您可以使用此方法保存条目:
我不确定这些方法将如何工作,但也许它们会帮助您找到正确的方法(如果这是不是吧)。
并且,还有一种选择:
It would seem that you can either use the read_local_entry method like this:
Or, you could save the entry with this method:
I'm not sure how those will work, but maybe they'll help you find the right method (if this ain't it).
And, one more alternative: