http://qs321.pair.com?node_id=668344


in reply to unpacking .gz from perl

For most uses, you'll probably be satisfied with the solutions above, where you decompress the .gz file into memory and "operates" on the uncompressed content as you wish.

But if you need to deal with really big files or if the operations you'll need to apply prefer the content in a file (eg. when you need to pass it to external programs), you can do it with a combination of Archive::Extract and File::Temp. The following code is such an example:

use File::Temp (); use Archive::Extract (); for my $gz ( @ARGV ) { my $tmp = File::Temp->new(); my $ae = Archive::Extract->new( archive => $gz ); my $ok = $ae->extract( to => $tmp->filename ); die $ae->error unless $ok; # do what you want to the uncompressed content in the # file named $tmp->filename ... # at the end of the scope, $tmp vanishes }

This code has the bonus of working with other compressed types, like .bz2, .Z and even .lzma with the latest release of Archive::Extract. Even more, because this module may also extract archives with multiple files like .tar.gz, .zip, etc.