在 php 上从 CURL 解压缩 gzip 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3002612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncompress a gzip file from CURL, on php
提问by PartySoft
Does anyone know how to uncompress the contents of a gzip file that i got with curl?
有谁知道如何解压缩我用 curl 得到的 gzip 文件的内容?
for example: http://torcache.com/torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent
例如: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,
然后是压缩的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
我试过 gzdecode 但没有用,gzeflate 也没有得到任何响应,文件的内容不超过 2k
采纳答案by Artefacto
回答by mixdev
Just tell cURL to decode the response automatically whenever it's gzipped
只需告诉 cURL 在 gzip 压缩时自动解码响应
curl_setopt($ch,CURLOPT_ENCODING, '');
回答by Daniel Stenberg
libcurl offers a feature that makes it decompress the contents automatically (if built with zlib).
libcurl 提供了一项功能,可以自动解压缩内容(如果使用 zlib 构建)。
See the CURLOPT_ACCEPT_ENCODING option: https://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html
查看 CURLOPT_ACCEPT_ENCODING 选项:https: //curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html
回答by Oren Hizkiya
Have you tried setting the header stating that you accept gzip encoding as follows?:
您是否尝试设置标头,说明您接受 gzip 编码如下?:
curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
回答by Gordon
With a zlib Stream Wrapper:
file_get_contents("compress.zlib://http://torcache.com/" .
"torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
回答by Yvan
You can do it with gzinflate (pretending that $headers contains all your HTTP headers, and $buffer contains your data):
您可以使用 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;
}
}
回答by deceze
Have you tried gzuncompressor gzinflate?
您是否尝试过gzuncompress或gzinflate?
gzdeflatecompresses, the opposite of what you want. To be honest, I can't figure out how gzdecodediffers from normal uncompressing.
gzdeflatecompresses,与您想要的相反。老实说,我无法弄清楚gzdecode与正常解压缩有什么不同。
There's also the cURL option CURLOPT_ENCODING:
还有 cURL 选项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.
“Accept-Encoding:”标题的内容。这使得能够对响应进行解码。支持的编码是“identity”、“deflate”和“gzip”。如果设置了空字符串“”,则发送包含所有支持的编码类型的标头。
It seems to mean it'll automatically decompress the response, but I haven't tested that.
这似乎意味着它会自动解压缩响应,但我还没有测试过。

