bwt2_file_compression.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 14 June 2023
  4. # Edit: 19 March 2024
  5. # https://github.com/trizen
  6. # Compress/decompress files using Burrows-Wheeler Transform (BWT) + Move-To-Front transform (MTF) + Run-length encoding (RLE) + Bzip2.
  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 => 'BWT2',
  16. VERSION => '0.01',
  17. FORMAT => 'bwt2',
  18. CHUNK_SIZE => 1 << 17,
  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. # Compress file
  84. sub compress_file ($input, $output) {
  85. open my $fh, '<:raw', $input
  86. or die "Can't open file <<$input>> for reading: $!";
  87. my $header = SIGNATURE;
  88. # Open the output file for writing
  89. open my $out_fh, '>:raw', $output
  90. or die "Can't open file <<$output>> for write: $!";
  91. # Print the header
  92. print $out_fh $header;
  93. # Compress data
  94. while (read($fh, (my $chunk), CHUNK_SIZE)) {
  95. print $out_fh bwt_compress($chunk, \&bwt_compress_symbolic);
  96. }
  97. # Close the file
  98. close $out_fh;
  99. }
  100. # Decompress file
  101. sub decompress_file ($input, $output) {
  102. # Open and validate the input file
  103. open my $fh, '<:raw', $input
  104. or die "Can't open file <<$input>> for reading: $!";
  105. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  106. # Open the output file
  107. open my $out_fh, '>:raw', $output
  108. or die "Can't open file <<$output>> for writing: $!";
  109. while (!eof($fh)) {
  110. print $out_fh bwt_decompress($fh, \&bwt_decompress_symbolic);
  111. }
  112. # Close the file
  113. close $fh;
  114. close $out_fh;
  115. }
  116. main();
  117. exit(0);