GitXplorerGitXplorer
r

P6-Compress-Zlib

public
4 stars
5 forks
2 issues

Commits

List of commits on branch master.
Verified
4c8eecb7cfcf64ba84c1df54a92ddb6af5e56802

Merge pull request #19 from MasterDuke17/master

rretupmoca committed 6 years ago
Verified
6eef7dd4c050a8d00aa16d1c2c73f39764ab817c

Merge pull request #18 from raiph/patch-1

rretupmoca committed 6 years ago
Unverified
d14d9ea1a11bf84da28e5a216e64e73b35989c19

Use the faster Buf.append instead of ~=

MMasterDuke17 committed 6 years ago
Unverified
a1ed2e572d4937f1d02cb4007c7e7a42e1b5dfff

Cache the Encoder that Str.encode uses...

MMasterDuke17 committed 6 years ago
Unverified
cc85f20a831e05c1ee10f2af9cbb3cbd3121f494

Create buffers once and reallocate to clear them

MMasterDuke17 committed 6 years ago
Unverified
d0f231ffceb2daeeef8cc98502ec47fedaebc618

Use the new builtins Buf.(append|reallocate)

MMasterDuke17 committed 6 years ago

README

The README file for this repository.

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
  • Streaming (de)compression
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));
}
  • Miscellaneous Functions
use Compress::Zlib;

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

Handle Wrapper

  • zwrap($handle, :$gzip, :$zlib, :$deflate --> Compress::Zlib::Wrap)

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

  • gzslurp($filename, :$bin)

  • gzspurt($filename, $stuff, :$bin)

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.

  • new(:$gzip, :$zlib, :$deflate --> Compress::Zlib::Stream)

    Creates a new object that can be used for either compressing or decompressing (but not both!).

    Defaults to zlib compression.

  • finished( --> Bool)

    Returns true if the end of the data stream has been reached. In this case, you can do nothing more that's useful with this object.

  • bytes-left( --> Int)

    After an .inflate object is .finished, this will return the number of bytes in the last input buffer that were "past the end" of the compressed data stream.

    Usually used when you're interested in any data that comes after the compressed stream.

  • inflate(Buf $data --> Buf)

    Decompresses a chunk of data.

  • deflate(Buf $data --> Buf)

    Compresses a chunk of data.

  • flush( --> Buf)

    Currently just returns an empty Buf, as inflate and deflate are using Z_SYNC_FLUSH. To be future-proof, call this any time you need Z_SYNC_FLUSH semantics (sending a complete request to a server, for example).

    At some point, will cause zlib to flush some of it's internal data structures and possibly return more data.

  • finish( --> Buf)

    Flushes all remaining data from zlib's internal structures, and deallocates them.

    Call when you want a Z_STREAM_END to happen when compressing, or if you are finished with the object.

Misc Functions

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

  • compress(Blob $data, Int $level? --> Buf)

    Compresses binary $data. $level is the optional compression level (0 <= x <= 9); defaults to 6.

  • uncompress(Blob $data --> Buf)

    Uncompresses previously compressed $data.