- if not (Pcre.pmatch ~pat:"\\.gz$" fname) then
- failwith "gunzip: source file doesn't end with '.gz'";
- let basename = Pcre.replace ~pat:"\\.gz$" fname in
- if keep then begin (* keep original file *)
- debug_print ("gunzip -f -c " ^ fname);
- Shell.call
- ~stdout:(Shell.to_file basename)
- [Shell.cmd "gunzip" ["-f"; "-c"; fname]]
- end else begin (* don't keep original file *)
- debug_print ("gunzip -f " ^ fname);
- Shell.call [Shell.cmd "gunzip" ["-f"; fname]]
- end
+ 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 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
+ done
+ with End_of_file -> ());
+ Gzip.close_in ic;
+ close_out oc;
+ if not keep then Sys.remove fname