通过Ruby Net :: Http下载文件大小

时间:2014-07-30 12:41:38

标签: ruby file http download

我正在尝试使用Net :: Http模块通过Http下载文件。该 我试图下载的文件是 http://public.dhe.ibm.com/ibmdl/export/pub/softwar ... 它的大小为25M。

当尝试使用下面的代码通过Ruby下载时,我在Ruby中看到了这一点 -2.1.2p95尺寸大约为85M。现在当试图解开时 这个存档使用zlib utils [Gem :: Package :: TarReader.new( Zlib :: GzipReader.open(archive))做| tar |它没有抱怨它是 不是gzip格式。

但是当我在Ruby-1.9.3p429中尝试相同的代码时,它工作得很好而且我是 能够看到成功下载和解压缩的25M文件。

知道出了什么问题吗?我错过了什么吗?

下载文件的代码:

def downloadCLIPackage(destination)

  uri =
URI.parse("http://public.dhe.ibm.com/ibmdl/export/pub/softwar...)
  filename = "/home/praveend/clidriver.tar.gz"

  http_object = Net::HTTP.new(uri.host, uri.port)
  http_object.use_ssl = true if uri.scheme == 'https'
  response = Net::HTTP.get_response(uri)
  f = open(filename, 'wb')
  f.write(response.body)
  f.close()

  filename

end

由于

普利文

1 个答案:

答案 0 :(得分:0)

这是因为,如果发现目标是gzip格式,Ruby-2.1.2中的Net :: HTTP会自动解压缩文件。

为了避免自动解压缩......需要将标头Accept-encoding设置为identity。

从7stud回答我在Ruby论坛上提出的同一问题:https://www.ruby-forum.com/topic/5342548#1153921

由于

普利文