bwlz2_file_compression.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 15 June 2023
  4. # Edit: 13 April 2024
  5. # https://github.com/trizen
  6. # Compress/decompress files using Burrows-Wheeler Transform (BWT) + LZ77 compression + Symbolic Bzip2.
  7. # Encoding the literals and the pointers using a DEFLATE-like approach.
  8. # References:
  9. # Data Compression (Summer 2023) - Lecture 11 - DEFLATE (gzip)
  10. # https://youtube.com/watch?v=SJPvNi4HrWQ
  11. #
  12. # Data Compression (Summer 2023) - Lecture 13 - BZip2
  13. # https://youtube.com/watch?v=cvoZbBZ3M2A
  14. use 5.036;
  15. use Getopt::Std qw(getopts);
  16. use File::Basename qw(basename);
  17. use List::Util qw(max uniq);
  18. use Compression::Util qw(:all);
  19. use constant {
  20. PKGNAME => 'BWLZ2',
  21. VERSION => '0.01',
  22. FORMAT => 'bwlz2',
  23. CHUNK_SIZE => 1 << 17, # higher value = better compression
  24. };
  25. # Container signature
  26. use constant SIGNATURE => uc(FORMAT) . chr(1);
  27. sub usage {
  28. my ($code) = @_;
  29. print <<"EOH";
  30. usage: $0 [options] [input file] [output file]
  31. options:
  32. -e : extract
  33. -i <filename> : input filename
  34. -o <filename> : output filename
  35. -r : rewrite output
  36. -v : version number
  37. -h : this message
  38. examples:
  39. $0 document.txt
  40. $0 document.txt archive.${\FORMAT}
  41. $0 archive.${\FORMAT} document.txt
  42. $0 -e -i archive.${\FORMAT} -o document.txt
  43. EOH
  44. exit($code // 0);
  45. }
  46. sub version {
  47. printf("%s %s\n", PKGNAME, VERSION);
  48. exit;
  49. }
  50. sub valid_archive {
  51. my ($fh) = @_;
  52. if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
  53. $sig eq SIGNATURE || return;
  54. }
  55. return 1;
  56. }
  57. sub main {
  58. my %opt;
  59. getopts('ei:o:vhr', \%opt);
  60. $opt{h} && usage(0);
  61. $opt{v} && version();
  62. my ($input, $output) = @ARGV;
  63. $input //= $opt{i} // usage(2);
  64. $output //= $opt{o};
  65. my $ext = qr{\.${\FORMAT}\z}io;
  66. if ($opt{e} || $input =~ $ext) {
  67. if (not defined $output) {
  68. ($output = basename($input)) =~ s{$ext}{}
  69. || die "$0: no output file specified!\n";
  70. }
  71. if (not $opt{r} and -e $output) {
  72. print "'$output' already exists! -- Replace? [y/N] ";
  73. <STDIN> =~ /^y/i || exit 17;
  74. }
  75. decompress_file($input, $output)
  76. || die "$0: error: decompression failed!\n";
  77. }
  78. elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
  79. $output //= basename($input) . '.' . FORMAT;
  80. compress_file($input, $output)
  81. || die "$0: error: compression failed!\n";
  82. }
  83. else {
  84. warn "$0: don't know what to do...\n";
  85. usage(1);
  86. }
  87. }
  88. sub compression ($chunk, $out_fh) {
  89. my @chunk_bytes = unpack('C*', $chunk);
  90. my $data = pack('C*', @{rle4_encode(\@chunk_bytes, 254)});
  91. my ($bwt, $idx) = bwt_encode($data);
  92. my @bytes = unpack('C*', $bwt);
  93. my @alphabet = sort { $a <=> $b } uniq(@bytes);
  94. my $enc_bytes = mtf_encode(\@bytes, \@alphabet);
  95. if (max(@$enc_bytes) < 255) {
  96. print $out_fh chr(1);
  97. $enc_bytes = zrle_encode($enc_bytes);
  98. }
  99. else {
  100. print $out_fh chr(0);
  101. $enc_bytes = rle4_encode($enc_bytes);
  102. }
  103. print $out_fh pack('N', $idx);
  104. print $out_fh encode_alphabet(\@alphabet);
  105. print $out_fh lzss_compress(pack('C*', @$enc_bytes), \&bwt_compress_symbolic);
  106. }
  107. sub decompression ($fh, $out_fh) {
  108. my $rle_encoded = ord(getc($fh) // die "error");
  109. my $idx = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
  110. my $alphabet = decode_alphabet($fh);
  111. my $dec = lzss_decompress($fh, \&bwt_decompress_symbolic);
  112. my $bytes = [unpack('C*', $dec)];
  113. if ($rle_encoded) {
  114. $bytes = zrle_decode($bytes);
  115. }
  116. else {
  117. $bytes = rle4_decode($bytes);
  118. }
  119. $bytes = mtf_decode($bytes, $alphabet);
  120. print $out_fh symbols2string(rle4_decode(string2symbols(bwt_decode(pack('C*', @$bytes), $idx))));
  121. }
  122. # Compress file
  123. sub compress_file ($input, $output) {
  124. open my $fh, '<:raw', $input
  125. or die "Can't open file <<$input>> for reading: $!";
  126. my $header = SIGNATURE;
  127. # Open the output file for writing
  128. open my $out_fh, '>:raw', $output
  129. or die "Can't open file <<$output>> for write: $!";
  130. # Print the header
  131. print $out_fh $header;
  132. # Compress data
  133. while (read($fh, (my $chunk), CHUNK_SIZE)) {
  134. compression($chunk, $out_fh);
  135. }
  136. # Close the output file
  137. close $out_fh;
  138. }
  139. # Decompress file
  140. sub decompress_file ($input, $output) {
  141. # Open and validate the input file
  142. open my $fh, '<:raw', $input
  143. or die "Can't open file <<$input>> for reading: $!";
  144. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  145. # Open the output file
  146. open my $out_fh, '>:raw', $output
  147. or die "Can't open file <<$output>> for writing: $!";
  148. while (!eof($fh)) {
  149. decompression($fh, $out_fh);
  150. }
  151. # Close the files
  152. close $fh;
  153. close $out_fh;
  154. }
  155. main();
  156. exit(0);