| 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
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_" ""
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