34

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

0

7 Answers 7

88

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

curl_setopt($ch,CURLOPT_ENCODING, '');
4
  • 3
    Value should be empty string or encoding type. not int
    – redben
    Commented Jul 12, 2012 at 10:32
  • 1
    Thanks! Wouldn't this be a sensible default CURL setting? Commented Jul 24, 2013 at 19:21
  • 1
    @redben My proposal was declined in 2011 and now AlixAxel's was approved. :(
    – mgutt
    Commented Apr 2, 2015 at 9:09
  • use 'curl_setopt($ch , CURLOPT_ENCODING, 'identity');' if you want to avoid encoding, per link
    – blsaws
    Commented Sep 30, 2022 at 17:29
18

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
...
6
  • 3
    yes true, but I have php 5.2, and they say that gzdecode is available since 6.0 only I have a gentoo with php and zlib configured , and i can't seem to have available those gz functions. Any ideeas? :)
    – PartySoft
    Commented Jun 9, 2010 at 15:13
  • 6
    Ok, i have a solution , no pretty because i had to write to a file the contents, and not only use a string : function gzdecode($data){ $g=tempnam('/tmp','ff'); @file_put_contents($g,$data); ob_start(); readgzfile($g); $d=ob_get_clean(); unlink($g); return $d; } and it works :)
    – PartySoft
    Commented Jun 9, 2010 at 15:27
  • @PartySoft That function is great!
    – barfoon
    Commented Oct 21, 2011 at 20:48
  • @PartySoft Thanks for that! Worked like a charm. Running 5.3.X and had no luck with gzuncompress().
    – donut
    Commented Mar 17, 2014 at 22:09
  • 1
    what the heck, no, just set CURLOPT_ENCODING to "gzip" and curl will automatically decompress gzip-encoded responses
    – hanshenrik
    Commented Jun 25, 2019 at 9:58
13

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

1

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

curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
1
  • 4
    he's already receiving it encoded, so I don't think declaring that he accepts it encoded would do any difference. I tried sending the server Accept-encoding: none and the server did not comply.
    – Artefacto
    Commented Jun 9, 2010 at 1:59
1

With a zlib Stream Wrapper:

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

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

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;
        }
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.