| 1 | package Filename::Type::Compressed; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 10 | 2 | our $DATE = '2024-12-20'; # DATE | |||||||||||
| 11 | 2 | our $DIST = 'Filename-Type-Compressed'; # DIST | |||||||||||
| 12 | 2 | our $VERSION = '0.051'; # VERSION | |||||||||||
| 13 | |||||||||||||
| 14 | 2 | our @EXPORT_OK = qw(check_compressed_filename); | |||||||||||
| 15 | #list_compressor_suffixes | ||||||||||||
| 16 | |||||||||||||
| 17 | 2 | our %SUFFIXES = ( | |||||||||||
| 18 | '.Z' => {name=>'NCompress'}, | ||||||||||||
| 19 | '.gz' => {name=>'Gzip'}, | ||||||||||||
| 20 | '.bz2' => {name=>'Bzip2'}, | ||||||||||||
| 21 | '.xz' => {name=>'XZ'}, | ||||||||||||
| 22 | '.lz' => {name=>'LZ'}, | ||||||||||||
| 23 | '.lzma'=> {name=>'LZMA'}, | ||||||||||||
| 24 | '.zst' => {name=>'Zstandard'}, | ||||||||||||
| 25 | '.br' => {name=>'Brotli'}, | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | 2 | our %COMPRESSORS = ( | |||||||||||
| 29 | NCompress => { | ||||||||||||
| 30 | # all programs mentioned here must accept filename(s) as arguments. | ||||||||||||
| 31 | # preferably CLI. | ||||||||||||
| 32 | compressor_programs => [ | ||||||||||||
| 33 | {name => 'compress', opts => ''}, | ||||||||||||
| 34 | ], | ||||||||||||
| 35 | decompressor_programs => [ | ||||||||||||
| 36 | {name => 'uncompress', opts => ''}, | ||||||||||||
| 37 | ], | ||||||||||||
| 38 | }, | ||||||||||||
| 39 | Gzip => { | ||||||||||||
| 40 | compressor_programs => [ | ||||||||||||
| 41 | {name => 'gzip', opts => ''}, | ||||||||||||
| 42 | ], | ||||||||||||
| 43 | decompressor_programs => [ | ||||||||||||
| 44 | {name => 'gzip', opts => '-d'}, | ||||||||||||
| 45 | {name => 'gunzip', opts => ''}, | ||||||||||||
| 46 | ], | ||||||||||||
| 47 | }, | ||||||||||||
| 48 | Bzip2 => { | ||||||||||||
| 49 | compressor_programs => [ | ||||||||||||
| 50 | {name => 'bzip2', opts => ''}, | ||||||||||||
| 51 | ], | ||||||||||||
| 52 | decompressor_programs => [ | ||||||||||||
| 53 | {name => 'bzip2', opts => '-d'}, | ||||||||||||
| 54 | {name => 'bunzip2', opts => ''}, | ||||||||||||
| 55 | ], | ||||||||||||
| 56 | }, | ||||||||||||
| 57 | XZ => { | ||||||||||||
| 58 | compressor_programs => [ | ||||||||||||
| 59 | {name => 'xz', opts => ''}, | ||||||||||||
| 60 | ], | ||||||||||||
| 61 | decompressor_programs => [ | ||||||||||||
| 62 | {name => 'xz', opts => '-d'}, | ||||||||||||
| 63 | {name => 'unxz', opts => ''}, | ||||||||||||
| 64 | ], | ||||||||||||
| 65 | }, | ||||||||||||
| 66 | Zstandard => { | ||||||||||||
| 67 | compressor_programs => [ | ||||||||||||
| 68 | {name => 'zstd', opts => ''}, | ||||||||||||
| 69 | ], | ||||||||||||
| 70 | decompressor_programs => [ | ||||||||||||
| 71 | {name => 'zstd', opts => '-d'}, | ||||||||||||
| 72 | {name => 'unzstd', opts => ''}, | ||||||||||||
| 73 | ], | ||||||||||||
| 74 | }, | ||||||||||||
| 75 | Brotli => { | ||||||||||||
| 76 | compressor_programs => [ | ||||||||||||
| 77 | ], | ||||||||||||
| 78 | decompressor_programs => [ | ||||||||||||
| 79 | ], | ||||||||||||
| 80 | }, | ||||||||||||
| 81 | LZ => { | ||||||||||||
| 82 | compressor_programs => [ | ||||||||||||
| 83 | ], | ||||||||||||
| 84 | decompressor_programs => [ | ||||||||||||
| 85 | ], | ||||||||||||
| 86 | }, | ||||||||||||
| 87 | LZMA => { | ||||||||||||
| 88 | compressor_programs => [ | ||||||||||||
| 89 | {name => 'lzma', opts => ''}, | ||||||||||||
| 90 | ], | ||||||||||||
| 91 | decompressor_programs => [ | ||||||||||||
| 92 | {name => 'lzma', opts => '-d'}, | ||||||||||||
| 93 | {name => 'unlzma', opts => ''}, | ||||||||||||
| 94 | ], | ||||||||||||
| 95 | }, | ||||||||||||
| 96 | ); | ||||||||||||
| 97 | |||||||||||||
| 98 | 2 | our %SPEC; | |||||||||||
| 99 | |||||||||||||
| 100 | $SPEC{check_compressed_filename} = { | ||||||||||||
| 101 | 2 | v => 1.1, | |||||||||||
| 102 | summary => 'Check whether filename indicates being compressed', | ||||||||||||
| 103 | description => <<'_', | ||||||||||||
| 104 | |||||||||||||
| 105 | |||||||||||||
| 106 | _ | ||||||||||||
| 107 | args => { | ||||||||||||
| 108 | filename => { | ||||||||||||
| 109 | schema => 'str*', | ||||||||||||
| 110 | req => 1, | ||||||||||||
| 111 | pos => 0, | ||||||||||||
| 112 | }, | ||||||||||||
| 113 | # recurse? | ||||||||||||
| 114 | ignore_case => { | ||||||||||||
| 115 | summary => 'Whether to match case-insensitively', | ||||||||||||
| 116 | schema => 'bool', | ||||||||||||
| 117 | default => 1, | ||||||||||||
| 118 | }, | ||||||||||||
| 119 | }, | ||||||||||||
| 120 | result_naked => 1, | ||||||||||||
| 121 | result => { | ||||||||||||
| 122 | schema => ['any*', of=>['bool*', 'hash*']], | ||||||||||||
| 123 | description => <<'_', | ||||||||||||
| 124 | |||||||||||||
| 125 | Return false if no compressor suffixes detected. Otherwise return a hash of | ||||||||||||
| 126 | information, which contains these keys: `compressor_name`, `compressor_suffix`, | ||||||||||||
| 127 | `uncompressed_filename`. | ||||||||||||
| 128 | |||||||||||||
| 129 | _ | ||||||||||||
| 130 | }, | ||||||||||||
| 131 | examples => [ | ||||||||||||
| 132 | { | ||||||||||||
| 133 | args => {filename=>'foo.bar'}, | ||||||||||||
| 134 | }, | ||||||||||||
| 135 | { | ||||||||||||
| 136 | args => {filename=>'baz.xz'}, | ||||||||||||
| 137 | }, | ||||||||||||
| 138 | { | ||||||||||||
| 139 | args => {filename=>'qux.Bz2'}, | ||||||||||||
| 140 | }, | ||||||||||||
| 141 | ], | ||||||||||||
| 142 | }; | ||||||||||||
| 143 | 7 | ▶ | sub check_compressed_filename { | ||||||||||
|
Subroutine
check_compressed_filename: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 144 | 7 | my %args = @_; | |||||||||||
| 145 | |||||||||||||
| 146 | 7 | my $filename = $args{filename}; | |||||||||||
| 147 | 7 | ▶ | $filename =~ /(\.\w+)\z/ or return 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 7 | ▶ | my $ci = $args{ignore_case} // 1; | ||||||||||
|
Condition100%2/2
$args{"ignore_case"} // 1
MC/DC100%1/1
$args{"ignore_case"} // 1
$args{"ignore_case"}
Truth table100%2/2
A
A= $args{"ignore_case"}
| |||||||||||||
| 149 | |||||||||||||
| 150 | 7 | my $suffix = $1; | |||||||||||
| 151 | |||||||||||||
| 152 | 7 | my $spec; | |||||||||||
| 153 | 7 | ▶ | if ($ci) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | 6 | my $suffix_lc = lc($suffix); | |||||||||||
| 155 | 6 | for (keys %SUFFIXES) { | |||||||||||
| 156 | 35 | ▶ | if (lc($_) eq $suffix_lc) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 5 | $spec = $SUFFIXES{$_}; | |||||||||||
| 158 | 5 | last; | |||||||||||
| 159 | } | ||||||||||||
| 160 | } | ||||||||||||
| 161 | } else { | ||||||||||||
| 162 | 1 | $spec = $SUFFIXES{$suffix}; | |||||||||||
| 163 | } | ||||||||||||
| 164 | 7 | ▶ | return 0 unless $spec; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | |||||||||||||
| 166 | 5 | (my $ufilename = $filename) =~ s/\.\w+\z//; | |||||||||||
| 167 | |||||||||||||
| 168 | return { | ||||||||||||
| 169 | compressor_name => $spec->{name}, | ||||||||||||
| 170 | 5 | compressor_suffix => $suffix, | |||||||||||
| 171 | uncompressed_filename => $ufilename, | ||||||||||||
| 172 | }; | ||||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 2 | 1; | |||||||||||
| 176 | # ABSTRACT: Check whether filename indicates being compressed | ||||||||||||
| 177 | |||||||||||||
| 178 | __END__ | ||||||||||||