1

I have a string that is the contents of a tar.gz compress file (the filename would be tar.gz if there was one). It's passed to me in JSON and the string is base64 encoded.

I've managed to decode the string from base64, and now am left with a tar.gz string. Does anyone know of a way for me to extract this to memory WITHOUT creating a temp file and then using system('tar -zxvf ...')? It think it would really be a waste of resources to do so, however if I cannot figure it out, I suppose I'll have to create a file and use system().

There is only ever one file inside (xml file) so its not multiple files.

Kladskull
  • 10,332
  • 20
  • 69
  • 111

1 Answers1

2

You can use proc_open and pipes to stream the file to stdin of tar. Here is a slightly adapted example from php.net

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$process = proc_open('tar xz', $descriptorspec, $pipes, __DIR__, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], $tar_gz_string);
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    proc_close($process);
}
akirk
  • 6,757
  • 2
  • 34
  • 57