Syntactic Confectionery Delight PerlMonks

### unzipping files with .gz extension

 on Dec 29, 2012 at 16:59 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Need to unzip files with .gz extension and have to zip it back.Can anyone help please? Thanks

Replies are listed 'Best First'.
Re: unzipping files with .gz extension
by roboticus (Chancellor) on Dec 29, 2012 at 17:15 UTC

The .gz suffix implies a gzip compressed file. If you go to CPAN and search for gzip, you'll find several modules that will help solve your problem. I can't recommend which one to use, as I've not used any of them. Perhaps another monk will chime in with a recommendation.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Re: unzipping files with .gz extension
by ww (Bishop) on Dec 29, 2012 at 17:35 UTC
Is this a Perl question or are you merely dropping a line in anything that looks like water?

If Perl-related, show us what you've tried and where you're stuck... or simply follow roboticus suggestion and see what you find. If the topic is not Perl, maybe a little searching the web will work better than asking at a Perl-specific site.

Re: unzipping files with .gz extension
by ansh batra (Friar) on Dec 29, 2012 at 17:53 UTC

do you need a perl script for that?
if yes
then please try it urself ,you'll feel good trying it urself
try this and this

Hi, I need perl script itself.My code is throwing an error like this Can't locate loadable object for module PerlIO::gzip in @INC (@INC contains: C:/ Perl/site/lib C:/Perl/lib .) at unzip2.pl line 1 Compilation failed in require at unzip2.pl line 1. BEGIN failed--compilation aborted at unzip2.pl line 1.

use PerlIO::gzip;

open( IN, "<:gzip", test1.txt.gz ) or die "open failed\n";
while (<IN>) {
print \$_;
}
[download]

Pop on over to CPAN and install PerlIO::gzip, then.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Re: unzipping files with .gz extension
by pvaldes (Chaplain) on Dec 29, 2012 at 23:21 UTC
Re: unzipping files with .gz extension
by space_monk (Chaplain) on Dec 29, 2012 at 19:59 UTC
Most tar commands also unzip files (and can create them): for unzipping, tar xvzf yourfile.gz will probably work on Linux and a lot of other Unix systems without going near Perl. Its a bit dirty, but if you are doing it in a Perl script, you can do that as a system command enclosed in backticks. However the other methods suggested are much more preferable if you are doing this within Perl. Update - ignore this reply. I should have read the question more carefully.. :-(
A Monk aims to give answers to those who have none, and to learn from those who know more.
Most tar commands also unzip files (and can create them): for unzipping, tar xvzf yourfile.gz will probably work on Linux and a lot of other Unix systems without going near Perl. Its a bit dirty, but if you are doing it in a Perl script, you can do that as a system command enclosed in backticks. However the other methods suggested are much more preferable if you are doing this within Perl.

This is nonsense!

GNU tar can decompress on the fly, but it DOES NOT decompress non-tar files:

/tmp>gzip foo.txt

/tmp>tar xvzf foo.txt.gz
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

/tmp>dir
total 1
-rw-r--r-- 1 alex users 527 Dec 29 21:21 foo.txt.gz

/tmp>
[download]

If you choose to use external tools, use the proper tools. tar is the wrong tool for handling arbitary gzip-compressed files. gzip with the -d switch, gunzip, and zcat are the right tools.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010828]
Front-paged by 2teez
help
Chatterbox?
 [Corion]: thezip: If you want to open vim and can live with opening a second console window, use start "The results" vim.exe c:\path\to\logfile .log [thezip]: Ooops... I lied. I guess Cygwin is back. I'll just do a tail -f instead. Better. Sorry for the noise.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2017-03-27 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (321 votes). Check out past polls.