Uncompress a gzip file from CURL, on php

后端 未结 7 1208
一个人的身影
一个人的身影 2020-12-01 15:58

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

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

相关标签:
7条回答
  • 2020-12-01 16:11

    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.

    0 讨论(0)
  • 2020-12-01 16:12

    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
    ...
    
    0 讨论(0)
  • 2020-12-01 16:22

    With a zlib Stream Wrapper:

    file_get_contents("compress.zlib://http://torcache.com/" .
        "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
    
    0 讨论(0)
  • 2020-12-01 16:24

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

    curl_setopt($ch,CURLOPT_ENCODING, '');
    
    0 讨论(0)
  • 2020-12-01 16:27

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

    curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
    
    0 讨论(0)
  • 2020-12-01 16:27

    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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题