ArafatK ArafatK - 11 months ago 93
Ruby Question

Open URI downloading corrupt files

I am trying to download a .tar.gz file using Ruby. Upon download, the file is always corrupt in some way.

I am using this code to download the file:

require "open-uri"'img.tar.gz', 'wb') do |fo|
fo.write open('').read

Is there a way to fix this?


Change the file mode in the open call:

open('', "rb").read

It was opening the file in text mode, when you wanted binary mode.

You also needed to be using the proper URL to download a raw file from Github. In this case, the correct URL can be found by right-clicking the Raw link on the file's repo page (the original URL given), and that Raw URL is the one that contains the actual binary image that you're trying to download. Change the URL to this:, and the change I suggested at the top of the answer works just fine.