-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.pl
executable file
·48 lines (44 loc) · 1.29 KB
/
unzip.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
use warnings;
use strict;
use Encode qw(decode encode);
use Encode::Locale;
use Encode::Detect;
use Archive::Zip 'AZ_OK';
use File::Path 'make_path';
print STDERR <<"BANNER";
Unzip.pl by AITap, 2013
Published under GPLv3+ or The "Artistic License"
BANNER
my $list_only = $ARGV[0] eq '-l' ? shift : 0;
die "Usage: $0 [-l] <file.zip> [another file.zip] ...\n" unless @ARGV;
for my $fname (@ARGV) {
my $zip = Archive::Zip::->new($fname) or die "$fname: $!";
for my $member ($zip->members) {
my $name = $member->fileName;
# print encode locale => decode ascii => $name, Encode::FB_PERLQQ;
my $target = encode locale => (do {
my $ans;
for (qw/locale Detect cp866/) {
$ans = eval { decode $_, $name, Encode::FB_CROAK } and last;
}
$ans;
} || eval { decode ascii => $name, Encode::FB_PERLQQ });
$target =~ tr{\\}{/}; # some ZIPs have \ as path separators
print "$target\n";
next if $list_only;
if ($target =~ m{/$}) { # again, fixed ZIPs
make_path $target; next
}
TRY: {
($_ = $zip->extractMember($member, $target)) == AZ_OK and last TRY;
if ($!{ENAMETOOLONG}) { # file name too long
my ($ext) = $target =~ /(\.[^.]+)$/;
substr($target,255-length($ext))=$ext;
redo;
} # other errors here
# TODO: IO::Prompt to ask what to do?
die $_;
}
}
}