Download and write .tar.gz files without corruption.
- by arbales
I've tried numerous ways of downloading files, specifically .zip and .tar.gz, with Ruby and write them to the disk.
I've found that the file appears to be the same as the reference (in size), but the archives refuse to extract. What I'm attempting now is:
Thanks!
def download_request(url, filePath:path, progressIndicator:progressBar)
file = File.open(path, "w+")
begin
Net::HTTP.get_response URI.parse(url) do |response|
if response['Location']!=nil
puts 'Direct to: ' + response['Location']
return download_request(response['Location'], filePath:path, progressIndicator:progressBar)
end
# some stuff
response.read_body do |segment|
file.write(segment)
# some progress stuff.
end
end
ensure
file.close
end
end
download_request("http://github.com/jashkenas/coffee-script/tarball/master", filePath:"tarball.tar.gz", progressIndicator:nil)