lz77_file_compression.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 15 December 2022
  4. # https://github.com/trizen
  5. # Compress/decompress files using LZ77 compression.
  6. use 5.020;
  7. use strict;
  8. use warnings;
  9. use experimental qw(signatures);
  10. use Getopt::Std qw(getopts);
  11. use File::Basename qw(basename);
  12. use constant {
  13. PKGNAME => 'LZ77',
  14. VERSION => '0.02',
  15. FORMAT => 'lz77',
  16. CHUNK_SIZE => 1 << 16,
  17. };
  18. # Container signature
  19. use constant SIGNATURE => uc(FORMAT) . chr(2);
  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. sub compression ($str) {
  82. my @rep;
  83. my $la = 0;
  84. my $prefix = '';
  85. my @chars = split(//, $str);
  86. my $end = $#chars;
  87. while ($la <= $end) {
  88. my $n = 1;
  89. my $p = 0;
  90. my $tmp;
  91. my $token = $chars[$la];
  92. while ( $n < 255
  93. and $la + $n <= $end
  94. and ($tmp = index($prefix, $token, $p)) >= 0) {
  95. $p = $tmp;
  96. $token .= $chars[$la + $n];
  97. ++$n;
  98. }
  99. --$n;
  100. push(@rep, $p, $n, $chars[$la + $n]);
  101. $la += $n + 1;
  102. $prefix .= $token;
  103. }
  104. pack('(SCa)*', @rep);
  105. }
  106. # Compress file
  107. sub compress_file ($input, $output) {
  108. open my $fh, '<:raw', $input
  109. or die "Can't open file <<$input>> for reading: $!";
  110. my $header = SIGNATURE;
  111. # Open the output file for writing
  112. open my $out_fh, '>:raw', $output
  113. or die "Can't open file <<$output>> for write: $!";
  114. # Print the header
  115. print $out_fh $header;
  116. # Compress data
  117. while (read($fh, (my $chunk), CHUNK_SIZE)) {
  118. print $out_fh compression($chunk);
  119. }
  120. # Close the file
  121. close $out_fh;
  122. }
  123. # Decompress file
  124. sub decompress_file ($input, $output) {
  125. # Open and validate the input file
  126. open my $fh, '<:raw', $input
  127. or die "Can't open file <<$input>> for reading: $!";
  128. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  129. # Open the output file
  130. open my $out_fh, '>:raw', $output
  131. or die "Can't open file <<$output>> for writing: $!";
  132. my $chunk = '';
  133. while (read($fh, (my $str), 4 * CHUNK_SIZE)) {
  134. my @decoded = unpack('(SCa)*', $str);
  135. while (@decoded) {
  136. my ($s, $l, $c) = splice(@decoded, 0, 3);
  137. $chunk .= substr($chunk, $s, $l) . $c;
  138. if (length($chunk) >= CHUNK_SIZE) {
  139. print $out_fh $chunk;
  140. $chunk = '';
  141. }
  142. }
  143. }
  144. print $out_fh $chunk;
  145. # Close the file
  146. close $fh;
  147. close $out_fh;
  148. }
  149. main();
  150. exit(0);