在 php 上从 CURL 解压缩 gzip 文件

发布于 2024-09-05 01:12:55 字数 662 浏览 4 评论 0 原文

有谁知道如何解压缩我用curl 获得的gzip 文件的内容?

例如: http://torcache.com/torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent

响应

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 09 Jun 2010 01:11:26 GMT
Content-Type: application/x-bittorrent
Content-Length: 52712
Last-Modified: Tue, 08 Jun 2010 15:09:58 GMT
Connection: keep-alive
Expires: Fri, 09 Jul 2010 01:11:26 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
Accept-Ranges: bytes

然后压缩的 gzip ,

我尝试了 gzdecode 但不起作用,gzeflate 也没有得到任何响应,并且文件的内容不超过 2k

Does anyone know how to uncompress the contents of a gzip file that i got with curl?

for example: http://torcache.com/torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent

responded

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 09 Jun 2010 01:11:26 GMT
Content-Type: application/x-bittorrent
Content-Length: 52712
Last-Modified: Tue, 08 Jun 2010 15:09:58 GMT
Connection: keep-alive
Expires: Fri, 09 Jul 2010 01:11:26 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
Accept-Ranges: bytes

then the compressed gzip,

i tried gzdecode but doesn't work , gzeflate as well doesn't they simply don't get any response, and the contents of the files are no more than 2k

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

幸福丶如此 2024-09-12 01:12:55

只要告诉 cURL 在 gzip 压缩时自动解码响应即可

curl_setopt($ch,CURLOPT_ENCODING, '');

Just tell cURL to decode the response automatically whenever it's gzipped

curl_setopt($ch,CURLOPT_ENCODING, '');
柒夜笙歌凉 2024-09-12 01:12:55

使用 gzdecode

<?php
    $c = file_get_contents("http://torcache.com/" .
        "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
    echo gzdecode($c);

给出

d8:announce42:http://tracker.openbittorrent.com/announce13:announce-listll42
...

Use gzdecode:

<?php
    $c = file_get_contents("http://torcache.com/" .
        "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
    echo gzdecode($c);

gives

d8:announce42:http://tracker.openbittorrent.com/announce13:announce-listll42
...
ら栖息 2024-09-12 01:12:55

libcurl 提供了一项功能,可以自动解压缩内容(如果使用 zlib 构建)。

请参阅 CURLOPT_ACCEPT_ENCODING 选项: https://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING .html

libcurl offers a feature that makes it decompress the contents automatically (if built with zlib).

See the CURLOPT_ACCEPT_ENCODING option: https://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html

乱世争霸 2024-09-12 01:12:55

您是否尝试过设置标头以表明您接受 gzip 编码,如下所示?:

curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));

Have you tried setting the header stating that you accept gzip encoding as follows?:

curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
雅心素梦 2024-09-12 01:12:55

使用 zlib 流包装器:​​

file_get_contents("compress.zlib://http://torcache.com/" .
    "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");

With a zlib Stream Wrapper:

file_get_contents("compress.zlib://http://torcache.com/" .
    "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
情深如许 2024-09-12 01:12:55

您是否尝试过 gzuncompressgzinflate

gzdeflate 压缩,与您想要的相反。老实说,我无法弄清楚 gzdecode 与普通解压缩有何不同。

还有 cURL 选项 CURLOPT_ENCODING

“Accept-Encoding:”标头的内容。 这可以对响应进行解码。支持的编码包括“identity”、“deflate”和“gzip”。如果设置了空字符串“”,则发送包含所有支持的编码类型的标头。

这似乎意味着它会自动解压缩响应,但我还没有测试过。

Have you tried gzuncompress or gzinflate?

gzdeflate compresses, the opposite of what you want. To be honest, I can't figure out how gzdecode differs from normal uncompressing.

There's also the cURL option CURLOPT_ENCODING:

The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent.

It seems to mean it'll automatically decompress the response, but I haven't tested that.

贱贱哒 2024-09-12 01:12:55

您可以使用 gzinflate 来完成此操作(假装 $headers 包含您的所有 HTTP 标头,而 $buffer 包含您的数据):

if (isset($headers['Content-Encoding']) && ($headers['Content-Encoding'] === 'gzip' || $headers['Content-Encoding'] === 'deflate'))
    {
        if ($headers['Content-Encoding'] === 'gzip')
        {
            $buffer = substr($buffer, 10);
        }
        $contents = @gzinflate($buffer);
        if ($contents === false)
        {
            return false;
        }
    }

You can do it with gzinflate (pretending that $headers contains all your HTTP headers, and $buffer contains your data):

if (isset($headers['Content-Encoding']) && ($headers['Content-Encoding'] === 'gzip' || $headers['Content-Encoding'] === 'deflate'))
    {
        if ($headers['Content-Encoding'] === 'gzip')
        {
            $buffer = substr($buffer, 10);
        }
        $contents = @gzinflate($buffer);
        if ($contents === false)
        {
            return false;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文