Compress a Single File with GZIP using camlzip

Task

Compression / Compress a Single File with GZIP

Opam Packages Used

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

Code

We open both files (source with In_channel.open_bin and dest with Gzip.open_out) and transfer bytes through a bytes buffer.


let buffer_size = 4096

let gzip source dest =
  let gz_file = Gzip.open_out ~level:9 dest in
  let buffer = Bytes.make buffer_size '*' in
  let ic = In_channel.open_bin source in
  let rec aux () =
    let len = In_channel.input ic buffer 0 buffer_size in
    if len <> 0 then
      begin
        Gzip.output gz_file buffer 0 len;
        aux ()
      end
  in
  aux ();
  Gzip.close_out gz_file;
  In_channel.close ic

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

Open an issue or contribute to this recipe!