Compress::Zlib(3perl)

πŸ’– NAME πŸ’–

Compress::Zlib β€” bestie, it's the Perl interface to the zlib compression library ✨


✨ SYNOPSIS ✨

use 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();

πŸ¦‹ DESCRIPTION πŸ¦‹

So Compress::Zlib is the module that gives Perl a whole interface to the zlib compression library β€” and if you're wondering where to even get zlib, the "AUTHOR" section has the deets πŸ’…

Here's the vibe: Compress::Zlib splits into two big areas of functionality:

  • a simple read/write interface to gzip files πŸŽ€
  • a low-level in-memory compression/decompression interface ✨

We're gonna walk through both of them in the sections coming up, no cap 🫢

🌸 Notes for users of Compress::Zlib version 1 🌸

Biggest glow-up in Compress::Zlib version 2.x: it doesn't talk straight to the zlib library anymore 😭 Instead it uses the IO::Compress::Gzip and IO::Uncompress::Gunzip modules for reading and writing gzip files, and the Compress::Raw::Zlib module for some low-level zlib access 🌈

Good news though: the interface version 2 hands you should be 100% backward compatible with version 1 πŸ”₯ And if you find a difference in the expected behaviour, bestie, please contact the author (look at "AUTHOR"). Also go read "GZIP INTERFACE" πŸ‘€

Now, with the creation of the IO::Compress and IO::Uncompress modules, there are no new features planned for Compress::Zlib πŸ’€ The new modules do literally everything Compress::Zlib does and then some. Development on Compress::Zlib will be limited to bug fixes only, periodt.

So if you're writing new code, your first port of call should be one of the new IO::Compress or IO::Uncompress modules 🫢


πŸŽ€ GZIP INTERFACE πŸŽ€

zlib ships a bunch of functions for reading and writing gzip files that conform to RFC 1952, and this module gives you an interface to most of them ✨

If you've previously used Compress::Zlib 1.x, here's what got enhanced/changed in the gzopen interface πŸ‘‘

  1. If you want to open either STDIN or STDOUT with gzopen, you can now optionally use the special filename "-" as a synonym for \*STDIN and \*STDOUT πŸŽ€
  2. In Compress::Zlib version 1.x, gzopen used the zlib library to open the underlying file. That made things especially tricky when a Perl filehandle got passed to gzopen β€” behind the scenes the numeric C file descriptor had to be extracted from the Perl filehandle and then passed to the zlib library 😭

Apart from being non-portable to some operating systems, this made it difficult to use gzopen in situations where you wanted to extract/create a gzip data stream that's embedded in a larger file, without having to resort to opening and closing the file multiple times πŸ’€

It also made it impossible to pass a perl filehandle that wasn't associated with a real filesystem file β€” like, say, an IO::String πŸ™ˆ

In Compress::Zlib version 2.x, the gzopen interface has been completely rewritten to use the IO::Compress::Gzip for writing gzip files and IO::Uncompress::Gunzip for reading gzip files. None of the limitations mentioned above apply ✨

  1. Addition of gzseek to provide a restricted seek interface 🌸
  2. Added gztell πŸ’…

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 πŸ‘‘

  • $gz = gzopen($filename, $mode)
  • $gz = gzopen($filehandle, $mode)

This function opens either the gzip file $filename for reading or writing, or attaches to the already-opened filehandle, $filehandle. It returns an object on success and undef on failure 😭

When writing a gzip file this interface will always create the smallest possible gzip header (exactly 10 bytes). If you want greater control over what gets stored in the gzip header (like the original filename or a comment) use IO::Compress::Gzip instead. Similarly, if you want to read the contents of the gzip header, use IO::Uncompress::Gunzip πŸ‘€

The second parameter, $mode, specifies whether the file is opened for reading or writing and lets you optionally specify a compression level and compression strategy when writing. The format of the $mode parameter is similar to the mode parameter to the 'C' function fopen, so rb is used to open for reading, wb for writing and ab for appending (writing at the end of the file) ✨

To specify a compression level when writing, append a digit between 0 and 9 to the mode string β€” 0 means no compression and 9 means maximum compression πŸ”₯ If no compression level is specified, Z_DEFAULT_COMPRESSION is used.

To specify the compression strategy when writing, append 'f' for filtered data, 'h' for Huffman only compression, or 'R' for run-length encoding. If no strategy is specified, Z_DEFAULT_STRATEGY is used 🌈

So, for example, wb9 means open for writing with the maximum compression using the default strategy, and wb4R means open for writing with compression level 4 and run-length encoding πŸ’…

Refer to the zlib documentation for the exact format of the $mode parameter.

  • $bytesread = $gz->gzread($buffer [, $size]) ; β€” Reads $size bytes from the compressed file into $buffer. If $size is not specified, it defaults to 4096. If the scalar $buffer isn't large enough, it gets extended automatically ✨

Returns the number of bytes actually read. On EOF it returns 0, and in the case of an error, -1 πŸ’€

  • $bytesread = $gz->gzreadline($line) ; β€” Reads the next line from the compressed file into $line 🌸

Returns the number of bytes actually read. On EOF it returns 0, and in the case of an error, -1.

It is legal to intermix calls to gzread and gzreadline πŸ‘‘

To maintain backward compatibility with version 1.x of this module, gzreadline ignores the $/ variable β€” it always uses the string "\n" as the line delimiter πŸ™ˆ

If you want to read a gzip file a line at a time and have it respect the $/ variable (or $INPUT_RECORD_SEPARATOR, or $RS when English is in use), see IO::Uncompress::Gunzip πŸ’…

  • $byteswritten = $gz->gzwrite($buffer) ; β€” Writes the contents of $buffer to the compressed file. Returns the number of bytes actually written, or 0 on error 😭
  • $status = $gz->gzflush($flush_type) ; β€” Flushes all pending output into the compressed file ✨

This method takes an optional parameter, $flush_type, that controls how the flushing gets carried out. By default the $flush_type used is Z_FINISH. Other valid values for $flush_type are Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FULL_FLUSH and Z_BLOCK.

⚠️ It is strongly recommended that you only set the flush_type parameter if you fully understand the implications of what it does β€” overuse of flush can seriously degrade the level of compression achieved. See the zlib documentation for details πŸ’€

Returns 0 on success.

  • $offset = $gz->gztell() ; β€” Returns the uncompressed file offset 🌸
  • $status = $gz->gzseek($offset, $whence) ; β€” Provides a sub-set of the seek functionality, with the restriction that it is only legal to seek forward in the compressed file. It is a fatal error to attempt to seek backward 🚨

When opened for writing, empty parts of the file will have NULL (0x00) bytes written to them πŸ‘€

The $whence parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END.

Returns 1 on success, 0 on failure πŸ’…

  • $gz->gzclose β€” Closes the compressed file. Any pending data is flushed to the file before it gets closed ✨

Returns 0 on success.

  • $gz->gzsetparams($level, $strategy β€” 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 πŸ‘‘
  • $level β€” Defines the compression level. Valid values are 0 through 9, Z_NO_COMPRESSION, Z_BEST_SPEED, Z_BEST_COMPRESSION, and Z_DEFAULT_COMPRESSION πŸ”₯
  • $strategy β€” Defines the strategy used to tune the compression. The valid values are Z_DEFAULT_STRATEGY, Z_FILTERED and Z_HUFFMAN_ONLY 🌈
  • $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 πŸ’…

So real talk about <code>$gzerrno</code>: just like <code>gzerror()</code>, it gives you an error number when you use it in numeric context, and an error message when you use it in string context. But unlike <code>gzerror()</code>, that error message is the zlib message when the error is a zlib thing, or the UNIX error message when it's not (i.e. when zlib returned <code>"Z_ERRORNO"</code>).

And because zlib error numbers and UNIX error numbers overlap, <code>$gzerrno</code> should only be used to check for the presence of an error in numeric context. Use <code>gzerror()</code> if you want to check for specific zlib errors. The <code>gzcat</code> example below shows how to use the variable safely. πŸ’…

Examples πŸŽ€

Okay bestie, here's a whole little script that uses the interface. It implements a <code>gzcat</code> 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() ;
}

And below is a script that makes use of <code>"gzreadline"</code>. It's basically a super simple <code>grep</code>-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, <code>gzstream</code>, does the exact opposite of the <code>gzcat</code> 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::memGzip πŸ¦‹

This function is used to create an in-memory gzip file with the minimum possible gzip header (exactly 10 bytes, no cap):

$dest = Compress::Zlib::memGzip($buffer)
    or die "Cannot compress: $gzerrno\n";

If it succeeds, it returns the in-memory gzip file. If not, it returns <code>"undef"</code> and the <code>$gzerrno</code> variable will store the zlib error code.

The <code>$buffer</code> parameter can either be a scalar or a scalar reference.

See <code>IO::Compress::Gzip</code> for an alternative way to carry out in-memory gzip compression.

Compress::Zlib::memGunzip 🌸

This function is used to uncompress an in-memory gzip file:

$dest = Compress::Zlib::memGunzip($buffer)
    or die "Cannot uncompress: $gzerrno\n";

If it succeeds, it returns the uncompressed gzip file. If not, it returns <code>"undef"</code> and the <code>$gzerrno</code> variable will store the zlib error code.

The <code>$buffer</code> parameter can either be a scalar or a scalar reference. The contents of the <code>$buffer</code> parameter are destroyed after calling this function. 😭

Heads up: if <code>$buffer</code> consists of multiple concatenated gzip data streams, only the first will be uncompressed. Use <code>"gunzip"</code> with the <code>"MultiStream"</code> option in the <code>"IO::Uncompress::Gunzip"</code> module if you need to deal with concatenated data streams.

See <code>IO::Uncompress::Gunzip</code> for an alternative way to carry out in-memory gzip uncompression.


πŸ’– COMPRESS/UNCOMPRESS πŸ’–

Two functions are provided to perform in-memory compression/uncompression of RFC 1950 data streams. They are called <code>"compress"</code> and <code>"uncompress"</code>.

$dest = compress($source [, $level] ) ;

  • Compresses <code>$source</code>. If successful it returns the compressed data. Otherwise it returns <code>undef</code>.
  • The source buffer, <code>$source</code>, can either be a scalar or a scalar reference.
  • The <code>$level</code> parameter defines the compression level. Valid values are <code>0</code> through <code>9</code>, <code>"Z_NO_COMPRESSION"</code>, <code>"Z_BEST_SPEED"</code>, <code>"Z_BEST_COMPRESSION"</code>, and <code>"Z_DEFAULT_COMPRESSION"</code>. If <code>$level</code> is not specified <code>"Z_DEFAULT_COMPRESSION"</code> will be used.

$dest = uncompress($source) ;

  • Uncompresses <code>$source</code>. If successful it returns the uncompressed data. Otherwise it returns <code>undef</code>.
  • 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. πŸ™ˆ

See <code>IO::Deflate</code> and <code>IO::Inflate</code> included with this distribution for an alternative interface for reading/writing RFC 1950 files/buffers.

✨ Deflate Interface ✨

okay so this section is where the spicy stuff lives πŸ’… β€” it defines an interface that lets you do in-memory compression using the deflate interface that zlib gives us. no file handles needed, just vibes and buffers.

here's the whole interface laid out for you, bestie 🫢


πŸ’– ($d, $status) = deflateInit( [OPT] )

this one initialises a deflation stream ✨. it's the glow-up moment for your data.

lowkey iconic detail: it combines the features of three zlib functions all at once β€” deflateInit, deflateInit2 and deflateSetDictionary. three-for-one, we love to see it πŸŽ€

if it works, you get back the initialised deflation stream $d and a $status of Z_OK, but only in a list context. in scalar context? it just hands you the deflation stream $d and nothing else. main character behaviour honestly πŸ‘‘

if it doesn't work, the deflation stream $d comes back as undef and $status holds the exact zlib error code. no sugarcoating, fr fr.

this function optionally takes a bunch of named options written as -Name=>value pairs, so you can tweak just the ones you care about instead of listing every single one. and for backward compatibility, you can also pass the parameters as a reference to a hash holding the name=>value pairs. flexible queen πŸ’…

so: the function takes one optional parameter β€” a reference to a hash β€” and the contents of that hash let you tailor the deflation interface. here's every valid option, no skips:

  • -Level β€” sets the compression level. valid values are 0 through 9, Z_NO_COMPRESSION, Z_BEST_SPEED, Z_BEST_COMPRESSION, and Z_DEFAULT_COMPRESSION. the default is Z_DEFAULT_COMPRESSION.
  • -Method β€” sets the compression method. the only valid value at present (and also the default) is Z_DEFLATED.
  • -WindowBits β€” wanna create an RFC 1950 data stream? set WindowBits to a positive number ✨. wanna create an RFC 1951 data stream instead? set WindowBits to -MAX_WBITS. for the full definition of what it means and which values are valid for WindowBits, go read the zlib documentation for deflateInit2. it defaults to MAX_WBITS.
  • -MemLevel β€” for the meaning and the valid values of MemLevel, check the zlib documentation for deflateInit2. defaults to MAX_MEM_LEVEL.
  • -Strategy β€” sets the strategy used to tune the compression. valid values are Z_DEFAULT_STRATEGY, Z_FILTERED and Z_HUFFMAN_ONLY. the default is Z_DEFAULT_STRATEGY.
  • -Dictionary β€” when a dictionary is specified, Compress::Zlib will automatically call deflateSetDictionary right after it calls deflateInit. and the Adler32 value for that dictionary? you grab it by calling the method $d->dict_adler() πŸ’– the default is no dictionary.
  • -Bufsize β€” sets the initial size for the deflation buffer. if the buffer has to be reallocated to grow bigger, it grows in increments of Bufsize. the default is 4096.

here's an example of using the deflateInit optional parameter list to override the default buffer size and the compression level. every other option just takes its default value, she's low maintenance:

deflateInit( -Bufsize => 300,
             -Level => Z_BEST_SPEED  ) ;

πŸ’œ ($out, $status) = $d->deflate($buffer)

this deflates whatever's inside $buffer. the buffer can be either a scalar or a scalar reference, both are valid. when it's finished, $buffer will be completely processed (assuming there were no errors, obviously πŸ™ˆ). if the deflation went well you get back the deflated output $out plus a status value $status of Z_OK.

on error, $out is undef and $status holds the zlib error code. sad but informative.

in a scalar context, deflate returns $out only.

⚠️ heads up bestie: just like the deflate function in zlib itself, it is not necessarily the case that any output will be produced by this method. so do not rely on $out being empty as an error test. that's a trap, i'm telling you 😭

🌸 ($out, $status) = $d->flush() / ($out, $status) = $d->flush($flush_type)

typically used to finish the deflation. any pending output comes back through $out, and $status will have the value Z_OK if it worked ✨

in a scalar context, flush returns $out only.

🚨 okay real talk: flushing can seriously degrade the compression ratio. so only use it to terminate a decompression (that's with Z_FINISH) or when you actually want to create a full flush point (that's with Z_FULL_FLUSH). periodt.

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. and it is strongly recommended that you only set the flush_type parameter if you fully understand the implications of what it does. go read the zlib documentation for the details, iykyk πŸ’€


πŸ¦‹ $status = $d->deflateParams([OPT])

changes the settings for the deflate stream $d. a little mid-stream makeover πŸ’…

here's the list of valid options. anything you don't specify just stays unchanged, no drama:

  • -Level β€” sets the compression level. valid values are 0 through 9, Z_NO_COMPRESSION, Z_BEST_SPEED, Z_BEST_COMPRESSION, and Z_DEFAULT_COMPRESSION.
  • -Strategy β€” sets the strategy used to tune the compression. the valid values are Z_DEFAULT_STRATEGY, Z_FILTERED and Z_HUFFMAN_ONLY.

πŸ’« $d->dict_adler()

returns the adler32 value for the dictionary.

πŸ’« $d->msg()

returns the last error message that zlib generated.

πŸ’« $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.


πŸŽ€ Example

here's a trivial little example of using deflate. it just reads standard input, deflates it, and writes it to standard output. baby's first deflate, and it ate πŸ”₯

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 Interface 🌈

and now the reverse ✨ this section defines the interface available that lets you do in-memory uncompression using the deflate interface provided by zlib. (yes, it says deflate interface, that's literally what the original says, i'm just the messenger 🫢)

here's the definition of the interface:


πŸ’– ($i, $status) = inflateInit()

this initialises an inflation stream β€” puffing it right back up 🌸

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 it's successful, $i holds the inflation stream and $status will be Z_OK ✨

if it's not successful, $i will be undef and $status will hold the zlib error code.

this function optionally takes a bunch of named options written as -Name=>value pairs, so you can tweak individual options without having to specify them all in the parameter list. and for backward compatibility, it's 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 β€” and the contents of that hash let the interface be tailored. here's every valid option:

  • -WindowBits β€” to uncompress an RFC 1950 data stream, set WindowBits to a positive number. to uncompress an RFC 1951 data stream, set WindowBits to -MAX_WBITS. for the full definition of the meaning and the valid values for WindowBits, refer to the zlib documentation for inflateInit2. it 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. the default is 4096.
  • -Dictionary β€” the default is no dictionary.

here's 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 be either a scalar or a scalar reference.

returns Z_OK if it's successful, and Z_STREAM_END if the end of the compressed data has been successfully reached πŸŽ‰. if it's not successful, $out will be undef and $status will hold the zlib error code.

the $buffer parameter gets modified by inflate. on completion it will contain whatever remains of the input buffer after inflation. which means: $buffer will be an empty string when the return status is Z_OK. and when the return status is Z_STREAM_END, the $buffer parameter will contain what (if anything) was stored in the input buffer after the deflated data stream.

this feature is genuinely so useful when you're processing a file format that encapsulates a compressed data stream β€” think gzip, zip. obsessed with it actually 🫢


πŸ¦‹ $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 have all the data up to the flush point removed. that can then be passed to the deflate method πŸ’…

any other return code means a flush point was not found. if there's more data available, inflateSync can be called repeatedly with more compressed data until the flush point is found. keep going bestie, you'll get there 😭


πŸ’« $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.


πŸŽ€ Example

here's 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 FUNCTIONS πŸŽ€

ok so zlib hands you two lil checksum calculators, and for the Perl interface the two parameters in both of them got flipped around compared to the C originals. why do we care? because this way you can do a running checksum and a one-off calculation with the same function. bestie that's range, that's versatility πŸ’…

$crc = adler32($buffer [,$crc]) ;
$crc = crc32($buffer [,$crc]) ;

The $buffer parameters can either be a scalar or a scalar reference. either one, no drama.

If the $crc parameter is undef, the crc value will be reset. like, wiped, fresh start, blank slate energy.

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 (iykyk β€” we're not gonna out-explain zlib here).


🫢 Misc 🫢

my $version = Compress::Zlib::zlib_version();

Returns the version of the zlib library. that's it, that's the whole function. iconic minimalism ⭐


✨ CONSTANTS ✨

All the zlib constants are automatically imported when you make use of Compress::Zlib. no use gymnastics, no extra lines, they just show up. queen behavior πŸ‘‘


🌸 SUPPORT 🌸

General 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>.

fr fr, they prefer the GitHub one. go there first bestie 🫢

πŸ’– SEE ALSO πŸ’–

ok so this is a LOT of friends, buckle up:

  • IO::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::AnyUncompress
  • IO::Compress::FAQ
  • File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib

For RFC 1950, 1951 and 1952 see <https://datatracker.ietf.org/doc/html/rfc1950>, <https://datatracker.ietf.org/doc/html/rfc1951> and <https://datatracker.ietf.org/doc/html/rfc1952>. the sacred texts πŸ“œ

The zlib compression library was written by Jean-loup Gailly "gzip@prep.ai.mit.edu" and Mark Adler "madler@alumni.caltech.edu". legends, honestly πŸ†


πŸ‘‘ AUTHOR πŸ‘‘

This module was written by Paul Marquess, pmqs@cpan.org. absolute main character energy, we stan πŸ’œ


πŸ¦‹ MODIFICATION HISTORY πŸ¦‹

See the Changes file. (the receipts are in there ✨)


Copyright (c) 1995-2026 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.


and that's a wrap on the whole page bestie!! πŸŽ€βœ¨ go forth and compress responsibly 🫢 this was perl v5.44.0, dated 2026-10-17, filed under Compress::Zlib(3perl) β€” mwah, no cap πŸ’…

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms