Gunzip using Compress::Zlib in Perl





9
Date Submitted Tue. Mar. 7th, 2006 4:47 PM
Revision 1 of 1
Helper digitaljunkie
Tags compress | gunzip | gzip | Perl
Comments 0 comments
The docs for Compress::Zlib for perl are very complex. But, the most simple use of the Module is not too bad. I've waded through the perdoc so you don't have to.

Gunzip.pl is here. I will let you know when I have Gzip.pl done.

You can do something more interesting with "success" variable.


use strict;
use Compress::Zlib;

my $infile  = $ARGV[0];
my $outfile = $ARGV[1];

if (! $infile || ! $outfile) {
   print "$0 <infile> <outfile>\n";
   exit;
}

my $buffer;
my $gz = undef;
my $success = 1;

if (! open FH, "> $outfile") {
   $success = 0;
   print "Unable to write to '$outfile'\n";
}
else {
   binmode FH;
   
   print "GZ File  = '$infile'\n";
   print "OUT File = '$outfile'\n";
         
   if ($gz = gzopen($infile, "rb")) {
      while ($gz->gzread($buffer) > 0) {
         print FH $buffer;
      }
      if ($gzerrno != Z_STREAM_END) {
         $success = 0;
         print "ZLib Error: reading $outfile - $gzerrno: $!\n";
      }
      else {
         $success = 1;
      }
      $gz->gzclose;
   }
   else {
      $success = 0;
      print "ZLib Error: opening $infile - $gzerrno: $!\n";
   }
   close FH;
}

 

Steve Wilder

Steve Wilder
Technology Resource Group
unpack "u", qq(1

Comments

There are currently no comments for this snippet.

Voting