Download and write .tar.gz files without corruption

前端 未结 4 1775
一整个雨季
一整个雨季 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:13

    When downloading a .tar.gz with open-uri via a simple open() call, I was also getting errors uncompressing the file on disk. I eventually noticed that the file size was much larger than expected.

    Inspecting the file download.tar.gz on disk, what it actually contained was download.tar uncompressed; and that could be untarred. This seems to be due to an implicit Accept-encoding: gzip header on the open() call which makes sense for web content, but is not what I wanted when retrieving a gzipped tarball. I was able to work around it and defeat that behavior by sending a blank Accept-encoding header in the optional hash argument to the remote open():

    open('/local/path/to/download.tar.gz', 'wb') do |file|
      # Send a blank Accept-encoding header
      file.write open('https://example.com/remote.tar.gz', {'Accept-encoding'=>''}).read
    end
    

提交回复
热议问题