Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

gzip / gunzip

by wufnik (Friar)
on Jul 09, 2004 at 23:12 UTC ( #373286=snippet: print w/replies, xml ) Need Help??
Description: simple gzip & gunzip functionality without nasty "system's" calls. deals with extensions relatively intelligently. illustrates & requires use of Compress::Zlib.
use Compress::Zlib;
use strict;
foreach my $f ( grep { -f } @ARGV ){
    my ($buffer,$gz);
    if ($f=~/gz$/){
    (my $rtf = $f) =~ s/(.)gz$//;
    open OOT, ">$rtf" or die "$0:$rtf:$!";    
    binmode OOT;    
    $gz = gzopen($f, "rb") or die "$0:$gzerrno:$!";
    print OOT $buffer while $gz->gzread($buffer) > 0;
    die "$f: $gzerrno:$!" if $gzerrno != Z_STREAM_END;
    $gz->gzclose();
    close(OOT);
    } else { # gzip our file
    open COOT, "$f" or die "$0:$f:$!";
    binmode COOT;
    $gz = gzopen("$f.gz", "wb") 
        or die "$0:$gzerrno:$!";
    while (<COOT>){
        $gz->gzwrite($_) or die "$f.gz,$gzerrno:$!";
    }
    $gz->gzclose;
    }
}
Replies are listed 'Best First'.
Re: gzip / gunzip
by hossman (Prior) on Jul 10, 2004 at 07:58 UTC

    see also the source to Tie::Gzip which demonstrates (un)gzip using Compress::Zlib if installed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://373286]
help
Chatterbox?
[Lady_Aleena]: Your Mother, that's the command line grep, not perl's grep.
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-05-23 19:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?