波 李
波 李

Reputation: 21

How to decompress gzip stream chunk by chunk using PHP?

I can't read from an active http gzip stream chunk by chunk.

In short, it can't decompress the stream chunk by chunk, it requires the first chunk when it decompress the second one, it requires the first and second one when decompress the third one, or it will return strange characters(gzip string I guess).

I guess there are no existing ways for this as I have googled it for 2 days, anyway, I'll be appreciative if you have any suggestions.

Following is the function which I am using for decompressing:

function gzdecode1($data){
    $g = tempnam('./','gz');
    file_put_contents($g,$data);
    ob_start();
    readgzfile($g);
    $d = ob_get_clean();
    unlink($g);
    return $d;
}

Here are ten example chunks http://2.youpiaoma.com/chunk_s.rar

Upvotes: 2

Views: 1274

Answers (1)

fd8s0
fd8s0

Reputation: 1927

Use gzopen() and gzread()

$h = gzopen($filename, 'r');
while ($chunk = gzread($h, $chunksize)) {
    // do magic
}

If it's a remote you might need to enable that remote file opens, I've never done it in that kind of environment though.

Upvotes: 1

Related Questions