lzih_file_compression.pl 4.0 KB

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