bwlz_file_compression.pl 4.9 KB

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