lz77_file_compression.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 15 December 2022
  4. # Edit: 11 April 2024
  5. # https://github.com/trizen
  6. # Compress/decompress files using LZ77 compression + Huffman coding.
  7. # Encoding the distances/indices using a DEFLATE-like approach.
  8. use 5.036;
  9. use Getopt::Std qw(getopts);
  10. use File::Basename qw(basename);
  11. use Compression::Util qw(:all);
  12. use constant {
  13. PKGNAME => 'LZ77',
  14. VERSION => '0.01',
  15. FORMAT => 'lz77',
  16. CHUNK_SIZE => 1 << 18, # higher value = better compression
  17. };
  18. # Container signature
  19. use constant SIGNATURE => uc(FORMAT) . chr(1);
  20. sub usage {
  21. my ($code) = @_;
  22. print <<"EOH";
  23. usage: $0 [options] [input file] [output file]
  24. options:
  25. -e : extract
  26. -i <filename> : input filename
  27. -o <filename> : output filename
  28. -r : rewrite output
  29. -v : version number
  30. -h : this message
  31. examples:
  32. $0 document.txt
  33. $0 document.txt archive.${\FORMAT}
  34. $0 archive.${\FORMAT} document.txt
  35. $0 -e -i archive.${\FORMAT} -o document.txt
  36. EOH
  37. exit($code // 0);
  38. }
  39. sub version {
  40. printf("%s %s\n", PKGNAME, VERSION);
  41. exit;
  42. }
  43. sub valid_archive {
  44. my ($fh) = @_;
  45. if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
  46. $sig eq SIGNATURE || return;
  47. }
  48. return 1;
  49. }
  50. sub main {
  51. my %opt;
  52. getopts('ei:o:vhr', \%opt);
  53. $opt{h} && usage(0);
  54. $opt{v} && version();
  55. my ($input, $output) = @ARGV;
  56. $input //= $opt{i} // usage(2);
  57. $output //= $opt{o};
  58. my $ext = qr{\.${\FORMAT}\z}io;
  59. if ($opt{e} || $input =~ $ext) {
  60. if (not defined $output) {
  61. ($output = basename($input)) =~ s{$ext}{}
  62. || die "$0: no output file specified!\n";
  63. }
  64. if (not $opt{r} and -e $output) {
  65. print "'$output' already exists! -- Replace? [y/N] ";
  66. <STDIN> =~ /^y/i || exit 17;
  67. }
  68. decompress_file($input, $output)
  69. || die "$0: error: decompression failed!\n";
  70. }
  71. elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
  72. $output //= basename($input) . '.' . FORMAT;
  73. compress_file($input, $output)
  74. || die "$0: error: compression failed!\n";
  75. }
  76. else {
  77. warn "$0: don't know what to do...\n";
  78. usage(1);
  79. }
  80. }
  81. # Compress file
  82. sub compress_file ($input, $output) {
  83. open my $fh, '<:raw', $input
  84. or die "Can't open file <<$input>> for reading: $!";
  85. my $header = SIGNATURE;
  86. # Open the output file for writing
  87. open my $out_fh, '>:raw', $output
  88. or die "Can't open file <<$output>> for write: $!";
  89. # Print the header
  90. print $out_fh $header;
  91. # Compress data
  92. while (read($fh, (my $chunk), CHUNK_SIZE)) {
  93. print $out_fh lz77_compress($chunk);
  94. }
  95. # Close the file
  96. close $out_fh;
  97. }
  98. # Decompress file
  99. sub decompress_file ($input, $output) {
  100. # Open and validate the input file
  101. open my $fh, '<:raw', $input
  102. or die "Can't open file <<$input>> for reading: $!";
  103. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  104. # Open the output file
  105. open my $out_fh, '>:raw', $output
  106. or die "Can't open file <<$output>> for writing: $!";
  107. while (!eof($fh)) {
  108. print $out_fh lz77_decompress($fh);
  109. }
  110. # Close the file
  111. close $fh;
  112. close $out_fh;
  113. }
  114. main();
  115. exit(0);