Rand Stats

Compress::Zlib

github:retupmoca

Compress::Zlib

This is a (hopefully) nice interface to zlib. It compresses and uncompresses data using zlib native library.

Usage

use Compress::Zlib;

my $wrapped = Compress::Zlib::Wrap.new($handle); # can be a socket, filehandle, etc
my $wrapped = zwrap($handle); # does the same thing as the above line

$wrapped.send("data");
my $response = $wrapped.get;

gzslurp("file.gz"); # reads in a gzipped file
gzspurt("file.gz", "stuff"); # spits out a gzipped file
use Compress::Zlib;

my $compressor = Compress::Zlib::Stream.new;
loop {
    $socket.write($compressor.deflate($data-chunk));
}
$socket.write($compressor.finish);

my $decompressor = Compress::Zlib::Stream.new;
while !$decompressor.finished {
    my $data-chunk = $decompressor.inflate($socket.read($size));
}
use Compress::Zlib;

my $compressed = compress($string.encode('utf8'));
my $original = uncompress($compressed).decode('utf8');

Handle Wrapper

Returns a wrapped handle that will read and write data in the compressed format. For example:

use Compress::Zlib;

my $file   = "data.txt.gz";
my $handle = try open $file or die "Error reading $file: $!";
my $zwrap  = zwrap($handle, :gzip);

for $zwrap.lines {
    .print
}

$handle.close;

CATCH { default { die "Error reading $file: $_" } }

Slurp/spurt

Stream Class

This currently has very few options. Over time, I will add support for custom compression levels, gzip/raw deflate streams, etc. If you need a specific feature, open an issue and I will move it to the top of my priority list.

Misc Functions

NOTE: These only handle zlib format data, not gzip or deflate.