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.
我正在尝试从HTTP GET请求读取zip文件。一种方法是首先将响应主体保存到物理文件,然后读取zip文件以读取zip中的文件。
Is there a way to read the files inside directly without having to save the zip file into a physical file first?
有没有办法直接读取里面的文件而不必先将zip文件保存到物理文件中?
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
2 个解决方案
#1
Looks like you're using rubyzip, which can't unzip from an in-memory buffer.
看起来你正在使用rubyzip,它无法从内存缓冲区中解压缩。
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.
您可能希望使用Chilkat的Ruby Zip Library,因为它支持zip数据的内存处理。它声称能够“创建或打开内存中的Zips”,尽管我自己没有使用过该库的经验。但是,Chilkat的图书馆不是免费的,所以这可能是一个考虑因素。不确定是否有免费的库具有此功能。
#2
One way might be to implement in-memory file, so that RubyZip can still play with your file without changing anything.
一种方法可能是实现内存中的文件,这样RubyZip仍然可以在不改变任何内容的情况下使用您的文件。
You sould take a look at this Ruby Hack
你应该看看这个Ruby Hack
#1
Looks like you're using rubyzip, which can't unzip from an in-memory buffer.
看起来你正在使用rubyzip,它无法从内存缓冲区中解压缩。
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.
您可能希望使用Chilkat的Ruby Zip Library,因为它支持zip数据的内存处理。它声称能够“创建或打开内存中的Zips”,尽管我自己没有使用过该库的经验。但是,Chilkat的图书馆不是免费的,所以这可能是一个考虑因素。不确定是否有免费的库具有此功能。
#2
One way might be to implement in-memory file, so that RubyZip can still play with your file without changing anything.
一种方法可能是实现内存中的文件,这样RubyZip仍然可以在不改变任何内容的情况下使用您的文件。
You sould take a look at this Ruby Hack
你应该看看这个Ruby Hack