tzip_file_compression.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/perl
  2. # Author: Șuteu "Trizen" Daniel
  3. # License: GPLv3
  4. # Date: 12 August 2013
  5. # Website: https://trizenx.blogspot.com
  6. #
  7. ## A very simple file compressor.
  8. #
  9. # Best usage of this script is to compress files which
  10. # contains not so many different bytes (for example, DNA-sequences)
  11. use 5.010;
  12. use strict;
  13. use autodie;
  14. use warnings;
  15. use Getopt::Std qw(getopts);
  16. use File::Basename qw(basename);
  17. our $DEBUG = 0;
  18. use constant {
  19. CHUNK_SIZE => 2 * 1024**2, # 2 MB
  20. SIGNATURE => 'TZP' . chr(1),
  21. FORMAT => 'tzp',
  22. };
  23. sub usage {
  24. my ($code) = @_;
  25. print <<"EOH";
  26. usage: $0 [options] [input file] [output file]
  27. options:
  28. -e : extract
  29. -i <filename> : input filename
  30. -o <filename> : output filename
  31. -r : rewrite output
  32. -v : version number
  33. -h : this message
  34. examples:
  35. $0 document.txt
  36. $0 document.txt archive.${\FORMAT}
  37. $0 archive.${\FORMAT} document.txt
  38. $0 -e -i archive.${\FORMAT} -o document.txt
  39. EOH
  40. exit($code // 0);
  41. }
  42. sub main {
  43. my %opt;
  44. getopts('ei:o:vhr', \%opt);
  45. $opt{h} && usage(0);
  46. $opt{v} && version();
  47. my ($input, $output) = @ARGV;
  48. $input //= $opt{i} // usage(2);
  49. $output //= $opt{o};
  50. my $ext = qr{\.${\FORMAT}\z}io;
  51. if ($opt{e} || $input =~ $ext) {
  52. if (not defined $output) {
  53. ($output = basename($input)) =~ s{$ext}{}
  54. || die "$0: no output file specified!\n";
  55. }
  56. if (not $opt{r} and -e $output) {
  57. print "'$output' already exists! -- Replace? [y/N] ";
  58. <STDIN> =~ /^y/i || exit 17;
  59. }
  60. decompress_file($input, $output)
  61. || die "$0: error: decompression failed!\n";
  62. }
  63. elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
  64. $output //= basename($input) . '.' . FORMAT;
  65. compress_file($input, $output)
  66. || die "$0: error: compression failed!\n";
  67. }
  68. else {
  69. warn "$0: don't know what to do...\n";
  70. usage(1);
  71. }
  72. }
  73. sub next_power_of_two {
  74. my ($number) = @_;
  75. ## If the number is a power of
  76. ## two, then return it as it is.
  77. unless ($number & ($number - 1)) {
  78. return $number;
  79. }
  80. ## Return the next power of two
  81. return 2 << (log($number) / log(2));
  82. }
  83. sub valid_archive {
  84. my ($fh) = @_;
  85. if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
  86. $sig eq SIGNATURE || return;
  87. }
  88. return 1;
  89. }
  90. sub open_file {
  91. my ($mode, $file) = @_;
  92. open(my $fh, $mode, $file);
  93. return $fh;
  94. }
  95. sub uniq_bytes {
  96. my ($fh) = @_;
  97. my %table;
  98. while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
  99. @table{split //, $chunk} = ();
  100. }
  101. seek($fh, 0, 0);
  102. return [keys %table];
  103. }
  104. sub info {
  105. my (%info) = @_;
  106. print STDERR <<"EOT";
  107. input : $info{input}
  108. output : $info{output}
  109. filesize : $info{filesize}
  110. bits num : $info{bits_num}
  111. bytes num : $info{bytes_num}
  112. compressing : $info{compress}
  113. EOT
  114. }
  115. sub compress_file {
  116. my ($input, $output) = @_;
  117. my $fh = open_file('<:raw', $input);
  118. my $out_fh = open_file('>:raw', $output);
  119. my $filesize = -s $input;
  120. my $uniq_bytes = uniq_bytes($fh);
  121. my $bytes_num = scalar @{$uniq_bytes};
  122. my $bits_num = log(next_power_of_two($bytes_num)) / log(2);
  123. $DEBUG
  124. && info(
  125. bytes_num => $bytes_num,
  126. bits_num => $bits_num,
  127. input => $input,
  128. output => $output,
  129. filesize => $filesize,
  130. compress => 'true',
  131. );
  132. my %table;
  133. my $bits_map = '';
  134. foreach my $i (0 .. $#{$uniq_bytes}) {
  135. $bits_map .= ($table{$uniq_bytes->[$i]} = sprintf("%0${bits_num}b", $i));
  136. }
  137. print {$out_fh} SIGNATURE,
  138. chr(int(length($filesize) / 2 + 0.5)),
  139. join('', map { chr } unpack '(A2)*', $filesize),
  140. chr($bits_num), chr($bytes_num - 1),
  141. join('', @{$uniq_bytes}), pack('B*', $bits_map);
  142. while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
  143. print {$out_fh} scalar pack "B*", join('', @table{split //, $chunk});
  144. }
  145. return 1;
  146. }
  147. sub decompress_file {
  148. my ($input, $output) = @_;
  149. my $fh = open_file('<:raw', $input);
  150. my $out_fh = open_file('>:raw', $output);
  151. valid_archive($fh) || die "$0: file `$input' is not a TZP archive!\n";
  152. my $fsize_len = do { read($fh, (my $byte), 1); ord $byte };
  153. my $filesize = do {
  154. read($fh, (my $bytes), $fsize_len);
  155. my @bytes = unpack('C*', $bytes);
  156. foreach my $i (0 .. $#bytes - 1) {
  157. length($bytes[$i]) != 2 && do { $bytes[$i] = sprintf('%02d', $bytes[$i]) }
  158. }
  159. join('', @bytes);
  160. };
  161. my $bits_num = do { read($fh, (my $byte), 1); ord $byte };
  162. my $bytes_num = do { read($fh, (my $byte), 1); 1 + ord $byte };
  163. $DEBUG
  164. && info(
  165. bytes_num => $bytes_num,
  166. bits_num => $bits_num,
  167. input => $input,
  168. output => $output,
  169. filesize => $filesize,
  170. compress => 'false',
  171. );
  172. my $bytes = do { read($fh, (my $bytes), $bytes_num); [split(//, $bytes)] };
  173. my $bits_len = $bits_num * $bytes_num;
  174. if ((my $mod = $bits_len % 8) != 0) {
  175. $bits_len += 8 - $mod;
  176. }
  177. my $bits = do { read($fh, my ($bytes), $bits_len / 8); unpack 'B*', $bytes };
  178. my %table;
  179. foreach my $byte (@{$bytes}) {
  180. $table{substr($bits, 0, $bits_num, '')} = $byte;
  181. }
  182. my $bit_counter = 0;
  183. my $prev_bits = '';
  184. while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
  185. $bit_counter += $size * 8;
  186. my $bits = $prev_bits . unpack "B*", $chunk;
  187. my $bits_len = ($size * 8 + length($prev_bits));
  188. if ($bit_counter / $bits_num - $filesize == 1) {
  189. chop($bits), $bits_len-- for (1 .. $bits_num);
  190. }
  191. elsif ($bits_num < 8 && $bit_counter % $bits_num != 0 && eof($fh)) {
  192. chop($bits), $bits_len-- for (1 .. $bit_counter % $bits_num);
  193. }
  194. my $sequence = '';
  195. foreach (1 .. $bits_len / $bits_num) {
  196. $sequence .= $table{substr($bits, 0, $bits_num, '')};
  197. }
  198. print {$out_fh} $sequence;
  199. $prev_bits = $bits;
  200. }
  201. return 1;
  202. }
  203. main();
  204. exit(0);