自己用的是日文的linux系统,昨天从同事那儿收了个压缩文件,结果解压之后文件名都乱吗了(内容正常显示)。网上google了以下发现是因为windows下默认的编码方式时Shift-JIS,到了linux下之后虽然内容可以显示但是文件名却乱码。解决方法也挺简单的,perl提供了一些方法,直接写一个简短的程序即可。
我也是从其他地方参照的,给一个例子程序。(这里需要安装Archive::Zip模块)
#!/usr/bin/perl
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Encode qw/from_to/;
use Getopt::Long;
use DateTime;
use DateTime::TimeZone::Local;
GetOptions('list|l' => my $mode_list);
if (scalar @ARGV < 1) {
print “Usage: $0 [-l]
exit 1;
}
my $zipfile = shift @ARGV;
my $zip = Archive::Zip->new();
unless ( $zip->read($zipfile) == AZ_OK ) {
die 'read error';
}
print “Archive: $zipfilen“;
if ($mode_list) {
print < Length Date Time Name -------- ---------- ----- ---- HERE } my $total_size = 0; my @members = $zip->members(); my $members_count = scalar @members; foreach my $member (@members) { my $filename = $member->fileName; from_to($filename, 'cp932', 'utf8'); if ($mode_list) { my $dt = DateTime->from_epoch( epoch => $member->lastModTime )->set_time_zone(DateTime::TimeZone::Local->TimeZone()); $total_size += $member->uncompressedSize; printf “%9d %s %sn“, $member->uncompressedSize, $dt->ymd('-') . ' ' . sprintf(“%02d“, $dt->hour) . ':' . sprintf(“%02d“, $dt->min), $filename; } else { print “ inflating: $filenamen“; unless (defined $zip->extractMember($member, $filename)) { warn “ failed: $filenamen“; } } } my $members_count_str = “$members_count file“ . ($members_count > 1 ? 's' : ''); if ($mode_list) { print < -------- ------- HERE printf “%9d %sn“, $total_size, $members_count_str; }