读取 zip 存档中的文件,无需解压存档 [英] Reading files in a zip archive, without unzipping the archive

查看:52
本文介绍了读取 zip 存档中的文件,无需解压存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 多个 zip 文件的目录,我需要读取 zip 文件中的文件以进行一些数据处理,而无需解压缩存档.

I have a directory with 100+ zip files and I need to read the files inside the zip files to do some data processing, without unzipping the archive.

是否有一个 Ruby 库可以在不解压文件的情况下读取 zip 存档中文件的内容?

Is there a Ruby library to read the contents of files in zip archives, without unzipping the file?

使用 ruby​​zip 会出错:

Using rubyzip gives an error:

require 'zip'

Zip::File.open('my_zip.zip') do |zip_file|
  # Handle entries one by one
  zip_file.each do |entry|
    # Extract to file/directory/symlink
    puts "Extracting #{entry.name}"
    entry.extract('here')

    # Read into memory
    content = entry.get_input_stream.read
  end
end 

出现此错误:

test.rb:12:in `block (2 levels) in <main>': undefined method `read' for Zip::NullInputStream:Module (NoMethodError)
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/entry_set.rb:42:in `call'
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/entry_set.rb:42:in `block in each'
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/entry_set.rb:41:in `each'
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/entry_set.rb:41:in `each'
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/central_directory.rb:182:in `each'
    from test.rb:6:in `block in <main>'
    from .gem/ruby/gems/rubyzip-1.1.6/lib/zip/file.rb:99:in `open'
    from test.rb:4:in `<main>'

推荐答案

如果条目是目录而不是文件,则返回 Zip::NullInputStream,可能是这种情况吗?

The Zip::NullInputStream is returned if the entry is a directory and not a file, could that be the case?

这是代码的更强大的变体:

Here's a more robust variation of the code:

#!/usr/bin/env ruby

require 'rubygems'
require 'zip'


Zip::File.open('my_zip.zip') do |zip_file|
  # Handle entries one by one
  zip_file.each do |entry|
    if entry.directory?
      puts "#{entry.name} is a folder!"
    elsif entry.symlink?
      puts "#{entry.name} is a symlink!"
    elsif entry.file?
      puts "#{entry.name} is a regular file!"

      # Read into memory
      content = entry.get_input_stream.read

      # Output
      puts content
    else
      puts "#{entry.name} is something unknown, oops!"
    end
  end
end

这篇关于读取 zip 存档中的文件,无需解压存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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