Compression.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. use base qw(Exporter);
  19. our @EXPORT = qw($compression_re_file_ext compression_get_list
  20. compression_is_supported compression_get_property
  21. compression_guess_from_filename);
  22. =head1 NAME
  23. Dpkg::Compression - simple database of available compression methods
  24. =head1 DESCRIPTION
  25. This modules provides a few public funcions and a public regex to
  26. interact with the set of supported compression methods.
  27. =head1 EXPORTED VARIABLES
  28. =over 4
  29. =cut
  30. my $COMP = {
  31. "gzip" => {
  32. "file_ext" => "gz",
  33. "comp_prog" => "gzip",
  34. "decomp_prog" => "gunzip",
  35. },
  36. "bzip2" => {
  37. "file_ext" => "bz2",
  38. "comp_prog" => "bzip2",
  39. "decomp_prog" => "bunzip2",
  40. },
  41. "lzma" => {
  42. "file_ext" => "lzma",
  43. "comp_prog" => "lzma",
  44. "decomp_prog" => "unlzma",
  45. },
  46. "xz" => {
  47. "file_ext" => "xz",
  48. "comp_prog" => "xz",
  49. "decomp_prog" => "unxz",
  50. },
  51. };
  52. =item $compression_re_file_ext
  53. A regex that matches a file extension of a file compressed with one of the
  54. supported compression methods.
  55. =back
  56. =cut
  57. my $regex = join "|", map { $_->{"file_ext"} } values %$COMP;
  58. our $compression_re_file_ext = qr/(?:$regex)/;
  59. =head1 EXPORTED FUNCTIONS
  60. =over 4
  61. =item my @list = compression_get_list()
  62. Returns a list of supported compression methods (sorted alphabetically).
  63. =cut
  64. sub compression_get_list {
  65. return sort keys %$COMP;
  66. }
  67. =item compression_is_supported($comp)
  68. Returns a boolean indicating whether the give compression method is
  69. known and supported.
  70. =cut
  71. sub compression_is_supported {
  72. return exists $COMP->{$_[0]};
  73. }
  74. =item compression_get_property($comp, $property)
  75. Returns the requested property of the compression method. Returns undef if
  76. either the property or the compression method doesn't exist. Valid
  77. properties currently include "file_ext" for the file extension,
  78. "comp_prog" for the name of the compression program and "decomp_prog" for
  79. the name of the decompression program.
  80. =cut
  81. sub compression_get_property {
  82. my ($comp, $property) = @_;
  83. return undef unless compression_is_supported($comp);
  84. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  85. return undef;
  86. }
  87. =item compression_guess_from_filename($filename)
  88. Returns the compression method that is likely used on the indicated
  89. filename based on its file extension.
  90. =cut
  91. sub compression_guess_from_filename {
  92. my $filename = shift;
  93. foreach my $comp (compression_get_list()) {
  94. my $ext = compression_get_property($comp, "file_ext");
  95. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  96. return $comp;
  97. }
  98. }
  99. return undef;
  100. }
  101. =back
  102. =head1 AUTHOR
  103. Raphaël Hertzog <hertzog@debian.org>.
  104. =cut
  105. 1;