代码之家  ›  专栏  ›  技术社区  ›  l008com

PHP:我可以使用内置的curl函数通过gunzip输出curl吗?

  •  0
  • l008com  · 技术社区  · 6 年前

    在…的帮助下 ##php curl 直接到 gunzip 要将磁盘i/o减半(不包括SQL):

    https://example.com/path/to/large_file.gz |gunzip-c>/large\u temp\u文件/large\u文件

    经验证,该方法不需要先写入压缩数据,直接将未压缩的数据写入磁盘。

    所以我的问题是,有没有什么方法可以使用php的内置代码来像这样对数据进行管道处理 卷曲

    curl_setopt 选项,将文件下载到磁盘,而不是将数据设置为变量。

    这些是5gb的文件,所以这是行不通的。我的所有其他代码都使用内置函数来处理http请求,因此如果可能的话,为了保持一致性和可读性,我希望继续这样做。

    1 回复  |  直到 6 年前
        1
  •  1
  •   hanshenrik    6 年前

    我还没有实际测试过这个,但是我假设可以通过使用一个自定义的CURLOPT\u WRITEFUNCTION和inflate\u init()&co之类的

    $decompressor = inflate_init(ZLIB_ENCODING_DEFLATE);
    $fp = fopen("decompressed", "wb");
    $ch = curl_init("http://url.com/large_file.zip");
    curl_setopt_array($ch, array(
        CURLOPT_WRITEFUNCTION => function ($ch, string $compressed) use (&$fp, &$decompressor) {
            fwrite($fp, inflate_add($decompressor, $compressed));
            return strlen($compressed);
        }
    ));
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    unset($fp,$ch,$decompressor); // don't know how to clean up the decompressor, hopefully GC will do it.
    

    顺便说一句,如果您真的想了解,您可以直接解析deflate_add()调用中的数据并将其插入SQL数据库,而无需将解压缩后的数据写入磁盘,这可能会更快(与从硬盘读取数据相比,从ram读取数据更容易) 非常 快:))