tags:

views:

100

answers:

2

I am trying read zip file from HTTP GET request. One way to do it is by saving the response body to a physical file first and then reading the zip file to read the files inside the zip.

Is there a way to read the files inside directly without having to save the zip file into a physical file first?

My current code:

Net::HTTP.start("clinicaltrials.gov") do |http|
  resp = http.get("/ct2/results/download?id=15002A")
  open("C:\\search_result.zip", "wb") do |file|
    file.write(resp.body)
  end
end

Zip::ZipFile.open("C:\\search_result.zip") do |zipfile|
  xml = zipfile.file.read("search_result.xml")
end
A: 

Looks like you're using rubyzip, which can't unzip from an in-memory buffer.

You might want to look at using Chilkat's Ruby Zip Library instead as it supports in-memory processing of zip data. It claims to be able to "Create or open in-memory Zips", though I have no experience with the library myself. Chilkat's library isn't free, however, so that may be a consideration. Not sure if there is a free library that has this feature.

Naaff
A: 

One way might be to implement in-memory file, so that RubyZip can still play with your file without changing anything.

You sould take a look at this Ruby Hack

Yoann Le Touche