lzih_file_compression.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. COMPRESSED_BYTE => chr(1),
  16. UNCOMPRESSED_BYTE => chr(0),
  17. CHUNK_SIZE => 1 << 16, # higher value = better compression
  18. RANDOM_DATA_THRESHOLD => 1, # in ratio
  19. };
  20. # Container signature
  21. use constant SIGNATURE => uc(FORMAT) . chr(4);
  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. my ($uncompressed, $indices, $lengths) = lz77_encode($chunk);
  96. my $est_ratio = length($chunk) / (4 * scalar(@$uncompressed));
  97. say(scalar(@$uncompressed), ' -> ', $est_ratio);
  98. if ($est_ratio > RANDOM_DATA_THRESHOLD) {
  99. print $out_fh COMPRESSED_BYTE;
  100. print $out_fh create_huffman_entry($uncompressed);
  101. print $out_fh create_huffman_entry($lengths);
  102. print $out_fh abc_encode($indices);
  103. }
  104. else {
  105. print $out_fh UNCOMPRESSED_BYTE;
  106. print $out_fh create_huffman_entry($chunk);
  107. }
  108. }
  109. # Close the file
  110. close $out_fh;
  111. }
  112. # Decompress file
  113. sub decompress_file ($input, $output) {
  114. # Open and validate the input file
  115. open my $fh, '<:raw', $input
  116. or die "Can't open file <<$input>> for reading: $!";
  117. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  118. # Open the output file
  119. open my $out_fh, '>:raw', $output
  120. or die "Can't open file <<$output>> for writing: $!";
  121. while (!eof($fh)) {
  122. my $compression_byte = getc($fh) // die "decompression error";
  123. if ($compression_byte eq COMPRESSED_BYTE) {
  124. my $uncompressed = decode_huffman_entry($fh);
  125. my $lengths = decode_huffman_entry($fh);
  126. my $indices = abc_decode($fh);
  127. print $out_fh lz77_decode($uncompressed, $indices, $lengths);
  128. }
  129. elsif ($compression_byte eq UNCOMPRESSED_BYTE) {
  130. print $out_fh pack('C*', @{decode_huffman_entry($fh)});
  131. }
  132. else {
  133. die "Invalid compression...";
  134. }
  135. }
  136. # Close the file
  137. close $fh;
  138. close $out_fh;
  139. }
  140. main();
  141. exit(0);