Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download and write .tar.gz files without corruption

How do you download files, specifically .zip and .tar.gz, with Ruby and write them to the disk?


This question was originally specific to a bug in MacRuby, but the answers are relevant to the above general question.

Using MacRuby, 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 at: https://gist.github.com/arbales/8203385
Thanks!

like image 888
arbales Avatar asked Dec 08 '22 03:12

arbales


1 Answers

I've successfully downloaded and extracted GZip files with this code:

require 'open-uri'
require 'zlib'

open('tarball.tar', 'w') do |local_file|
  open('http://github.com/jashkenas/coffee-script/tarball/master/tarball.tar.gz') do |remote_file|
    local_file.write(Zlib::GzipReader.new(remote_file).read)
  end
end
like image 93
Theo Avatar answered Dec 18 '22 09:12

Theo