2011-03-21 116 views
4

我試圖建立一個與RubyGem路邊一個文件下載。 (請看This Question。)壓縮文件下載到實際的壓縮文件中的文件結構

我試圖下載一個壓縮文件,然後用類文件我想實際上使文件,這樣我可以雙擊它在Finder中(我在OS X)。我將如何去將這個「Curl'ed」正文轉換爲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 

我沒有收到任何錯誤,我也找不到任何文件。我嘗試過絕對路徑,沒有任何區別。

+1

你調用'perform',它身上轉移後加入'on_body'事件。如果您在調用'curl.perform'之前創建該事件,代碼是否工作? – 2011-03-21 20:24:51

+0

啊,當然我必須在'perform'之前調用'on_body'。我也不得不改變File.new參數'「w''到'」 a'' – maetthew 2011-03-21 20:31:36

+0

大,很高興固定它。我將其添加爲一個答案,以及讓別人也能找到解決辦法容易:) – 2011-03-21 20:33:33

回答

2

你打電話perform,其傳輸體內後加入on_body事件。如果您將事件聲明移至perform呼叫之前,這應該起作用。

4

我使用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.newFile.open沒有這個它並沒有奏效。 在結束運動curl.perfom也幫助了我。