-
Notifications
You must be signed in to change notification settings - Fork 1
/
unzip-encoding.pl
executable file
·86 lines (64 loc) · 1.63 KB
/
unzip-encoding.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Data::Dumper;
use Encode;
use File::stat;
use Getopt::Std;
# CPAN:
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Digest::CRC;
#
sub HELP_MESSAGE {
my $fh = shift;
print {$fh} <<'HELP';
Options:
-f[FILENAME] : zip filename.
-e[ENCODING] : filename encoding in the zip, default=euc-kr.
Exiting
HELP
exit 0;
}
sub crc32_file {
my $fn = shift;
my $ctx = Digest::CRC->new( type => 'crc32' );
open my $fh, '<:raw', $fn or die $!;
$ctx->addfile(*$fh);
close $fh;
return $ctx->digest;
}
sub unzip_with_encoding {
my $fn = shift;
my $enc = shift;
local $Archive::Zip::UNICODE = 0;
binmode STDOUT, ":utf8";
my $readzip = Archive::Zip->new();
unless ( $readzip->read($fn) == AZ_OK ) {
die 'read error';
}
my @members = $readzip->members();
foreach my $member (@members) {
my $mem_fn = $member->fileName;
my $mem_fn_2 = decode('euc-kr', $mem_fn);
my $size = $member->uncompressedSize();
my $crc = $member->crc32();
print 'Extracting: ', $mem_fn_2, "\n";
$member->extractToFileNamed($mem_fn_2);
next if $size == 0;
# check the size and the crc32:
my $filesize = stat($mem_fn_2)->size;
$filesize == $size or warn "Size mismatch: extracted($filesize) <=> archived($size)";
my $crc_actual = crc32_file($mem_fn_2);
$crc_actual == $crc or warn "CRC32 mismatch";
}
}
{
my %opts = (
'e' => 'euc-kr',
);
getopts('e:f:', \%opts);
die "Specify a zip-file. (-f)" unless exists $opts{f};
print("Zip-file($opts{f}) with Encoding($opts{e}) ...\n");
unzip_with_encoding($opts{f}, $opts{e});
}