2016-08-01 91 views
2

我想我的ocaml應用程序直接下載,解壓縮(gzip),然後逐行處理生成的文本文件,而不使用臨時文件和外部程序。如何下載,解壓縮和處理OCaml中的gzip文件?

我看着庫cohttpocurlcamlzip。不幸的是,我發現沒有很好的方法讓他們一起工作。

OCaml實現這一目標的方式是什麼?

+0

我在OPAM回購中找不到這樣的庫。現在可能沒有。 – camlspotter

+0

臨時文件有什麼問題? – PatJ

+0

我只是希望有像其他許多語言一樣的簡單直接的方式,例如:new GzipInputStream(url.openStream())。foreach(println) – gruenewa

回答

4

您可以ocurl並通過管道和線程camlzip一起工作。概念驗證:

#use "topfind";; 
#thread;; 
#require "unix";; 
#require "curl";; 
#require "zip";; 

let() = Curl.(global_init CURLINIT_GLOBALALL) 

let download url oc = 
    let open Curl in 
    let h = init() in 
    setopt h (CURLOPT_URL url); 
    setopt h (CURLOPT_WRITEFUNCTION (fun x -> output_string oc x; String.length x)); 
    perform h; 
    cleanup h 

let read_line really_input = 
    let buf = Buffer.create 256 in 
    try 
    while true do 
     let x = " " in 
     let() = really_input x 0 1 in 
     if x = "\n" then raise Exit else Buffer.add_string buf x; 
    done; 
    assert false 
    with 
    | Exit -> Buffer.contents buf 
    | End_of_file -> if Buffer.length buf = 0 then raise End_of_file else Buffer.contents buf 

let curl_gzip_iter f url = 
    let ic, oc = Unix.pipe() in 
    let ic = Unix.in_channel_of_descr ic and oc = Unix.out_channel_of_descr oc in 
    let t = Thread.create (fun() -> download url oc; close_out oc)() in 
    let zic = Gzip.open_in_chan ic in 
    let zii = Gzip.really_input zic in 
    let() = 
    try 
     while true do 
     let() = f (read_line zii) in() 
     done; 
     assert false 
    with 
    | End_of_file ->() 
    in 
    Gzip.close_in zic; 
    Thread.join t 

let() = curl_gzip_iter print_endline "file:///tmp/toto.gz" 

但是,當人們必須處理錯誤時,這會變得很痛苦。