Compression.pm 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Dpkg::Compression;
  14. use strict;
  15. use warnings;
  16. use base qw(Exporter);
  17. our @EXPORT = qw(@comp_supported %comp_supported %comp_ext $comp_regex
  18. %comp_prog %comp_decomp_prog
  19. get_compression_from_filename);
  20. our @comp_supported = qw(gzip bzip2 lzma xz);
  21. our %comp_supported = map { $_ => 1 } @comp_supported;
  22. our %comp_ext = (gzip => 'gz', bzip2 => 'bz2', lzma => 'lzma', xz => 'xz');
  23. our $comp_regex = '(?:gz|bz2|lzma|xz)';
  24. our %comp_prog = (gzip => 'gzip', bzip2 => 'bzip2', lzma => 'lzma',
  25. xz => 'xz');
  26. our %comp_decomp_prog = (gzip => 'gunzip', bzip2 => 'bunzip2', lzma => 'unlzma',
  27. xz => 'unxz');
  28. sub get_compression_from_filename {
  29. my $filename = shift;
  30. foreach my $comp (@comp_supported) {
  31. if ($filename =~ /^(.*)\.\Q$comp_ext{$comp}\E$/) {
  32. return $comp;
  33. }
  34. }
  35. return undef;
  36. }
  37. 1;