ActivePerl Documentation
|
SUPPORTED PLATFORMS
NAMECompress::Zlib - Interface to zlib compression library
SYNOPSIS
use Compress::Zlib ;
($d, $status) = deflateInit( [OPT] ) ;
($out, $status) = $d->deflate($buffer) ;
($out, $status) = $d->flush() ;
$d->dict_adler() ;
($i, $status) = inflateInit( [OPT] ) ;
($out, $status) = $i->inflate($buffer) ;
$i->dict_adler() ;
$dest = compress($source) ;
$dest = uncompress($source) ;
$gz = gzopen($filename or filehandle, $mode) ;
$bytesread = $gz->gzread($buffer [,$size]) ;
$bytesread = $gz->gzreadline($line) ;
$byteswritten = $gz->gzwrite($buffer) ;
$status = $gz->gzflush($flush) ;
$status = $gz->gzclose() ;
$errstring = $gz->gzerror() ;
$gzerrno
$dest = Compress::Zlib::memGzip($buffer) ;
$crc = adler32($buffer [,$crc]) ;
$crc = crc32($buffer [,$crc]) ;
ZLIB_VERSION
DESCRIPTIONThe Compress::Zlib module provides a Perl interface to the zlib compression library (see AUTHORS for details about where to get zlib). Most of the functionality provided by zlib is available in Compress::Zlib. The module can be split into two general areas of functionality, namely in-memory compression/decompression and read/write access to gzip files. Each of these areas will be discussed separately below.
DEFLATEThe interface Compress::Zlib provides to the in-memory deflate (and inflate) functions has been modified to fit into a Perl model. The main difference is that for both inflation and deflation, the Perl interface will always consume the complete input buffer before returning. Also the output buffer returned will be automatically grown to fit the amount of output available. Here is a definition of the interface available:
($d, $status) = deflateInit( [OPT] )Initialises a deflation stream. It combines the features of the zlib functions deflateInit, deflateInit2 and deflateSetDictionary. If successful, it will return the initialised deflation stream, $d
and $status of If not successful, the returned deflation stream ($d) will be undef and $status will hold the exact zlib error code. The function optionally takes a number of named options specified as
For backward compatability, it is also possible to pass the parameters as a reference to a hash containing the name=>value pairs. The function takes one optional parameter, a reference to a hash. The contents of the hash allow the deflation interface to be tailored. Here is a list of the valid options:
Here is an example of using the deflateInit optional parameter list to override the default buffer size and compression level. All other options will take their default values.
deflateInit( -Bufsize => 300,
-Level => Z_BEST_SPEED ) ;
($out, $status) = $d->deflate($buffer)Deflates the contents of $buffer. The buffer can either be a scalar
or a scalar reference. When finished, $buffer will be
completely processed (assuming there were no errors). If the deflation
was successful it returns the deflated output, $out, and a status
value, $status, of On error, $out will be undef and $status will contain the zlib error code. In a scalar context deflate will return $out only. As with the deflate function in zlib, it is not necessarily the case that any output will be produced by this method. So don't rely on the fact that $out is empty for an error test.
($out, $status) = $d->flush()Finishes the deflation. Any pending output will be returned via $out.
$status will have a value In a scalar context flush will return $out only. Note that flushing can degrade the compression ratio, so it should only be used to terminate a decompression.
$d->dict_adler()Returns the adler32 value for the dictionary.
ExampleHere is a trivial example of using deflate. It simply reads standard input, deflates it and writes it to standard output.
use Compress::Zlib ;
binmode STDIN;
binmode STDOUT;
$x = deflateInit()
or die "Cannot create a deflation stream\n" ;
while (<>)
{
($output, $status) = $x->deflate($_) ;
$status == Z_OK
or die "deflation failed\n" ;
print $output ;
}
($output, $status) = $x->flush() ;
$status == Z_OK
or die "deflation failed\n" ;
print $output ;
INFLATEHere is a definition of the interface:
($i, $status) = inflateInit()Initialises an inflation stream. In a list context it returns the inflation stream, $i, and the zlib status code ($status). In a scalar context it returns the inflation stream only. If successful, $i will hold the inflation stream and $status will
be If not successful, $i will be undef and $status will hold the zlib error code. The function optionally takes a number of named options specified as
For backward compatability, it is also possible to pass the parameters as a reference to a hash containing the name=>value pairs. The function takes one optional parameter, a reference to a hash. The contents of the hash allow the deflation interface to be tailored. Here is a list of the valid options:
Here is an example of using the inflateInit optional parameter to override the default buffer size.
inflateInit( -Bufsize => 300 ) ;
($out, $status) = $i->inflate($buffer)Inflates the complete contents of $buffer. The buffer can either be a scalar or a scalar reference. Returns The This feature is useful when processing a file format that encapsulates a compressed data stream (e.g. gzip, zip).
$i->dict_adler()Returns the adler32 value for the dictionary.
ExampleHere is an example of using inflate.
use Compress::Zlib ;
$x = inflateInit()
or die "Cannot create a inflation stream\n" ;
$input = '' ;
binmode STDIN;
binmode STDOUT;
while (read(STDIN, $input, 4096))
{
($output, $status) = $x->inflate(\$input) ;
print $output
if $status == Z_OK or $status == Z_STREAM_END ;
last if $status != Z_OK ;
}
die "inflation failed\n"
unless $status == Z_STREAM_END ;
COMPRESS/UNCOMPRESSTwo high-level functions are provided by zlib to perform in-memory compression. They are compress and uncompress. Two Perl subs are provided which provide similar functionality.
GZIP INTERFACEA number of functions are supplied in zlib for reading and writing gzip files. This module provides an interface to most of them. In general the interface provided by this module operates identically to the functions provided by zlib. Any differences are explained below.
ExamplesHere is an example script which uses the interface. It implements a gzcat function.
use Compress::Zlib ;
die "Usage: gzcat file...\n"
unless @ARGV ;
foreach $file (@ARGV) {
$gz = gzopen($file, "rb")
or die "Cannot open $file: $gzerrno\n" ;
print $buffer
while $gz->gzread($buffer) > 0 ;
die "Error reading from $file: $gzerrno\n"
if $gzerrno != Z_STREAM_END ;
$gz->gzclose() ;
}
Below is a script which makes use of gzreadline. It implements a very simple grep like script.
use Compress::Zlib ;
die "Usage: gzgrep pattern file...\n"
unless @ARGV >= 2;
$pattern = shift ;
foreach $file (@ARGV) {
$gz = gzopen($file, "rb")
or die "Cannot open $file: $gzerrno\n" ;
while ($gz->gzreadline($_) > 0) {
print if /$pattern/ ;
}
die "Error reading from $file: $gzerrno\n"
if $gzerrno != Z_STREAM_END ;
$gz->gzclose() ;
}
This script, gzstream, does the opposite of the gzcat script above. It reads from standard input and writes a gzip file to standard output.
use Compress::Zlib ;
binmode STDOUT; # gzopen only sets it on the fd
my $gz = gzopen(\*STDOUT, "wb")
or die "Cannot open stdout: $gzerrno\n" ;
while (<>) {
$gz->gzwrite($_)
or die "error writing: $gzerrno\n" ;
}
$gz->gzclose ;
Compress::Zlib::memGzipThis function is used to create an in-memory gzip file. It creates a minimal gzip header.
$dest = Compress::Zlib::memGzip($buffer) ;
If successful, it returns the in-memory gzip file, otherwise it returns undef. The buffer parameter can either be a scalar or a scalar reference.
CHECKSUM FUNCTIONSTwo functions are provided by zlib to calculate a checksum. For the Perl interface, the order of the two parameters in both functions has been reversed. This allows both running checksums and one off calculations to be done.
$crc = adler32($buffer [,$crc]) ;
$crc = crc32($buffer [,$crc]) ;
The buffer parameters can either be a scalar or a scalar reference. If the $crc parameters is
ACCESSING ZIP FILESIf you want to use this module to access zip files there are a number of undocumented features in the zlib library you need to be aware of.
CONSTANTSAll the zlib constants are automatically imported when you make use of Compress::Zlib.
AUTHORThe Compress::Zlib module was written by Paul Marquess, Paul.Marquess@btinternet.com. The latest copy of the module can be found on CPAN in modules/by-module/Compress/Compress-Zlib-x.x.tar.gz. The primary site for the zlib compression library is http://www.cdrom.com/pub/infozip/zlib/.
MODIFICATION HISTORY
0.1 2nd October 1995.First public release of Compress::Zlib.
0.2 5th October 1995.Fixed a minor allocation problem in Zlib.xs
0.3 12th October 1995.Added prototype specification.
0.4 25th June 1996.
0.50 19th Feb 1997
1.00 14 Nov 1997
1.01 23 Nov 1997
1.02 10 Jan 1998
1.03 17 Mar 1999
1.04 27 May 1999
1.05 3 June 1999
1.06 20 September 1999
1.07 27 November 1999
1.08 27 January 2000
|