123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911 |
- #!/usr/bin/perl
- # Author: Trizen
- # Date: 15 June 2023
- # Edit: 19 March 2024
- # https://github.com/trizen
- # Compress/decompress files using Burrows-Wheeler Transform (BWT) + LZ77 compression (LZHD variant) + Arithmetic Coding (in fixed bits).
- # Encoding the distances using a DEFLATE-like approach.
- # References:
- # Data Compression (Summer 2023) - Lecture 11 - DEFLATE (gzip)
- # https://youtube.com/watch?v=SJPvNi4HrWQ
- #
- # Data Compression (Summer 2023) - Lecture 13 - BZip2
- # https://youtube.com/watch?v=cvoZbBZ3M2A
- #
- # Basic arithmetic coder in C++
- # https://github.com/billbird/arith32
- use 5.036;
- use Getopt::Std qw(getopts);
- use File::Basename qw(basename);
- use List::Util qw(max uniq sum);
- use constant {
- PKGNAME => 'BWLZA2',
- VERSION => '0.01',
- FORMAT => 'bwlza2',
- CHUNK_SIZE => 1 << 17, # higher value = better compression
- LOOKAHEAD_LEN => 128,
- };
- # Arithmetic Coding settings
- use constant BITS => 32;
- use constant MAX => oct('0b' . ('1' x BITS));
- # Container signature
- use constant SIGNATURE => uc(FORMAT) . chr(1);
- # [distance value, offset bits]
- my @DISTANCE_SYMBOLS = map { [$_, 0] } (0 .. 4);
- until ($DISTANCE_SYMBOLS[-1][0] > CHUNK_SIZE) {
- push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (4 / 3)), $DISTANCE_SYMBOLS[-1][1] + 1];
- push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (3 / 2)), $DISTANCE_SYMBOLS[-1][1]];
- }
- my @DISTANCE_INDICES;
- foreach my $i (0 .. $#DISTANCE_SYMBOLS) {
- my ($min, $bits) = @{$DISTANCE_SYMBOLS[$i]};
- foreach my $k ($min .. $min + (1 << $bits) - 1) {
- last if ($k > CHUNK_SIZE);
- $DISTANCE_INDICES[$k] = $i;
- }
- }
- sub usage {
- my ($code) = @_;
- print <<"EOH";
- usage: $0 [options] [input file] [output file]
- options:
- -e : extract
- -i <filename> : input filename
- -o <filename> : output filename
- -r : rewrite output
- -v : version number
- -h : this message
- examples:
- $0 document.txt
- $0 document.txt archive.${\FORMAT}
- $0 archive.${\FORMAT} document.txt
- $0 -e -i archive.${\FORMAT} -o document.txt
- EOH
- exit($code // 0);
- }
- sub version {
- printf("%s %s\n", PKGNAME, VERSION);
- exit;
- }
- sub valid_archive {
- my ($fh) = @_;
- if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
- $sig eq SIGNATURE || return;
- }
- return 1;
- }
- sub main {
- my %opt;
- getopts('ei:o:vhr', \%opt);
- $opt{h} && usage(0);
- $opt{v} && version();
- my ($input, $output) = @ARGV;
- $input //= $opt{i} // usage(2);
- $output //= $opt{o};
- my $ext = qr{\.${\FORMAT}\z}io;
- if ($opt{e} || $input =~ $ext) {
- if (not defined $output) {
- ($output = basename($input)) =~ s{$ext}{}
- || die "$0: no output file specified!\n";
- }
- if (not $opt{r} and -e $output) {
- print "'$output' already exists! -- Replace? [y/N] ";
- <STDIN> =~ /^y/i || exit 17;
- }
- decompress_file($input, $output)
- || die "$0: error: decompression failed!\n";
- }
- elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
- $output //= basename($input) . '.' . FORMAT;
- compress_file($input, $output)
- || die "$0: error: compression failed!\n";
- }
- else {
- warn "$0: don't know what to do...\n";
- usage(1);
- }
- }
- sub lz77_compression ($str, $uncompressed, $indices, $lengths) {
- my $la = 0;
- my $prefix = '';
- my @chars = split(//, $str);
- my $end = $#chars;
- while ($la <= $end) {
- my $n = 1;
- my $p = length($prefix);
- my $tmp;
- my $token = $chars[$la];
- while ( $n < 255
- and $la + $n <= $end
- and ($tmp = rindex($prefix, $token, $p)) >= 0) {
- $p = $tmp;
- $token .= $chars[$la + $n];
- ++$n;
- }
- --$n;
- push @$indices, $la - $p;
- push @$lengths, $n;
- push @$uncompressed, ord($chars[$la + $n]);
- $la += $n + 1;
- $prefix .= $token;
- }
- return;
- }
- sub lz77_decompression ($uncompressed, $indices, $lengths) {
- my $chunk = '';
- my $offset = 0;
- foreach my $i (0 .. $#{$uncompressed}) {
- $chunk .= substr($chunk, $offset - $indices->[$i], $lengths->[$i]) . chr($uncompressed->[$i]);
- $offset += $lengths->[$i] + 1;
- }
- return $chunk;
- }
- sub read_bit ($fh, $bitstring) {
- if (($$bitstring // '') eq '') {
- $$bitstring = unpack('b*', getc($fh) // return undef);
- }
- chop($$bitstring);
- }
- sub read_bits ($fh, $bits_len) {
- my $data = '';
- read($fh, $data, $bits_len >> 3);
- $data = unpack('B*', $data);
- while (length($data) < $bits_len) {
- $data .= unpack('B*', getc($fh) // return undef);
- }
- if (length($data) > $bits_len) {
- $data = substr($data, 0, $bits_len);
- }
- return $data;
- }
- sub delta_encode ($integers, $double = 0) {
- my @deltas;
- my $prev = 0;
- unshift(@$integers, scalar(@$integers));
- while (@$integers) {
- my $curr = shift(@$integers);
- push @deltas, $curr - $prev;
- $prev = $curr;
- }
- my $bitstring = '';
- foreach my $d (@deltas) {
- if ($d == 0) {
- $bitstring .= '0';
- }
- elsif ($double) {
- my $t = sprintf('%b', abs($d) + 1);
- my $l = sprintf('%b', length($t));
- $bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($l) - 1)) . '0' . substr($l, 1) . substr($t, 1);
- }
- else {
- my $t = sprintf('%b', abs($d));
- $bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($t) - 1)) . '0' . substr($t, 1);
- }
- }
- pack('B*', $bitstring);
- }
- sub delta_decode ($fh, $double = 0) {
- my @deltas;
- my $buffer = '';
- my $len = 0;
- for (my $k = 0 ; $k <= $len ; ++$k) {
- my $bit = read_bit($fh, \$buffer);
- if ($bit eq '0') {
- push @deltas, 0;
- }
- elsif ($double) {
- my $bit = read_bit($fh, \$buffer);
- my $bl = 0;
- ++$bl while (read_bit($fh, \$buffer) eq '1');
- my $bl2 = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $bl));
- my $int = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. ($bl2 - 1)));
- push @deltas, ($bit eq '1' ? 1 : -1) * ($int - 1);
- }
- else {
- my $bit = read_bit($fh, \$buffer);
- my $n = 0;
- ++$n while (read_bit($fh, \$buffer) eq '1');
- my $d = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
- push @deltas, ($bit eq '1' ? $d : -$d);
- }
- if ($k == 0) {
- $len = pop(@deltas);
- }
- }
- my @acc;
- my $prev = $len;
- foreach my $d (@deltas) {
- $prev += $d;
- push @acc, $prev;
- }
- return \@acc;
- }
- sub create_cfreq ($freq) {
- my @cf;
- my $T = 0;
- foreach my $i (sort { $a <=> $b } keys %$freq) {
- $freq->{$i} // next;
- $cf[$i] = $T;
- $T += $freq->{$i};
- $cf[$i + 1] = $T;
- }
- return (\@cf, $T);
- }
- sub ac_encode ($bytes_arr) {
- my $enc = '';
- my $EOF_SYMBOL = (max(@$bytes_arr) // 0) + 1;
- my @bytes = (@$bytes_arr, $EOF_SYMBOL);
- my %freq;
- ++$freq{$_} for @bytes;
- my ($cf, $T) = create_cfreq(\%freq);
- if ($T > MAX) {
- die "Too few bits: $T > ${\MAX}";
- }
- my $low = 0;
- my $high = MAX;
- my $uf_count = 0;
- foreach my $c (@bytes) {
- my $w = $high - $low + 1;
- $high = ($low + int(($w * $cf->[$c + 1]) / $T) - 1) & MAX;
- $low = ($low + int(($w * $cf->[$c]) / $T)) & MAX;
- if ($high > MAX) {
- die "high > MAX: $high > ${\MAX}";
- }
- if ($low >= $high) { die "$low >= $high" }
- while (1) {
- if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
- my $bit = $high >> (BITS - 1);
- $enc .= $bit;
- if ($uf_count > 0) {
- $enc .= join('', 1 - $bit) x $uf_count;
- $uf_count = 0;
- }
- $low <<= 1;
- ($high <<= 1) |= 1;
- }
- elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
- ($high <<= 1) |= (1 << (BITS - 1));
- $high |= 1;
- ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
- ++$uf_count;
- }
- else {
- last;
- }
- $low &= MAX;
- $high &= MAX;
- }
- }
- $enc .= '0';
- $enc .= '1';
- while (length($enc) % 8 != 0) {
- $enc .= '1';
- }
- return ($enc, \%freq);
- }
- sub ac_decode ($fh, $freq) {
- my ($cf, $T) = create_cfreq($freq);
- my @dec;
- my $low = 0;
- my $high = MAX;
- my $enc = oct('0b' . join '', map { getc($fh) // 1 } 1 .. BITS);
- my @table;
- foreach my $i (sort { $a <=> $b } keys %$freq) {
- foreach my $j ($cf->[$i] .. $cf->[$i + 1] - 1) {
- $table[$j] = $i;
- }
- }
- my $EOF_SYMBOL = max(keys %$freq) // 0;
- while (1) {
- my $w = $high - $low + 1;
- my $ss = int((($T * ($enc - $low + 1)) - 1) / $w);
- my $i = $table[$ss] // last;
- last if ($i == $EOF_SYMBOL);
- push @dec, $i;
- $high = ($low + int(($w * $cf->[$i + 1]) / $T) - 1) & MAX;
- $low = ($low + int(($w * $cf->[$i]) / $T)) & MAX;
- if ($high > MAX) {
- die "error";
- }
- if ($low >= $high) { die "$low >= $high" }
- while (1) {
- if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
- ($high <<= 1) |= 1;
- $low <<= 1;
- ($enc <<= 1) |= (getc($fh) // 1);
- }
- elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
- ($high <<= 1) |= (1 << (BITS - 1));
- $high |= 1;
- ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
- $enc = (($enc >> (BITS - 1)) << (BITS - 1)) | (($enc & ((1 << (BITS - 2)) - 1)) << 1) | (getc($fh) // 1);
- }
- else {
- last;
- }
- $low &= MAX;
- $high &= MAX;
- $enc &= MAX;
- }
- }
- return \@dec;
- }
- sub create_ac_entry ($bytes, $out_fh) {
- my ($enc, $freq) = ac_encode($bytes);
- my $max_symbol = max(keys %$freq) // 0;
- my @freqs;
- foreach my $k (0 .. $max_symbol) {
- push @freqs, $freq->{$k} // 0;
- }
- push @freqs, length($enc) >> 3;
- say "Max symbol: $max_symbol";
- print $out_fh delta_encode(\@freqs);
- print $out_fh pack("B*", $enc);
- }
- sub decode_ac_entry ($fh) {
- my @freqs = @{delta_decode($fh)};
- my $bits_len = pop(@freqs);
- my %freq;
- foreach my $i (0 .. $#freqs) {
- if ($freqs[$i]) {
- $freq{$i} = $freqs[$i];
- }
- }
- say "Encoded length: $bits_len";
- my $bits = read_bits($fh, $bits_len << 3);
- if ($bits_len > 0) {
- open my $bits_fh, '<:raw', \$bits;
- return ac_decode($bits_fh, \%freq);
- }
- return [];
- }
- sub encode_distances ($distances, $out_fh) {
- my @symbols;
- my $offset_bits = '';
- foreach my $dist (@$distances) {
- my $i = $DISTANCE_INDICES[$dist];
- my ($min, $bits) = @{$DISTANCE_SYMBOLS[$i]};
- push @symbols, $i;
- if ($bits > 0) {
- $offset_bits .= sprintf('%0*b', $bits, $dist - $min);
- }
- }
- create_ac_entry(\@symbols, $out_fh);
- print $out_fh pack('B*', $offset_bits);
- }
- sub decode_distances ($fh) {
- my $symbols = decode_ac_entry($fh);
- my $bits_len = 0;
- foreach my $i (@$symbols) {
- $bits_len += $DISTANCE_SYMBOLS[$i][1];
- }
- my $bits = read_bits($fh, $bits_len);
- my @distances;
- foreach my $i (@$symbols) {
- push @distances, $DISTANCE_SYMBOLS[$i][0] + oct('0b' . substr($bits, 0, $DISTANCE_SYMBOLS[$i][1], ''));
- }
- return \@distances;
- }
- sub mtf_encode ($bytes, $alphabet = [0 .. 255]) {
- my @C;
- my @table;
- @table[@$alphabet] = (0 .. $#{$alphabet});
- foreach my $c (@$bytes) {
- push @C, (my $index = $table[$c]);
- unshift(@$alphabet, splice(@$alphabet, $index, 1));
- @table[@{$alphabet}[0 .. $index]] = (0 .. $index);
- }
- return \@C;
- }
- sub mtf_decode ($encoded, $alphabet = [0 .. 255]) {
- my @S;
- foreach my $p (@$encoded) {
- push @S, $alphabet->[$p];
- unshift(@$alphabet, splice(@$alphabet, $p, 1));
- }
- return \@S;
- }
- sub bwt_balanced ($s) { # O(n * LOOKAHEAD_LEN) space (fast)
- #<<<
- [
- map { $_->[1] } sort {
- ($a->[0] cmp $b->[0])
- || ((substr($s, $a->[1]) . substr($s, 0, $a->[1])) cmp(substr($s, $b->[1]) . substr($s, 0, $b->[1])))
- }
- map {
- my $t = substr($s, $_, LOOKAHEAD_LEN);
- if (length($t) < LOOKAHEAD_LEN) {
- $t .= substr($s, 0, ($_ < LOOKAHEAD_LEN) ? $_ : (LOOKAHEAD_LEN - length($t)));
- }
- [$t, $_]
- } 0 .. length($s) - 1
- ];
- #>>>
- }
- sub bwt_encode ($s) {
- my $bwt = bwt_balanced($s);
- my $ret = join('', map { substr($s, $_ - 1, 1) } @$bwt);
- my $idx = 0;
- foreach my $i (@$bwt) {
- $i || last;
- ++$idx;
- }
- return ($ret, $idx);
- }
- sub bwt_decode ($bwt, $idx) { # fast inversion
- my @tail = split(//, $bwt);
- my @head = sort @tail;
- my %indices;
- foreach my $i (0 .. $#tail) {
- push @{$indices{$tail[$i]}}, $i;
- }
- my @table;
- foreach my $v (@head) {
- push @table, shift(@{$indices{$v}});
- }
- my $dec = '';
- my $i = $idx;
- for (1 .. scalar(@head)) {
- $dec .= $head[$i];
- $i = $table[$i];
- }
- return $dec;
- }
- sub rle4_encode ($bytes) { # RLE1
- my @rle;
- my $end = $#{$bytes};
- my $prev = -1;
- my $run = 0;
- for (my $i = 0 ; $i <= $end ; ++$i) {
- if ($bytes->[$i] == $prev) {
- ++$run;
- }
- else {
- $run = 1;
- }
- push @rle, $bytes->[$i];
- $prev = $bytes->[$i];
- if ($run >= 4) {
- $run = 0;
- $i += 1;
- while ($run < 254 and $i <= $end and $bytes->[$i] == $prev) {
- ++$run;
- ++$i;
- }
- push @rle, $run;
- $run = 1;
- if ($i <= $end) {
- $prev = $bytes->[$i];
- push @rle, $bytes->[$i];
- }
- }
- }
- return \@rle;
- }
- sub rle4_decode ($bytes) { # RLE1
- my @dec = $bytes->[0];
- my $end = $#{$bytes};
- my $prev = $bytes->[0];
- my $run = 1;
- for (my $i = 1 ; $i <= $end ; ++$i) {
- if ($bytes->[$i] == $prev) {
- ++$run;
- }
- else {
- $run = 1;
- }
- push @dec, $bytes->[$i];
- $prev = $bytes->[$i];
- if ($run >= 4) {
- if (++$i <= $end) {
- $run = $bytes->[$i];
- push @dec, (($prev) x $run);
- }
- $run = 0;
- }
- }
- return \@dec;
- }
- sub rle_encode ($bytes) { # RLE2
- my @rle;
- my $end = $#{$bytes};
- for (my $i = 0 ; $i <= $end ; ++$i) {
- my $run = 0;
- while ($i <= $end and $bytes->[$i] == 0) {
- ++$run;
- ++$i;
- }
- if ($run >= 1) {
- my $t = sprintf('%b', $run + 1);
- push @rle, split(//, substr($t, 1));
- }
- if ($i <= $end) {
- push @rle, $bytes->[$i] + 1;
- }
- }
- return \@rle;
- }
- sub rle_decode ($rle) { # RLE2
- my @dec;
- my $end = $#{$rle};
- for (my $i = 0 ; $i <= $end ; ++$i) {
- my $k = $rle->[$i];
- if ($k == 0 or $k == 1) {
- my $run = 1;
- while (($i <= $end) and ($k == 0 or $k == 1)) {
- ($run <<= 1) |= $k;
- $k = $rle->[++$i];
- }
- push @dec, (0) x ($run - 1);
- }
- if ($i <= $end) {
- push @dec, $k - 1;
- }
- }
- return \@dec;
- }
- sub encode_alphabet ($alphabet) {
- my %table;
- @table{@$alphabet} = ();
- my $populated = 0;
- my @marked;
- for (my $i = 0 ; $i <= 255 ; $i += 32) {
- my $enc = 0;
- foreach my $j (0 .. 31) {
- if (exists($table{$i + $j})) {
- $enc |= 1 << $j;
- }
- }
- if ($enc == 0) {
- $populated <<= 1;
- }
- else {
- ($populated <<= 1) |= 1;
- push @marked, $enc;
- }
- }
- my $delta = delta_encode([@marked], 1);
- say "Populated : ", sprintf('%08b', $populated);
- say "Marked : @marked";
- say "Delta len : ", length($delta);
- my $encoded = '';
- $encoded .= chr($populated);
- $encoded .= $delta;
- return $encoded;
- }
- sub decode_alphabet ($fh) {
- my @populated = split(//, sprintf('%08b', ord(getc($fh) // die "error")));
- my $marked = delta_decode($fh, 1);
- my @alphabet;
- for (my $i = 0 ; $i <= 255 ; $i += 32) {
- if (shift(@populated)) {
- my $m = shift(@$marked);
- foreach my $j (0 .. 31) {
- if ($m & 1) {
- push @alphabet, $i + $j;
- }
- $m >>= 1;
- }
- }
- }
- return \@alphabet;
- }
- sub lzhd_compression ($chunk, $out_fh) {
- my (@uncompressed, @indices, @lengths);
- lz77_compression($chunk, \@uncompressed, \@indices, \@lengths);
- my $est_ratio = length($chunk) / (4 * scalar(@uncompressed));
- say(scalar(@uncompressed), ' -> ', $est_ratio);
- create_ac_entry(\@uncompressed, $out_fh);
- create_ac_entry(\@lengths, $out_fh);
- encode_distances(\@indices, $out_fh);
- }
- sub lzhd_decompression ($fh) {
- my $uncompressed = decode_ac_entry($fh);
- my $lengths = decode_ac_entry($fh);
- my $indices = decode_distances($fh);
- return lz77_decompression($uncompressed, $indices, $lengths);
- }
- sub compression ($chunk, $out_fh) {
- my @chunk_bytes = unpack('C*', $chunk);
- my $data = pack('C*', @{rle4_encode(\@chunk_bytes)});
- my ($bwt, $idx) = bwt_encode($data);
- my @bytes = unpack('C*', $bwt);
- my @alphabet = sort { $a <=> $b } uniq(@bytes);
- my $enc_bytes = mtf_encode(\@bytes, [@alphabet]);
- if (max(@$enc_bytes) < 255) {
- print $out_fh chr(1);
- $enc_bytes = rle_encode($enc_bytes);
- }
- else {
- print $out_fh chr(0);
- $enc_bytes = rle4_encode($enc_bytes);
- }
- print $out_fh pack('N', $idx);
- print $out_fh encode_alphabet(\@alphabet);
- lzhd_compression(pack('C*', @$enc_bytes), $out_fh);
- }
- sub decompression ($fh, $out_fh) {
- my $rle_encoded = ord(getc($fh) // die "error");
- my $idx = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
- my $alphabet = decode_alphabet($fh);
- my $bytes = [unpack('C*', lzhd_decompression($fh))];
- if ($rle_encoded) {
- $bytes = rle_decode($bytes);
- }
- else {
- $bytes = rle4_decode($bytes);
- }
- $bytes = mtf_decode($bytes, [@$alphabet]);
- print $out_fh pack('C*', @{rle4_decode([unpack('C*', bwt_decode(pack('C*', @$bytes), $idx))])});
- }
- # Compress file
- sub compress_file ($input, $output) {
- open my $fh, '<:raw', $input
- or die "Can't open file <<$input>> for reading: $!";
- my $header = SIGNATURE;
- # Open the output file for writing
- open my $out_fh, '>:raw', $output
- or die "Can't open file <<$output>> for write: $!";
- # Print the header
- print $out_fh $header;
- # Compress data
- while (read($fh, (my $chunk), CHUNK_SIZE)) {
- compression($chunk, $out_fh);
- }
- # Close the output file
- close $out_fh;
- }
- # Decompress file
- sub decompress_file ($input, $output) {
- # Open and validate the input file
- open my $fh, '<:raw', $input
- or die "Can't open file <<$input>> for reading: $!";
- valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
- # Open the output file
- open my $out_fh, '>:raw', $output
- or die "Can't open file <<$output>> for writing: $!";
- while (!eof($fh)) {
- decompression($fh, $out_fh);
- }
- # Close the output file
- close $out_fh;
- }
- main();
- exit(0);
|