GZip a file using Perl





7
Date Submitted Wed. Mar. 29th, 2006 11:31 AM
Revision 1 of 1
Helper digitaljunkie
Tags compress | File | gzip | Perl
Comments 0 comments
Need to GZip a file using Perl?

Use Compress::Zlib.

This is a simple perl script which creates a GZip file.

use strict;
use Compress::Zlib;

my $file  = $ARGV[0];

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

my $gzfile = $file.".gz";

open (FILE, $file);
binmode FILE;

my $buf;
my $gz = gzopen($gzfile, "wb");
if (! $gz) {
   print "Unable to write $gzfile $!\n";
   exit;
}
else {
   while (my $by = sysread (FILE, $buf, 4096)) {
      if (! $gz->gzwrite($buf)) {
         print "Zlib error writing to $gzfile: $gz->gzerror\n";
         exit;
      }
   }
   $gz->gzclose();
   print "'$file' GZipped to '$gzfile'\n";
}
 

Steve Wilder

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

Comments

There are currently no comments for this snippet.

Voting