Rand Stats



Lempel–Ziv–Welch (LZW) algorithm implementation in Raku


my int $r = 0;
my str $d = 'Тексты - это не энциклопедические и не лингвистические системы.' x 255;

# create LWZ object with default dictionary size (97000)
my $lzw = LZW::Revolunet.new;

# OR create LWZ object with user defined dictionary size (be careful)
my $lzw = LZW::Revolunet.new(:dictsize(200000));

# compress
my $cmp  = $lzw.compress(:s($lzw.encode_utf8(:s($d))));

# get compression ratio
$r = $lzw.get_ratio;

# decompress
my $dcmp = $lzw.decode_utf8(:s($lzw.decompress(:s($cmp))));

# validate
if !($dcmp eq $d) {
    die 'decompressed data is corrupted';

# compress/decompress statistics
('compression ratio ' ~ $r ~ '%').say;


LZW::RevolunetRaku implementation of universal lossless data compression algorithm created by Abraham Lempel, Jacob Ziv, and Terry Welch. This module is based on JavaScript implementation (lzw_encoder.js) by Julien Bouquillon


LZW::Revolunet is free and opensource software, so you can redistribute it and/or modify it under the terms of the The Artistic License 2.0.


Please contact me via LinkedIn or Twitter. Your feedback is welcome at narkhov.pro.

See also

LZW Data Compression