【问题标题】:Zipfile download through CURL to an actual Zip-file in your file structure通过 CURL 将 Zipfile 下载到文件结构中的实际 Zip 文件
【发布时间】:2011-03-21 20:19:32
【问题描述】:

我正在尝试使用 RubyGem Curb 构建文件下载器。 (看This Question。)

我正在尝试下载一个 zip 文件,然后使用类 File 我正在尝试实际制作该文件,以便我可以在 Finder 中双击它(我在 OS X 上)。我将如何将这个“卷曲”的主体转换为 zip 文件。

require 'rubygems'
require 'curb'

class Download
  def start
    curl = Curl::Easy.new('http://wordpress.org/latest.zip')
    curl.perform
    curl.on_body {
      |d| f = File.new('test.zip', 'w') {|f| f.write d}
    }
  end
end

dl = Download.new
dl.start

我没有收到任何错误,也找不到任何文件。我已经尝试过没有区别的绝对路径。

【问题讨论】:

  • 您在调用 perform 之后添加了 on_body 事件,这会传输正文。如果您在调用 curl.perform 之前创建该事件,代码是否有效?
  • 啊,我当然得在perform 之前打电话给on_body。我还必须将 File.new 参数 'w' 更改为 'a'
  • 太好了,很高兴修复了它。我也会将此添加为答案,以便其他人可以轻松找到解决方案:)
  • @maetthew,你能把答案打勾吗?
  • @Tim Snowwhite @Hans Engel 我正要请你把它作为答案发布,真好,你打败了我 :) 检查了它作为答案

标签: ruby file curl curb


【解决方案1】:

我正在使用 ruby​​ 2.0

我的代码是:

curl = Curl::Easy.new('http://somepage.cz/index.html')
curl.on_body do |d|
  f = File.open('output_file.html', 'w') {|f| f.write(d)}
end
curl.perform

我不得不将 File.new 更改为 File.open 没有这个它没有工作。 最后移动 curl.perfom 确实对我有帮助。

【讨论】:

    【解决方案2】:

    您在调用 perform 之后添加了 on_body 事件,这会传输正文。如果您将事件声明移到 perform 调用之前,这应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多