Boris
Boris

Reputation: 59

Reading .gz file line by line

I am trying to read large .gz file line by line. Here is what i got so far:

$sfp = gzopen($filename, "rb");
while (!gzeof($sfp)) 
{
    $line = gzread($sfp, 4096);
}

and thats where the problem comes in, gzread reading the length specified in variable (in this case 4096) and ignoring new lines .

I checked "fget" function and it works properly, so its reading line limiting the size by hitting new line or the size which ever comes first. How I can do the same with gzread or any work around ?

Upvotes: 0

Views: 1886

Answers (1)

Barmar
Barmar

Reputation: 782181

Use fgets() just like reading an ordinary file.

You shouldn't use b mode if you're reading lines, since line-by-line implies it's a text file.

$sfp = gzopen($filename, "r");
while ($line = fgets($sfp)) {
    echo $line;
}

Upvotes: 3

Related Questions