Php 如何解压缩tar.gz字符串

Php 如何解压缩tar.gz字符串,php,tar,gzip,compression,Php,Tar,Gzip,Compression,我有一个字符串,它是tar.gz压缩文件的内容(如果有,文件名将是tar.gz)。它以JSON的形式传递给我,字符串是base64编码的 我已经设法从base64解码了字符串,现在剩下一个tar.gz字符串。有人知道我有什么方法可以将其提取到内存中,而不必创建临时文件,然后使用系统('tar-zxvf…')?它认为这样做真的是浪费资源,但是如果我不能弄明白,我想我必须创建一个文件并使用system() 内部只有一个文件(xml文件),因此它不是多个文件。您可以使用和管道将文件流式传输到tar的s

我有一个字符串,它是tar.gz压缩文件的内容(如果有,文件名将是tar.gz)。它以JSON的形式传递给我,字符串是base64编码的

我已经设法从base64解码了字符串,现在剩下一个tar.gz字符串。有人知道我有什么方法可以将其提取到内存中,而不必创建临时文件,然后使用系统('tar-zxvf…')?它认为这样做真的是浪费资源,但是如果我不能弄明白,我想我必须创建一个文件并使用system()

内部只有一个文件(xml文件),因此它不是多个文件。

您可以使用和管道将文件流式传输到tar的stdin。下面是一个来自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);
}

我知道PHP可以解压内存中的
.gz
文件,但不确定
.tar
。我认为您必须创建一个文件并使用命令行查看以下内容: