문제

 require 'active_support/core_ext'
 require 'open-uri'
 require 'zip/zip'


 zipfilename = open(url which returns a zip file with no of xml files inside)
 Zip::ZipFile.open(zipfilename) do |zipfile|
   zipfile.each do |entry|  
    xml = File.open(entry).read
    xml_to_hash = Hash.from_xml(xml)
   end
end

when i print try to print variable entry, it comes out as file_name.xml . Error comes from xml = File.open(entry).read.

Error:

test.rb:51:in `initialize': can't convert Zip::ZipEntry into String (TypeError)
from test.rb:51:in `open'
from test.rb:51:in `block (2 levels) in <main>'
도움이 되었습니까?

해결책

the entry you are iterating over is not a real file. it just represents a file in your archive. i think that you need to transform your entry wich is of type Zip::ZipEntry into something that can be read.

see the example at http://rubyzip.sourceforge.net/classes/Zip/ZipFile.html

as far as i have seen you can get an io like object by calling get_input_stream or just call read: http://rubyzip.sourceforge.net/classes/Zip/ZipEntry.html#M000135

다른 팁

instead of

xml = File.open(entry).read

try

xml = zipfile.read(entry)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top