I’m using a webservice that responds with XML compressed in zip format (not gzip). I know that Ruby automatically decompresses responses with gzip format, but there is no gzip format for the service that I’m using.
Here is the code I’m using to get responses for the service:
require 'net/http'
require 'uri'
require 'nokogiri'
xml = Nokogiri::XML::Builder.new do |xml|
xml.method {
xml.param1 'value1'
xml.param2 'value2'
}
end
url = URI.parse('http://url.to.webservice/')
request = Net::HTTP::Post.new(url.path)
request.content_type = 'text/xml'
request.body = xml.to_xml
response = Net::HTTP.new(url.host, url.port).start { |http| http.request(request) }
case response
when Net::HTTPSuccess, Net::HTTPRedirection
# Decompress Zip response
else
puts "Request error."
end
Is there a built in method to decompress Zip responses using Ruby?
Thanks!
You should be able to decompress the file on the fly using
zipruby, like so:This takes into account the fact that the zip file the web service sends may include several files. If the service is only sending a single file, you can go with a simpler solution like this: