NAME
Compress::Zlib - Interface to zlib compression library
SYNOPSIS
use Compress::Zlib ;
($d, $status) = deflateInit() ;
($out, $status) = $d->deflate($buffer) ;
($out, $status) = $d->flush() ;
($i, $status) = inflateInit() ;
($out, $status) = $i->inflate($buffer) ;
$dest = compress($source) ;
$dest = uncompress($source) ;
$gz = gzopen($filename, $mode) ;
$status = $gz->gzread($buffer [,$size]) ;
$status = $gz->gzreadline($line) ;
$status = $gz->gzwrite($buffer) ;
$status = $gz->gzflush($flush) ;
$status = $gz->gzclose() ;
$errstring = $gz->gzerror() ;
$gzerrno
$crc = adler32($buffer [,$crc]) ;
$crc = crc32($buffer [,$crc]) ;
ZLIB_VERSION
DESCRIPTION
The Compress::Zlib module provides a Perl interface to the zlib compression library. Most of the functionality provided by zlib is available for use in Compress::Zlib.
The module actually 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.
DEFLATE
The interface Compress::Zlib provides to the in-memory deflate (and inflate) functions has been modified to fit into a Perl model.
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()
Initialises a deflation stream.
It combines the features of both zlib functions deflateInit and deflateInit2.
If successful, it will return the initialised deflation stream, $d and $status of Z_OK
in a list context. In scalar context it returns the deflation stream, $d, only.
If not successful, the returned deflation stream ($d) will be undef and $status will hold the exact zlib error code.
The function takes one optional parameter, a reference to a hash. The contents of the hash allow the deflation interface to be tailored.
Below is a list of the valid keys that the hash can take.
- Level
-
Defines the compression level. Valid values are 1 through 9,
Z_BEST_SPEED
,Z_BEST_COMPRESSION
, andZ_DEFAULT_COMPRESSION
.The default is
Z_DEFAULT_COMPRESSION
. - Method
-
Defines the compression method. The only valid value at present (and the default) is
DEFLATED
. - WindowBits
-
For a definition of the meaning and valid values for WindowBits refer to the zlib documentation for deflateInit2.
Defaults to
MAX_WBITS
. - MemLevel
-
For a definition of the meaning and valid values for MemLevel refer to the zlib documentation for deflateInit2.
Defaults to
DEF_MEM_LEVEL
. - Strategy
-
Defines the strategy used to tune the compression. The valid values are
Z_DEFAULT_STRATEGY
,Z_FILTERED
andZ_HUFFMAN_ONLY
.The default is
Z_DEFAULT_STRATEGY
. - Bufsize
-
Sets the initial size for the deflation buffer. If the buffer has to be reallocated to increase the size, it will grow in increments of Bufsize.
The default is 4096.
Here is an example of using the deflateInit optional parameter to override the default buffer size and compression level.
deflateInit( {Bufsize => 300, Level => Z_BEST_SPEED } ) ;
($out, $status) = $d->deflate($buffer)
Deflates the contents of $buffer. 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 Z_OK
.
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 Z_OK
if successful.
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.
Example
Here is a trivial example of using deflate. It simply reads standard input, deflates it and writes it to standard output.
use Compress::Zlib ;
$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 ;
INFLATE
Here 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 Z_OK
.
If not successful, $i will be undef and $status will hold the zlib error code.
The function takes one optional parameter, a reference to a hash. The contents of the hash allow the inflation interface to be tailored.
Below is a list of the valid keys that the hash can take.
- WindowBits
-
For a definition of the meaning and valid values for WindowBits refer to the zlib documentation for inflateInit2.
Defaults to
MAX_WBITS
. - Bufsize
-
Sets the initial size for the inflation buffer. If the buffer has to be reallocated to increase the size, it will grow in increments of Bufsize.
Default is 4096.
Here is an example of using the inflateInit optional parameter to override the default buffer size.
deflateInit( {Bufsize => 300 } ) ;
($out, $status) = $i->inflate($buffer)
Inflates the complete contents of $buffer
Returns Z_OK
if successful and Z_STREAM_END
if the end of the compressed data has been reached.
Example
Here is an example of using inflate.
use Compress::Zlib ;
$x = inflateInit()
or die "Cannot create a inflation stream\n" ;
while ($size = 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/UNCOMPRESS
Two 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.
- $dest = compress($source) ;
-
Compresses $source. If successful it returns the compressed data. Otherwise it returns undef.
- $dest = uncompress($source) ;
-
Uncompresses $source. If successful it returns the uncompressed data. Otherwise it returns undef.
GZIP INTERFACE
A 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.
- $gz = gzopen(filename, mode)
-
This function operates identically to the zlib equivalent except that it returns an object which is used to access the other gzip methods.
As with the zlib equivalent, the mode parameter is used to specify both whether the file is opened for reading or writing and to optionally specify a a compression level. Refer to the zlib documentation for the exact format of the mode parameter.
- $status = $gz->gzread($buffer [, $size]) ;
-
Reads $size bytes from the compressed file into $buffer. If $size is not specified, it will default to 4096. If the scalar $buffer is not large enough, it will be extended automatically.
- $status = $gz->gzreadline($line) ;
-
Reads the next line from the compressed file into $line.
It is legal to intermix calls to gzread and gzreadline.
At this time gzreadline ignores the variable
$/
($INPUT_RECORD_SEPARATOR
or$RS
whenEnglish
is in use). The end of a line is denoted by the C character'\n'
. - $status = $gz->gzwrite($buffer) ;
-
Writes the contents of $buffer to the compressed file.
- $status = $gz->gzflush($flush) ;
-
Flushes all pending output into the compressed file. Works identically to the zlib function it interfaces to. Note that the use of gzflush can degrade compression.
Refer to the zlib documentation for the valid values of $flush.
- $gz->gzclose
-
Closes the compressed file. Any pending data is flushed to the file before it is closed.
- $gz->gzerror
-
Returns the zlib error message or number for the last operation associated with $gz. The return value will be the zlib error number when used in a numeric context and the zlib error message when used in a string context. The zlib error number constants, shown below, are available for use.
Z_OK Z_STREAM_END Z_ERRNO Z_STREAM_ERROR Z_DATA_ERROR Z_MEM_ERROR Z_BUF_ERROR
- $gzerrno
-
The $gzerrno scalar holds the error code associated with the most recent gzip routine. Note that unlike gzerror(), the error is not associated with a particular file.
As with gzerror() it returns an error number in numeric context and an error message in string context. Unlike gzerror() though, the error message will correspond to the zlib message when the error is associated with zlib itself, or the UNIX error message when it is not (i.e. zlib returned
Z_ERRORNO
).As there is an overlap between the error numbers used by zlib and UNIX, $gzerrno should only be used to check for the presence of an error in numeric context. Use gzerror() to check for specific zlib errors. The gzcat example below shows how the variable can be used safely.
Examples
Here 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 ;
$gz->gzclose() ;
}
Below is a script which makes use of gzreadline. It implements a simple very 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 ;
$gz->gzclose() ;
}
CHECKSUM FUNCTIONS
Two 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]) ;
CONSTANTS
All the zlib constants are automatically imported when you make use of Compress::Zlib.
AUTHOR
The Compress::Zlib module was written by Paul Marquess, pmarquess@bfsec.bt.co.uk.
The zlib compression library was written by Jean-loup Gailly gzip@prep.ai.mit.edu and Mark Adler madler@alumni.caltech.edu.
MODIFICATION HISTORY
0.1
First public release of Compress::Zlib.
Paul Marquess, 2nd October 1995.
0.2
Fixed a minor allocation problem in Zlib.xs
Paul Marquess, 5th October 1995.
0.3
Paul Marquess, 12th October 1995.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 213:
=cut found outside a pod block. Skipping to next block.