Compression.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Compression;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use base qw(Exporter);
  22. our @EXPORT = qw($compression_re_file_ext compression_get_list
  23. compression_is_supported compression_get_property
  24. compression_guess_from_filename
  25. compression_get_default compression_set_default
  26. compression_get_default_level
  27. compression_set_default_level
  28. compression_is_valid_level);
  29. =head1 NAME
  30. Dpkg::Compression - simple database of available compression methods
  31. =head1 DESCRIPTION
  32. This modules provides a few public funcions and a public regex to
  33. interact with the set of supported compression methods.
  34. =head1 EXPORTED VARIABLES
  35. =over 4
  36. =cut
  37. my $COMP = {
  38. "gzip" => {
  39. "file_ext" => "gz",
  40. "comp_prog" => [ "gzip" ],
  41. "decomp_prog" => [ "gunzip" ],
  42. },
  43. "bzip2" => {
  44. "file_ext" => "bz2",
  45. "comp_prog" => [ "bzip2" ],
  46. "decomp_prog" => [ "bunzip2" ],
  47. },
  48. "lzma" => {
  49. "file_ext" => "lzma",
  50. "comp_prog" => [ "lzma" ],
  51. "decomp_prog" => [ "unlzma" ],
  52. },
  53. "xz" => {
  54. "file_ext" => "xz",
  55. "comp_prog" => [ "xz" ],
  56. "decomp_prog" => [ "unxz" ],
  57. },
  58. };
  59. our $default_compression = "gzip";
  60. our $default_compression_level = 9;
  61. =item $compression_re_file_ext
  62. A regex that matches a file extension of a file compressed with one of the
  63. supported compression methods.
  64. =back
  65. =cut
  66. my $regex = join "|", map { $_->{"file_ext"} } values %$COMP;
  67. our $compression_re_file_ext = qr/(?:$regex)/;
  68. =head1 EXPORTED FUNCTIONS
  69. =over 4
  70. =item my @list = compression_get_list()
  71. Returns a list of supported compression methods (sorted alphabetically).
  72. =cut
  73. sub compression_get_list {
  74. return sort keys %$COMP;
  75. }
  76. =item compression_is_supported($comp)
  77. Returns a boolean indicating whether the give compression method is
  78. known and supported.
  79. =cut
  80. sub compression_is_supported {
  81. return exists $COMP->{$_[0]};
  82. }
  83. =item compression_get_property($comp, $property)
  84. Returns the requested property of the compression method. Returns undef if
  85. either the property or the compression method doesn't exist. Valid
  86. properties currently include "file_ext" for the file extension,
  87. "comp_prog" for the name of the compression program and "decomp_prog" for
  88. the name of the decompression program.
  89. =cut
  90. sub compression_get_property {
  91. my ($comp, $property) = @_;
  92. return undef unless compression_is_supported($comp);
  93. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  94. return undef;
  95. }
  96. =item compression_guess_from_filename($filename)
  97. Returns the compression method that is likely used on the indicated
  98. filename based on its file extension.
  99. =cut
  100. sub compression_guess_from_filename {
  101. my $filename = shift;
  102. foreach my $comp (compression_get_list()) {
  103. my $ext = compression_get_property($comp, "file_ext");
  104. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  105. return $comp;
  106. }
  107. }
  108. return undef;
  109. }
  110. =item my $comp = compression_get_default()
  111. Return the default compression method. It's "gzip" unless
  112. C<compression_set_default> has been used to change it.
  113. =item compression_set_default($comp)
  114. Change the default compression methode. Errors out if the
  115. given compression method is not supported.
  116. =cut
  117. sub compression_get_default {
  118. return $default_compression;
  119. }
  120. sub compression_set_default {
  121. my ($method) = @_;
  122. error(_g("%s is not a supported compression"), $method)
  123. unless compression_is_supported($method);
  124. $default_compression = $method;
  125. }
  126. =item my $level = compression_get_default_level()
  127. Return the default compression level used when compressing data. It's "9"
  128. unless C<compression_set_default_level> has been used to change it.
  129. =item compression_set_default_level($level)
  130. Change the default compression level. Errors out if the
  131. level is not valid (see C<compression_is_valid_level>).
  132. either a number between 1 and 9 or "fast"
  133. or "best".
  134. =cut
  135. sub compression_get_default_level {
  136. return $default_compression_level;
  137. }
  138. sub compression_set_default_level {
  139. my ($level) = @_;
  140. error(_g("%s is not a compression level"), $level)
  141. unless compression_is_valid_level($level);
  142. $default_compression_level = $level;
  143. }
  144. =item compression_is_valid_level($level)
  145. Returns a boolean indicating whether $level is a valid compression level
  146. (it must be either a number between 1 and 9 or "fast" or "best")
  147. =cut
  148. sub compression_is_valid_level {
  149. my ($level) = @_;
  150. return $level =~ /^([1-9]|fast|best)$/;
  151. }
  152. =back
  153. =head1 AUTHOR
  154. Raphaël Hertzog <hertzog@debian.org>.
  155. =cut
  156. 1;