![]() |
![]()
| ![]() |
![]()
NAMECompress::Zlib - Interface to zlib compression librarySYNOPSISuse Compress::Zlib ; ($d, $status) = deflateInit( [OPT] ) ; $status = $d->deflate($input, $output) ; $status = $d->flush([$flush_type]) ; $d->deflateParams(OPTS) ; $d->deflateTune(OPTS) ; $d->dict_adler() ; $d->crc32() ; $d->adler32() ; $d->total_in() ; $d->total_out() ; $d->msg() ; $d->get_Strategy(); $d->get_Level(); $d->get_BufSize(); ($i, $status) = inflateInit( [OPT] ) ; $status = $i->inflate($input, $output [, $eof]) ; $status = $i->inflateSync($input) ; $i->dict_adler() ; $d->crc32() ; $d->adler32() ; $i->total_in() ; $i->total_out() ; $i->msg() ; $d->get_BufSize(); $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) ; $offset = $gz->gztell() ; $status = $gz->gzseek($offset, $whence) ; $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]) ; $crc = crc32_combine($crc1, $crc2, $len2); $adler = adler32_combine($adler1, $adler2, $len2); my $version = Compress::Raw::Zlib::zlib_version(); DESCRIPTIONThe Compress::Zlib module provides a Perl interface to the zlib compression library (see "AUTHOR" for details about where to get zlib).The "Compress::Zlib" module can be split into two general areas of functionality, namely a simple read/write interface to gzip files and a low-level in-memory compression/decompression interface. Each of these areas will be discussed in the following sections. Notes for users of Compress::Zlib version 1The main change in "Compress::Zlib" version 2.x is that it does not now interface directly to the zlib library. Instead it uses the "IO::Compress::Gzip" and "IO::Uncompress::Gunzip" modules for reading/writing gzip files, and the "Compress::Raw::Zlib" module for some low-level zlib access.The interface provided by version 2 of this module should be 100% backward compatible with version 1. If you find a difference in the expected behaviour please contact the author (See "AUTHOR"). See "GZIP INTERFACE" With the creation of the "IO::Compress" and "IO::Uncompress" modules no new features are planned for "Compress::Zlib" - the new modules do everything that "Compress::Zlib" does and then some. Development on "Compress::Zlib" will be limited to bug fixes only. If you are writing new code, your first port of call should be one of the new "IO::Compress" or "IO::Uncompress" modules. GZIP INTERFACEA number of functions are supplied in zlib for reading and writing gzip files that conform to RFC 1952. This module provides an interface to most of them.If you have previously used "Compress::Zlib" 1.x, the following enhancements/changes have been made to the "gzopen" interface:
A more complete and flexible interface for reading/writing gzip files/buffers is included with the module "IO-Compress-Zlib". See IO::Compress::Gzip and IO::Uncompress::Gunzip for more details.
ExamplesHere is an example script which uses the interface. It implements a gzcat function.use strict ; use warnings ; use Compress::Zlib ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $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 != Z_STREAM_END ; $gz->gzclose() ; } Below is a script which makes use of "gzreadline". It implements a very simple grep like script. use strict ; use warnings ; use Compress::Zlib ; die "Usage: gzgrep pattern [file...]\n" unless @ARGV >= 1; my $pattern = shift ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $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 != 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 data stream 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 ; Compress::Zlib::memGzipThis function is used to create an in-memory gzip file with the minimum possible gzip header (exactly 10 bytes).$dest = Compress::Zlib::memGzip($buffer) or die "Cannot compress: $gzerrno\n"; If successful, it returns the in-memory gzip file. Otherwise it returns "undef" and the $gzerrno variable will store the zlib error code. The $buffer parameter can either be a scalar or a scalar reference. See IO::Compress::Gzip for an alternative way to carry out in-memory gzip compression. Compress::Zlib::memGunzipThis function is used to uncompress an in-memory gzip file.$dest = Compress::Zlib::memGunzip($buffer) or die "Cannot uncompress: $gzerrno\n"; If successful, it returns the uncompressed gzip file. Otherwise it returns "undef" and the $gzerrno variable will store the zlib error code. The $buffer parameter can either be a scalar or a scalar reference. The contents of the $buffer parameter are destroyed after calling this function. If $buffer consists of multiple concatenated gzip data streams only the first will be uncompressed. Use "gunzip" with the "MultiStream" option in the "IO::Uncompress::Gunzip" module if you need to deal with concatenated data streams. See IO::Uncompress::Gunzip for an alternative way to carry out in-memory gzip uncompression. COMPRESS/UNCOMPRESSTwo functions are provided to perform in-memory compression/uncompression of RFC 1950 data streams. They are called "compress" and "uncompress".
Please note: the two functions defined above are not compatible with the Unix commands of the same name. See IO::Deflate and IO::Inflate included with this distribution for an alternative interface for reading/writing RFC 1950 files/buffers. Deflate InterfaceThis section defines an interface that allows in-memory compression using the deflate interface provided by zlib.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 "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 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:
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 "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() =head2 ($out, $status) = $d->flush($flush_type)Typically used to finish 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 seriously degrade the compression ratio, so it should only be used to terminate a decompression (using "Z_FINISH") or when you want to create a full flush point (using "Z_FULL_FLUSH"). By default the "flush_type" used is "Z_FINISH". Other valid values for "flush_type" are "Z_NO_FLUSH", "Z_PARTIAL_FLUSH", "Z_SYNC_FLUSH" and "Z_FULL_FLUSH". It is strongly recommended that you only set the "flush_type" parameter if you fully understand the implications of what it does. See the "zlib" documentation for details. $status = $d->deflateParams([OPT])Change settings for the deflate stream $d.The list of the valid options is shown below. Options not specified will remain unchanged.
$d->dict_adler()Returns the adler32 value for the dictionary.$d->msg()Returns the last error message generated by zlib.$d->total_in()Returns the total number of bytes uncompressed bytes input to deflate.$d->total_out()Returns the total number of compressed bytes output from deflate.ExampleHere is a trivial example of using "deflate". 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 == Z_OK or die "deflation failed\n" ; print $output ; } ($output, $status) = $x->flush() ; $status == Z_OK or die "deflation failed\n" ; print $output ; Inflate InterfaceThis section defines the interface available that allows in-memory uncompression using the deflate interface provided by zlib.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 in $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 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:
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 "Z_OK" if successful and "Z_STREAM_END" if the end of the compressed data has been successfully reached. If not successful, $out will be undef and $status 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 means that $buffer will be an empty string when the return status is "Z_OK". When the return status is "Z_STREAM_END" 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). $status = $i->inflateSync($buffer)Scans $buffer until it reaches either a full flush point or the end of the buffer.If a full flush point is found, "Z_OK" 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. $i->dict_adler()Returns the adler32 value for the dictionary.$i->msg()Returns the last error message generated by zlib.$i->total_in()Returns the total number of bytes compressed bytes input to inflate.$i->total_out()Returns the total number of uncompressed bytes output from inflate.ExampleHere is an example of using "inflate".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 == Z_OK or $status == Z_STREAM_END ; last if $status != Z_OK ; } die "inflation failed\n" unless $status == Z_STREAM_END ; CHECKSUM FUNCTIONSTwo functions are provided by zlib to calculate checksums. 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. If you have built this module with zlib 1.2.3 or better, two more CRC-related functions are available. $crc = crc32_combine($crc1, $crc2, $len2); $adler = adler32_combine($adler1, $adler2, $len2); These functions allow checksums to be merged. Refer to the zlib documentation for more details. Miscmy $version = Compress::Zlib::zlib_version();Returns the version of the zlib library.CONSTANTSAll the zlib constants are automatically imported when you make use of Compress::Zlib.SUPPORTGeneral feedback/questions/bug reports should be sent to <https://github.com/pmqs/IO-Compress/issues> (preferred) or <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.SEE ALSOIO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Compress::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd, IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompressIO::Compress::FAQ File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib For RFC 1950, 1951 and 1952 see <http://www.faqs.org/rfcs/rfc1950.html>, <http://www.faqs.org/rfcs/rfc1951.html> and <http://www.faqs.org/rfcs/rfc1952.html> The zlib compression library was written by Jean-loup Gailly "gzip@prep.ai.mit.edu" and Mark Adler "madler@alumni.caltech.edu". The primary site for the zlib compression library is <http://www.zlib.org>. The primary site for gzip is <http://www.gzip.org>. AUTHORThis module was written by Paul Marquess, "pmqs@cpan.org".MODIFICATION HISTORYSee the Changes file.COPYRIGHT AND LICENSECopyright (c) 1995-2021 Paul Marquess. All rights reserved.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|