2017-07-18 23 views
1

我一直在試圖讀取壓縮文件的內容進行數據比較,類似於從這個線程的人着想:Reading files in a zip archive, without unzipping the archive嘗試讀取zip壓縮包文件,而無需提取他們

我試過接受有確切的代碼,但我仍然得到錯誤 /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)

僅供參考,這裏是我的代碼:

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是我存儲於zip文件中的變量側。我檢查,以確保其類型顯示爲Zip::Entry

回答

2

按照rubyzip documentation(和你鏈接的問題)mZipString類和包含一個文件而不是FileZip::Entry路徑。

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