From 9e2208be9eaf40ff0d9c9c00d0a388bf930f7a27 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Wed, 12 Mar 2003 18:03:46 +0000 Subject: [PATCH] added "output" parameter to gzip and gunzip used to specify target file for compression and decompression --- helm/http_getter/http_getter_misc.ml | 33 +++++++++++++++++++-------- helm/http_getter/http_getter_misc.mli | 12 ++++++---- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/helm/http_getter/http_getter_misc.ml b/helm/http_getter/http_getter_misc.ml index 4b82dd8e7..70bd814ce 100644 --- a/helm/http_getter/http_getter_misc.ml +++ b/helm/http_getter/http_getter_misc.ml @@ -133,9 +133,10 @@ let wget ?output url = | scheme -> (* unsupported scheme *) failwith ("Http_getter_misc.wget: unsupported scheme: " ^ scheme) -let gzip ?(keep = false) fname = - debug_print (sprintf "gzipping %s (keep: %b)" fname keep); - let (ic, oc) = (open_in fname, Gzip.open_out (fname ^ ".gz")) in +let gzip ?(keep = false) ?output fname = + let output = match output with None -> fname ^ ".gz" | Some fname -> fname in + debug_print (sprintf "gzipping %s (keep: %b, output: %s)" fname keep output); + let (ic, oc) = (open_in fname, Gzip.open_out output) in let buf = String.create bufsiz in (try while true do @@ -145,21 +146,33 @@ let gzip ?(keep = false) fname = with End_of_file -> ()); close_in ic; Gzip.close_out oc; if not keep then Sys.remove fname - -let gunzip ?(keep = false) fname = - debug_print (sprintf "gunzipping %s (keep: %b)" fname keep); - let basename = Pcre.replace ~rex:trailing_dot_gz_RE fname in - assert (basename <> fname); - let (ic, oc) = (Gzip.open_in fname, open_out basename) in +;; + +let gunzip ?(keep = false) ?output fname = + (* assumption: given file name ends with ".gz" or output is set *) + let output = + match output with + | None -> + if (Pcre.pmatch ~rex:trailing_dot_gz_RE fname) then + Pcre.replace ~rex:trailing_dot_gz_RE fname + else + failwith + "Http_getter_misc.gunzip: unable to determine output file name" + | Some fname -> fname + in + debug_print (sprintf "gunzipping %s (keep: %b, output: %s)" + fname keep output); + let (ic, oc) = (Gzip.open_in fname, open_out output) in let buf = String.create bufsiz in (try while true do let bytes = Gzip.input ic buf 0 bufsiz in - if bytes = 0 then raise End_of_file else output oc buf 0 bytes + if bytes = 0 then raise End_of_file else Pervasives.output oc buf 0 bytes done with End_of_file -> ()); Gzip.close_in ic; close_out oc; if not keep then Sys.remove fname +;; let tempfile () = Filename.temp_file "http_getter_" "" diff --git a/helm/http_getter/http_getter_misc.mli b/helm/http_getter/http_getter_misc.mli index 946446dc6..2047cf204 100644 --- a/helm/http_getter/http_getter_misc.mli +++ b/helm/http_getter/http_getter_misc.mli @@ -47,10 +47,14 @@ val cp: string -> string -> unit standard wget rules are used. Additionally this function support also the "file://" scheme for file system addressing *) val wget: ?output: string -> string -> unit - (** gzip frontend, if keep = true original file will be kept *) -val gzip: ?keep: bool -> string -> unit - (** gunzip frontend, if keep = true original file will be kept *) -val gunzip: ?keep: bool -> string -> unit + (** gzip frontend. If keep = true original file will be kept, default is + false. output is the file on which gzipped data will be saved, default is + given file with an added ".gz" suffix *) +val gzip: ?keep: bool -> ?output: string -> string -> unit + (** gunzip frontend. If keep = true original file will be kept, default is + false. output is the file on which gunzipped data will be saved, default is + given file name without trailing ".gz" *) +val gunzip: ?keep: bool -> ?output: string -> string -> unit (** tempfile frontend, return the name of created file. A special purpose suffix is used (actually "_http_getter" *) val tempfile: unit -> string -- 2.39.2