Skip to content

Instantly share code, notes, and snippets.

@hcarty
Created April 21, 2021 20:41
Show Gist options
  • Save hcarty/18650157caa9f0f6d1a19df62d89c138 to your computer and use it in GitHub Desktop.
Save hcarty/18650157caa9f0f6d1a19df62d89c138 to your computer and use it in GitHub Desktop.
Partial example for extracting a tar in memory using ocaml-tar
let max_ocaml_int = Int64.of_int max_int
let read_file input_channel (header : Tar_cstruct.Header.t) =
let file_size = header.file_size in
assert (file_size <= max_ocaml_int);
let buf = Cstruct.create (Int64.to_int file_size) in
Tar_cstruct.really_read input_channel buf;
buf
let rec read_files input_channel accu =
match Tar_cstruct.Archive.with_next_file input_channel read_file with
| file -> read_files input_channel (file :: accu)
| exception Tar_cstruct.Header.End_of_stream -> List.rev accu
let do_stuff data =
let tar = Tar_cstruct.make_in_channel data in
let files = read_files tar [] in
... do something with files ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment