Download and write .tar.gz files without corruption

How do you download files, in particular .zip and .tar.gz, from Ruby and write them to disk?

-
This question was initially error-specific in MacRuby, but the answers are related to the above general question.

Using MacRuby, I found that the file looks the same as the link (in size), but the archives refuse to extract. Now I'm trying: https://gist.github.com/arbales/8203385
Thanks!

+4
source share
3 answers

I successfully downloaded and extracted the GZip files using 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 
+11
source

I would recommend using open-uri in ruby ​​stdlib.

 require 'open-uri' open(out_file, 'w') do |out| out.write(open(url).read) end 

http://ruby-doc.org/stdlib/libdoc/open-uri/rdoc/classes/OpenURI/OpenRead.html#M000832

Make sure you look at the option: progress_proc to open, as it seems to you that you want to make progress.

+3
source

The last time I received updated files from Ruby, I forgot to call file.binmode right after File.open . I spent several hours to find out what happened. Does this help with your problem?

+1
source

Source: https://habr.com/ru/post/1307388/


All Articles