如何使用 ruby​​zip 库获取压缩文件的内容? [英] How do I get a zipped file's content using the rubyzip library?

查看:32
本文介绍了如何使用 ruby​​zip 库获取压缩文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取上传的 zip 文件并将其内容存储在数据库中,每个文件一个条目.rubyzip 库几乎没有有用的文档.

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).

我正在使用 ruby​​zip 库,并且已经做到了这一点:

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

如何设置 ZipEntry 中的数据?我必须使用临时文件吗?

How can I set the data from a ZipEntry? Do I have to use a temp file?

推荐答案

找到了更简单的方法:

asset.data = entry.get_input_stream.read

这篇关于如何使用 ruby​​zip 库获取压缩文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆