Rails 有人用 rubyzip 解压过 zip 文件嚒

suyuhang · 2014年05月20日 · 最后由 suyuhang 回复于 2014年05月21日 · 4761 次阅读

有人用过 rubyzip 解压 zip 文件,我想看看代码,上网看的不太明白,是我理解有问题,所以麻烦各位了

用过,它的 API 确实是比较难理解,需要用 Stream 的方式去理解。

最简单的用法是这样:

http://qiita.com/chitsaou/items/7e059f1ebeedcbcd5703

Requires Rubyzip 1.1.0+

Given a data of Zip file that is read from an IO, extract that data into a Hash.

def unzip(data)
  fin = StringIO.new(data)

  entries = {}

  ::Zip::InputStream.open(fin) do |fzip|
    while entry = fzip.get_next_entry
      entries[entry.name] = fzip.read
    end
  end

  entries
end

Example:

entries = unzip(open("http://example.com/archive.zip").read)
entries["file.txt"] #=> "Hello World\n" (File Content)

#1 楼 @chitsaou 我看了好久 api 也不行,大哥您这样的方法 我是不是得在服务器上安装一下才行阿

#1 楼 @chitsaou 我现在写了一个 Zip::File.open("#{Rails.root}/public/error.log") do |zip_file| zip_file.each do |entry| puts "Extracting============#{entry.name}" @content = entry.get_input_stream.read end entry = zip_file.glob('*.txt').first puts entry.get_input_stream.read end

我打印这个得到

{"app_version":"0.0.4","systemModel":"iPad","systemVersion":"7.1.1","app_id":"aizhuizhui","systemType":"iPhone OS","channel":"App Store"} e_bp_6598995_2014-05-09 19:44 e:empty 的日志类型 f:feedback 的日志类型 c:comic_id s:section_id p:picture_id bp:有问题的 picture l:landscape 横屏 p:portrait 竖屏 我现在想把这些东西 弄成一个数组,然后方便放到数据库中。

我尝试了好几次都不行。

https://github.com/ucooling/TestCenter/blob/master/app/controllers/project_controller.rb 这个是我以前做的一个东西,用到过,你可以参考一下。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号