通过 CURL 将 Zip 文件下载到文件结构中的实际 Zip 文件
我正在尝试使用 RubyGem Curb 构建一个文件下载器。 (查看这个问题。)
我正在尝试下载一个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
我没有收到任何错误,也找不到任何文件。我试过绝对路径没有区别。
I am trying to build a file downloader with the RubyGem Curb. (Look at This Question.)
I am trying to download a zip file and then with the class File I am trying to actually make the file so that I can double click it in Finder (I am on OS X). How would I go about to convert this "Curl'ed" body to a zip-file.
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
I am not getting any error, neither can I find any file. I have tried absolute paths with no difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在使用 ruby 2.0
,我的代码是:
我必须将
File.new
更改为File.open
没有这个它不起作用。将
curl.perfom
移到最后确实对我有帮助。I am using ruby 2.0
and my code is:
I had to change
File.new
toFile.open
without this it didn't worked.Moving
curl.perfom
on the end does helped me.您将在调用传输正文的
perform
之后添加on_body
事件。如果将事件声明移至perform
调用之前,这应该可以工作。You're adding the
on_body
event after callingperform
, which transfers the body. If you move the event declaration to before theperform
call, this should work.