简体   繁体   中英

Trying to read files in a zip archive without extracting them

I've been trying to read the contents of a zipped file for the sake of data comparison, similar to the person from this thread: Reading files in a zip archive, without unzipping the archive

I tried the accepted code there exactly, but I'm still getting the error /home/fikayo/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rubyzip-1.2.0/lib/zip/file.rb:73:in `size?': no implicit conversion of Zip::Entry into String (TypeError)

For reference, here is my code:

 require 'rubygems'
 require 'zip'

      def read_file
        Zip::File.open(myZip) do |zip_file|
          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 "No sell"
            end
          end
        end
      end

myZip is the variable that I stored the zip file inside. I checked to make sure and its type is listed as Zip::Entry

According to the rubyzip documentation (and the question you linked) mZip should be of class String and contain the path to a file rather than File or Zip::Entry .

mZip = './folder/file.zip'
def read_file
  Zip::File.open(myZip) do |zip_file|
  #...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM