David Wells
David Wells

Reputation: 29

Using perl, is there a way to open gzip files and read through the lines without fully decompressing?

I basically want to make a file handle for the gzip and use it to match strings like I would any other file. I need to compare the lines from two gzip files but they're each 15 GB. I was using Archive::Extract and File::temp to read through them but my hard drive fills up.

Upvotes: 2

Views: 3754

Answers (1)

bolav
bolav

Reputation: 6998

You can pipe gzip -dc into filehandles like this:

 open(my $fh1, '-|', '/usr/bin/gzip -dc filename1.gz') or die $!;
 open(my $fh2, '-|', '/usr/bin/gzip -dc filename2.gz') or die $!;

It should buffer the output from gzip, and not use space on your disk.

PerlIO::gzip does not decompress the whole file, but uncompresses as it reads to your handle, so you can use this as well.

Upvotes: 2

Related Questions