bwrm2_file_compression.pl 4.6 KB

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