Download and write .tar.gz files without corruption.

Posted by arbales on Stack Overflow See other posts from Stack Overflow or by arbales
Published on 2010-04-19T20:33:56Z Indexed on 2010/04/20 16:03 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

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)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about download