Download and write .tar.gz files without corruption

前端 未结 4 1772
一整个雨季
一整个雨季 2021-02-10 13:33

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, b

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 14:12

    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
    

提交回复
热议问题