def read_xml_from_zip(target, zipfile)
Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exist?(zipfile)}"
if Oddb2xml.skip_download?
entry = nil
Dir.glob(File.join(DOWNLOADS, "*")).each do |name|
if target.match(name)
entry = name
break
end
end
if entry
dest = "#{DOWNLOADS}/#{File.basename(entry)}"
@file2save = dest
if File.exist?(dest)
Oddb2xml.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
return IO.read(dest)
else
Oddb2xml.log "read_xml_from_zip could not read #{dest}"
end
else
Oddb2xml.log "read_xml_from_zip could not find #{target}"
end
end
xml = ""
if RUBY_PLATFORM.match?(/mswin|mingw|bccwin|cygwin/i)
Zip::File.open(zipfile) do |a_zip_file|
a_zip_file.each do |entry|
if entry.name&.match?(target)
Oddb2xml.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
io = entry.get_input_stream
until io.eof?
bytes = io.read(1024)
xml << bytes
bytes = nil
end
io.close if io.respond_to?(:close)
dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
File.open(dest, "w+") { |f| f.write xml }
Oddb2xml.log "read_xml_from_zip saved as #{dest}"
end
end
end
else
Zip::File.foreach(zipfile) do |entry|
if entry.name&.match?(target)
Oddb2xml.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
entry.get_input_stream { |io| xml = io.read }
File.open(dest, "w+") { |f| f.write xml }
Oddb2xml.log "read_xml_from_zip saved as #{dest}"
end
end
end
xml
end