NAME
Compress::Zlib - Interface to zlib compression library
SYNOPSIS
use Compress::Zlib ;
($d, $status) = deflateInit( [OPT] ) ;
($out, $status) = $d->deflate($buffer) ;
$status = $d->deflateParams([OPT]) ;
($out, $status) = $d->flush() ;
$d->dictadler() ;
$d->totalin() ;
$d->totalout() ;
$d->msg() ;
($i, $status) = inflateInit( [OPT] ) ;
($out, $status) = $i->inflate($buffer) ;
$status = $i->inflateSync($buffer) ;
$i->dictadler() ;
$i->totalin() ;
$i->totalout() ;
$i->msg() ;
$dest = compress($source, [$level]) ;
$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() ;
$status = $gz->gzeof() ;
$status = $gz->gzsetparams($level, $strategy) ;
$errstring = $gz->gzerror() ;
$gzerrno
$dest = Compress::Zlib::memGzip($buffer) ;
$dest = Compress::Zlib::memGunzip($buffer) ;
$crc = adler32($buffer [,$crc]) ;
$crc = crc32($buffer [,$crc]) ;
ZLIBVERSIONDESCRIPTION
The Compress::Zlib module provides a Perl interface to the zlib
compression library (see "AUTHOR" for details about where to get zlib). Most of the functionality provided by zlib is available inCompress::Zlib.
The module can be split into two general areas of functionality, namelyin-memory compression/decompression and read/write access to gzip
files. Each of these areas will be discussed separately below. DDEEFFLLAATTEEThe 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:(($$dd,, $$ssttaattuuss)) == ddeeffllaatteeIInniitt(( [[OOPPTT]] ))
Initialises a deflation stream. It combines the features of the zlib functions ddeeffllaatteeIInniitt, ddeeffllaatteeIInniitt22 and ddeeffllaatteeSSeettDDiiccttiioonnaarryy.If successful, it will return the initialised deflation stream, $$dd and
$$ssttaattuuss of "ZOK" in a list context. In scalar context it returns the
deflation stream, $$dd, only.
If not successful, the returned deflation stream ($$dd) will be undef and
$$ssttaattuuss will hold the exact zlib error code.
The function optionally takes a number of named options specified as"-Name=>value" pairs. This allows individual options to be tailored
without having to specify them all in the parameter list. For backward compatibility, 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:-LLeevveell
Defines the compression level. Valid values are 0 through 9, "ZNOCOMPRESSION", "ZBESTSPEED", "ZBESTCOMPRESSION", and "ZDEFAULTCOMPRESSION".The default is "-Level =>ZDEFAULTCOMPRESSION".
Method Defines the compression method. The only valid value at present(and the default) is "-Method =>ZDEFLATED".
-WWiinnddoowwBBiittss
For a definition of the meaning and valid values for WWiinnddoowwBBiittss refer to the zlib documentation for deflateInit2.Defaults to "-WindowBits =>MAXWBITS".
-MMeemmLLeevveell
For a definition of the meaning and valid values for MMeemmLLeevveell refer to the zlib documentation for deflateInit2.Defaults to "-MemLevel =>MAXMEMLEVEL".
-SSttrraatteeggyy
Defines the strategy used to tune the compression. The valid values are "ZDEFAULTSTRATEGY", "ZFILTERED" and "ZHUFFMANONLY".The default is "-Strategy =>ZDEFAULTSTRATEGY".
-DDiiccttiioonnaarryy
When a dictionary is specified Compress::Zlib will automatically
call ddeeffllaatteeSSeettDDiiccttiioonnaarryy directly after calling ddeeffllaatteeIInniitt. The Adler32 value for the dictionary can be obtained by calling themethod "$d-"dictadler()>.
The default is no dictionary.-BBuuffssiizzee
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 BBuuffssiizzee. The default is 4096. Here is an example of using the ddeeffllaatteeIInniitt optional parameter list to override the default buffer size and compression level. All other options will take their default values.deflateInit( -Bufsize => 300,
-Level => ZBESTSPEED ) ;
(($$oouutt,, $$ssttaattuuss)) == $$dd->>ddeeffllaattee(($$bbuuffffeerr))
Deflates the contents of $$bbuuffffeerr. The buffer can either be a scalar or
a scalar reference. When finished, $$bbuuffffeerr will be completely
processed (assuming there were no errors). If the deflation wassuccessful it returns the deflated output, $$oouutt, and a status value,
$$ssttaattuuss, of "ZOK".
On error, $$oouutt will be undef and $$ssttaattuuss will contain the zlib error
code.In a scalar context ddeeffllaattee will return $$oouutt 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 thefact that $$oouutt is empty for an error test.
(($$oouutt,, $$ssttaattuuss)) == $$dd->>fflluusshh(([[fflluusshhttyyppee]]))
Typically used to finish the deflation. Any pending output will bereturned via $$oouutt. $$ssttaattuuss will have a value "ZOK" if successful.
In a scalar context fflluusshh will return $$oouutt only.
Note that flushing can seriously degrade the compression ratio, so it should only be used to terminate a decompression (using "ZFINISH") or when you want to create a full flush point (using "ZFULLFLUSH"). By default the "flushtype" used is "ZFINISH". Other valid values for "flushtype" are "ZNOFLUSH", "ZPARTIALFLUSH", "ZSYNCFLUSH" and "ZFULLFLUSH". It is strongly recommended that you only set the "flushtype" parameter if you fully understand the implications of what it does. See the "zlib" documentation for details.$$ssttaattuuss == $$dd->>ddeeffllaatteePPaarraammss(([[OOPPTT]]))
Change settings for the deflate stream $d.
The list of the valid options is shown below. Options not specified will remain unchanged.-LLeevveell
Defines the compression level. Valid values are 0 through 9, "ZNOCOMPRESSION", "ZBESTSPEED", "ZBESTCOMPRESSION", and "ZDEFAULTCOMPRESSION".-SSttrraatteeggyy
Defines the strategy used to tune the compression. The valid values are "ZDEFAULTSTRATEGY", "ZFILTERED" and "ZHUFFMANONLY".$$dd->>ddiiccttaaddlleerr(())
Returns the adler32 value for the dictionary.$$dd->>mmssgg(())
Returns the last error message generated by zlib.$$dd->>ttoottaalliinn(())
Returns the total number of bytes uncompressed bytes input to deflate.$$dd->>ttoottaalloouutt(())
Returns the total number of compressed bytes output from deflate. EExxaammppllee Here is a trivial example of using ddeeffllaattee. It simply reads standard input, deflates it and writes it to standard output. use strict ; use warnings ;use Compress::Zlib ;
binmode STDIN; binmode STDOUT;my $x = deflateInit()
or die "Cannot create a deflation stream\n" ;my ($output, $status) ;
while (<>) {($output, $status) = $x->deflate($) ;
$status == ZOK
or die "deflation failed\n" ;print $output ;
}($output, $status) = $x->flush() ;
$status == ZOK
or die "deflation failed\n" ;print $output ;
IINNFFLLAATTEE Here is a definition of the interface:(($$ii,, $$ssttaattuuss)) == iinnffllaatteeIInniitt(())
Initialises an inflation stream.In a list context it returns the inflation stream, $$ii, and the zlib
status code ($$ssttaattuuss). In a scalar context it returns the inflation
stream only.If successful, $$ii will hold the inflation stream and $$ssttaattuuss will be
"ZOK".If not successful, $$ii will be undef and $$ssttaattuuss will hold the zlib
error code. The function optionally takes a number of named options specified as"-Name=>value" pairs. This allows individual options to be tailored
without having to specify them all in the parameter list. For backward compatibility, 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:-WWiinnddoowwBBiittss
For a definition of the meaning and valid values for WWiinnddoowwBBiittss refer to the zlib documentation for inflateInit2.Defaults to "-WindowBits =>MAXWBITS".
-BBuuffssiizzee
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 BBuuffssiizzee. Default is 4096.-DDiiccttiioonnaarryy
The default is no dictionary. Here is an example of using the iinnffllaatteeIInniitt optional parameter to override the default buffer size.inflateInit( -Bufsize => 300 ) ;
(($$oouutt,, $$ssttaattuuss)) == $$ii->>iinnffllaattee(($$bbuuffffeerr))
Inflates the complete contents of $$bbuuffffeerr. The buffer can either be a
scalar or a scalar reference. Returns "ZOK" if successful and "ZSTREAMEND" if the end of thecompressed data has been successfully reached. If not successful, $$oouutt
will be undef and $$ssttaattuuss will hold the zlib error code.
The $buffer parameter is modified by "inflate". On completion it will
contain what remains of the input buffer after inflation. This meansthat $buffer will be an empty string when the return status is "ZOK".
When the return status is "ZSTREAMEND" the $buffer parameter will
contains what (if anything) was stored in the input buffer after the deflated data stream. This feature is useful when processing a file format that encapsulates a compressed data stream (e.g. gzip, zip).$$ssttaattuuss == $$ii->>iinnffllaatteeSSyynncc(($$bbuuffffeerr))
Scans $buffer until it reaches either a full flush point or the end of
the buffer.If a full flush point is found, "ZOK" is returned and $buffer will be
have all data up to the flush point removed. This can then be passed to the "deflate" method. Any other return code means that a flush point was not found. If more data is available, "inflateSync" can be called repeatedly with more compressed data until the flush point is found.$$ii->>ddiiccttaaddlleerr(())
Returns the adler32 value for the dictionary.$$ii->>mmssgg(())
Returns the last error message generated by zlib.$$ii->>ttoottaalliinn(())
Returns the total number of bytes compressed bytes input to inflate.$$ii->>ttoottaalloouutt(())
Returns the total number of uncompressed bytes output from inflate. EExxaammppllee Here is an example of using iinnffllaattee. use strict ; use warnings ;use Compress::Zlib ;
my $x = inflateInit()
or die "Cannot create a inflation stream\n" ;my $input = '' ;
binmode STDIN; binmode STDOUT;my ($output, $status) ;
while (read(STDIN, $input, 4096))
{($output, $status) = $x->inflate(\$input) ;
print $output
if $status == ZOK or $status == ZSTREAMEND ;
last if $status != ZOK ;
} die "inflation failed\n"unless $status == ZSTREAMEND ;
CCOOMMPPRREESSSS//UUNNCCOOMMPPRREESSSSTwo high-level functions are provided by zlib to perform in-memory
compression/uncompression of RFC1950 data streams. They are called ccoommpprreessss and uunnccoommpprreessss. The two Perl subs defined below provide the equivalent functionality.$$ddeesstt == ccoommpprreessss(($$ssoouurrccee [[,, $$lleevveell]] )) ;;
Compresses $$ssoouurrccee. If successful it returns the compressed data.
Otherwise it returns undef. The source buffer can either be a scalar or a scalar reference.The $$lleevveell paramter defines the compression level. Valid values
are 0 through 9, "ZNOCOMPRESSION", "ZBESTSPEED","ZBESTCOMPRESSION", and "ZDEFAULTCOMPRESSION". If $$lleevveell is
not specified "ZDEFAULTCOMPRESSION" will be used.$$ddeesstt == uunnccoommpprreessss(($$ssoouurrccee)) ;;
Uncompresses $$ssoouurrccee. If successful it returns the uncompressed
data. Otherwise it returns undef. The source buffer can either be a scalar or a scalar reference. Please note: the two functions defined above are not compatible with the Unix commands of the same name. GGZZIIPP IINNTTEERRFFAACCEE 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.$$ggzz == ggzzooppeenn((ffiilleennaammee oorr ffiilleehhaannddllee,, mmooddee))
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 mmooddee 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 mmooddee parameter. If a reference to an open filehandle is passed in place of the filename, gzdopen will be called behind the scenes. The third example at the end of this section, gzstream, uses this feature.$$bbyytteessrreeaadd == $$ggzz->>ggzzrreeaadd(($$bbuuffffeerr [[,, $$ssiizzee]])) ;;
Reads $$ssiizzee bytes from the compressed file into $$bbuuffffeerr. If $$ssiizzee
is not specified, it will default to 4096. If the scalar $$bbuuffffeerr
is not large enough, it will be extended automatically. Returns the number of bytes actually read. On EOF it returns 0 andin the case of an error, -1.
$$bbyytteessrreeaadd == $$ggzz->>ggzzrreeaaddlliinnee(($$lliinnee)) ;;
Reads the next line from the compressed file into $$lliinnee.
Returns the number of bytes actually read. On EOF it returns 0 andin the case of an error, -1.
It is legal to intermix calls to ggzzrreeaadd and ggzzrreeaaddlliinnee.At this time ggzzrreeaaddlliinnee ignores the variable $/
($INPUTRECORDSEPARATOR or $RS when "English" is in use). The end
of a line is denoted by the C character '\n'.$$bbyytteesswwrriitttteenn == $$ggzz->>ggzzwwrriittee(($$bbuuffffeerr)) ;;
Writes the contents of $$bbuuffffeerr to the compressed file. Returns the
number of bytes actually written, or 0 on error.$$ssttaattuuss == $$ggzz->>ggzzfflluusshh(($$fflluusshh)) ;;
Flushes all pending output to the compressed file. Works identically to the zlib function it interfaces to. Note that the use of ggzzfflluusshh can degrade compression.Returns "ZOK" if $$fflluusshh is "ZFINISH" and all output could be
flushed. Otherwise the zlib error code is returned.Refer to the zlib documentation for the valid values of $$fflluusshh.
$$ssttaattuuss == $$ggzz->>ggzzeeooff(()) ;;
Returns 1 if the end of file has been detected while reading the input file, otherwise returns 0.$$ggzz->>ggzzcclloossee
Closes the compressed file. Any pending data is flushed to the file before it is closed.$$ggzz->>ggzzsseettppaarraammss(($$lleevveell,, $$ssttrraatteeggyy
Change settings for the deflate stream $gz.
The list of the valid options is shown below. Options not specified will remain unchanged. Note: This method is only available if you are running zlib 1.0.6 or better.$$lleevveell
Defines the compression level. Valid values are 0 through 9, "ZNOCOMPRESSION", "ZBESTSPEED", "ZBESTCOMPRESSION", and "ZDEFAULTCOMPRESSION".$$ssttrraatteeggyy
Defines the strategy used to tune the compression. The valid values are "ZDEFAULTSTRATEGY", "ZFILTERED" and "ZHUFFMANONLY".$$ggzz->>ggzzeerrrroorr
Returns the zlib error message or number for the last operationassociated with $$ggzz. 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. ZOK ZSTREAMEND ZERRNOZSTREAMERROR
ZDATAERROR
ZMEMERROR
ZBUFERROR
$$ggzzeerrrrnnoo
The $$ggzzeerrrrnnoo scalar holds the error code associated with the most
recent gzip routine. Note that unlike ggzzeerrrroorr(()), the error is not associated with a particular file. As with ggzzeerrrroorr(()) it returns an error number in numeric context and an error message in string context. Unlike ggzzeerrrroorr(()) though, the error message will correspond to the zlib message when the error is associated with zlib itself, or the UNIX error messagewhen it is not (i.e. zlib returned "ZERRORNO").
As there is an overlap between the error numbers used by zlib andUNIX, $$ggzzeerrrrnnoo should only be used to check for the presence of an
error in numeric context. Use ggzzeerrrroorr(()) to check for specific zlib errors. The gzcat example below shows how the variable can be used safely. EExxaammpplleess Here is an example script which uses the interface. It implements a gzcat function. use strict ; use warnings ;use Compress::Zlib ;
die "Usage: gzcat file...\n" unless @ARGV ;my $file ;
foreach $file (@ARGV) {
my $buffer ;
my $gz = gzopen($file, "rb")
or die "Cannot open $file: $gzerrno\n" ;
print $buffer while $gz->gzread($buffer) > 0 ;
die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n"
if $gzerrno != ZSTREAMEND ;
$gz->gzclose() ;
} Below is a script which makes use of ggzzrreeaaddlliinnee. It implements a very simple grep like script. use strict ; use warnings ;use Compress::Zlib ;
die "Usage: gzgrep pattern file...\n" unless @ARGV >= 2;my $pattern = shift ;
my $file ;
foreach $file (@ARGV) {
my $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 != ZSTREAMEND ;
$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 strict ; use warnings ;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 ;
CCoommpprreessss::::ZZlliibb::::mmeemmGGzziippThis 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. CCoommpprreessss::::ZZlliibb::::mmeemmGGuunnzziippThis function is used to uncompress an in-memory gzip file.
$dest = Compress::Zlib::memGunzip($buffer) ;
If successful, it returns the uncompressed gzip file, otherwise it returns undef. The buffer parameter can either be a scalar or a scalar reference. The contents of the buffer parameter are destroyed after calling this function. CCHHEECCKKSSUUMM FFUUNNCCTTIIOONNSS 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]) ;
The buffer parameters can either be a scalar or a scalar reference.If the $crc parameters is "undef", the crc value will be reset.
FFAAQQ CCoommppaattiibbiilliittyy wwiitthh UUnniixx ccoommpprreessss//uunnccoommpprreessss..Although "Compress::Zlib" has a pair of functions called "compress" and
"uncompress", they are not the same as the Unix programs of the samename. The "Compress::Zlib" library is not compatable with Unix
"compress". If you have the "uncompress" program available, you can use this to read compressed filesopen F, "uncompress -c $filename |";
while () { ... If you have the "gunzip" program available, you can use this to read compressed files open F, "gunzip -c $filename |";
while () { ... and this to write compress files if you have the "compress" program available open F, "| compress -c $filename ";
print F "data"; ... close F ; AAcccceessssiinngg ..ttaarr..ZZ ffiilleessThe "Archive::Tar" module can optionally use "Compress::Zlib" (via the
"IO::Zlib" module) to access tar files that have been compressed with "gzip". Unfortunately tar files compressed with the Unix "compress"utility cannot be read by "Compress::Zlib" and so cannot be directly
accesses by "Archive::Tar". If the "uncompress" or "gunzip" programs are available, you can use one of these workarounds to read ".tar.Z" files from "Archive::Tar" Firstly with "uncompress" use strict; use warnings; use Archive::Tar;open F, "uncompress -c $filename |";
my $tar = Archive::Tar->new(*F);
... and this with "gunzip" use strict; use warnings; use Archive::Tar;open F, "gunzip -c $filename |";
my $tar = Archive::Tar->new(*F);
... Similarly, if the "compress" program is available, you can use this to write a ".tar.Z" file use strict; use warnings; use Archive::Tar; use IO::File;my $fh = newIO::File "| compress -c >$filename";
my $tar = Archive::Tar->new();
...$tar->write($fh);
$fh->close ;
AAcccceessssiinngg ZZIIPP FFiilleess Although it is possible to use this module to access .zip files, there is a module on CPAN that will do all the hard work for you. Check outhttp://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz
Assuming you don't 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. 1. When calling iinnffllaatteeIInniitt or ddeeffllaatteeIInniitt the WWiinnddoowwBBiittss parametermust be set to "-MAXWBITS". This disables the creation of the
zlib header. 2. The zlib function iinnffllaattee, and so the iinnffllaattee method supplied in this module, assume that there is at least one trailing byte after the compressed data stream. Normally this isn't a problem because both the gzip and zip file formats will guarantee that there is data directly after the compressed data stream. CCOONNSSTTAANNTTSS All the zlib constants are automatically imported when you make use ofCompress::Zlib.
AUTHORThe Compress::Zlib module was written by Paul Marquess, pmqs@cpan.org.
The latest copy of the module can be found on CPAN inmodules/by-module/Compress/Compress-Zlib-x.x.tar.gz.
The primary site for the zlib compression library is http://www.zlib.org. MODIFICATION HISTORY See the Changes file.perl v5.8.8 2007-09-23 Zlib(3)