Decompress a Single File Compressed With GZIP using camlzip

Task

Compression / Decompress a Single File Compressed With GZIP

Opam Packages Used

  • camlzip Tested with version: 1.11 — Used libraries: camlzip

Code

To decompress a single file using the Gzip module from camlzip, we open both files (source with Gzip.open_in and dest with Out_channel.open_bin) and transfer bytes through a bytes buffer.


let buffer_size = 4096

let gunzip source dest =
  let gz_file = Gzip.open_in source in
  let buffer = Bytes.make buffer_size '*' in
  let oc = Out_channel.open_bin dest in
  let rec aux () =
    let len = Gzip.input gz_file buffer 0 buffer_size in
    if len <> 0 then
      begin
        Out_channel.output oc buffer 0 len;
        aux ()
      end
  in
  aux ();
  Gzip.close_in gz_file;
  Out_channel.close oc

Recipe not working? Comments not clear or out of date?

Open an issue or contribute to this recipe!