- 论坛徽章:
- 0
|
[ 本帖最后由 hmlijg 于 2014-06-14 02:00 编辑 ]
从一个网友的帖子里复制的perl代码,他提供的exe文件中文显示正常,我用他的代码重新生成exe文件后中文显示为乱码(图片在帖子末尾),请问各位高手,这是什么原因?
源代码:
#!/usr/bin/perl
# Written by Limeng
# April 1, 2012 Changsha
# 仅扫描大小相同的文件,大量节省扫描时间
# 改进耗时显示格式
# 改写GUI
use strict;
#use warnings;
use Win32::GUI qw( MB_ICONINFORMATION MB_OK );
use Win32::GUI::DropFiles();
use Encode qw/ encode /;
use Win32::OLE qw(in);
use Digest::MD5;
use Benchmark;
my $about= <<EOF;
/`\\ /`\\
(/\\ V /\\) Limeng's Clone Files Finder V3.0
/6 6\\ -- Written by Limeng o_*, April, 2012
>{= Y =}<
/'-^-'\\ All Rights Reserved 2010-3000 ChangSha
(_)""-(_) Homepage: [url=http://limeng.class22.net]http://limeng.class22.net[/url]
E-mail: iamlimeng\@163.com
BlueIdea!
EOF
my $clone_report_file;
my %paths;
my @file_list;
my $w_m = 700;
my $h_m = 400;
my $icon = new Win32::GUI::Icon("icon.ico");
my $wm_class = new Win32::GUI::Class(
-name => 'Limeng',
-icon => $icon,
);
my $main = Win32::GUI::Window->new(
-title => " Limeng's Clone Files Finder V3.0",
-class => $wm_class,
-size => [$w_m,$h_m],
-maximizebox => 0,
-dialogui => 1,
-noflicker => 1,
-onResize => \&mwResize,
);
$main->AddLabel(
-name => 'L1',
-pos => [($w_m-190)/2,15],
-size => [$w_m,20],
-text => "请将要查找的目录拖拽进下面的框!",
);
$main->AddButton(
-name => 'B1',
-pos => [150,$h_m-70],
-text => " 开 始 分 析 ",
-default => 1,
-ok => 1,
-visible => 1,
-onClick => \&FindClone,
);
$main->AddButton(
-name => 'B2',
-pos => [300,$h_m-70],
-text => " 分 析 报 告 ",
-default => 1,
-ok => 1,
-visible => 1,
-onClick => \&OpenReport,
);
$main->AddButton(
-name => 'B3',
-pos => [450,$h_m-70],
-text => " 关 于 程 序 ",
-default => 1,
-ok => 1,
-visible => 1,
-onClick => \&About,
);
$main->AddListbox(
-name => 'LB',
-pos => [10,40],
-vscroll => 1,
-acceptfiles => 1,
-onClick => \&changeTitle,
-onDblClick => \&deleteSelect,
-onDropFiles => \&gotDrop,
);
my ($DOS) = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($DOS);
$main->B2->Disable();
$main->Center();
#$main->Maximize();
$main->Show();
Win32::GUI::Dialog();
$main->Hide();
undef $main;
exit(0);
sub gotDrop {
my ($self, $dropObj) = @_;
my @files = $dropObj->GetDroppedFiles();
foreach (sort @files) {
my $full_name = encode('gbk',$_);
if (-d $full_name) {
$self->Add($full_name) if (!$paths{$full_name});
$paths{$full_name} = 1;
}
}
return 0;
}
sub deleteSelect {
my $self = shift;
my $item = $self->GetText($self->GetCaretIndex());
delete($paths{$item});
$self->RemoveItem($self->GetCaretIndex());
return 0;
}
sub changeTitle {
my $self = shift;
$main->L1->Change(-text => '双击选中的项,可将其从列表中删除!');
return 0;
}
sub mwResize {
my $self = shift;
$self->LB->Resize($self->ScaleWidth()-20,280);
return 1;
}
sub About {
my $self = shift;
$self->MessageBox("$about","关于本程序...",MB_ICONINFORMATION | MB_OK,);
return 0;
}
sub OpenReport {
my $self = shift;
$main->B1->Change(-text => " 开 始 分 析 ");
$main->B1->Enable();
if (-e $clone_report_file) { system("$clone_report_file"); }
else {
$self->MessageBox("指定的目录未发现重复文件!","分析报告...",MB_ICONINFORMATION | MB_OK,);
}
return 0;
}
sub FindClone {
my $self = shift;
my $time = &get_time;
my $error_file = "错误信息报告_$time.txt";
$clone_report_file = "重复文件报告_$time.txt";
my $TT0 = new Benchmark;
$main->B1->Disable();
$main->B2->Disable();
my @error;
#获得目录清单
my @path_list;
my $list = $main->LB->GetCount();
foreach (0 .. $list-1) {
push(@path_list,$main->LB->GetText($_));
}
# scan files from the given directories
$main->B1->Change(-text => '扫描目录...');
@file_list = ();
foreach (@path_list) { readsub($_); }
# search files with the same size
my $n = 1;
my %same_size;
foreach my $file(@file_list) {
my $percent = int($n/($#file_list+1)*100);
$main->B1->Change(-text => "分析中...$percent %");
my @stat = stat($file);
push(@{$same_size{$stat[7]}},$file);
$n++;
}
my @files_with_same_size;
foreach (keys %same_size) {
if (@{$same_size{$_}} > 1) {
push(@files_with_same_size,@{$same_size{$_}});
}
}
my $clone;
if (@files_with_same_size) {
my $same_size_total = $#files_with_same_size+1;
# Compare files with the same size
my $n = 1;
my %clone;
foreach my $file(@files_with_same_size) {
my $percent = int($n/($same_size_total+1)*100);
$main->B1->Change(-text => "比较中...$percent %");
open(FILE, $file) or push(@error,"$file\t$!");
binmode(FILE);
my $md5 = Digest::MD5->new->addfile(*FILE)->hexdigest();
close FILE;
push(@{$clone{$md5}},$file);
$n++;
}
# generate clone report
my $check = 0;
foreach (keys %clone) {
if (@{$clone{$_}} > 1) {
$check++;
last;
}
}
if ($check) {
open FH, ">$clone_report_file";
print FH "已分析目录:\n";
foreach (@path_list) { print FH "$_\n"; }
print FH "\n";
print FH "创建时间 \t大小\t文件名\n";
print FH "="x100,"\n";
foreach (keys %clone) {
if (@{$clone{$_}} > 1) {
foreach(@{$clone{$_}}) {
my @stat = stat($_);
print FH time_transfer($stat[9]),"\t",int($stat[7]/1024+0.5)," KB\t$_\n";
}
print FH "\n";
$clone++;
}
}
}
}
$main->B1->Change(-text => " 分 析 结 束 ");
# return analyze message
my $message;
if ($clone) {
$message = "共发现 $clone 组重复文件,详情请点击\"分析报告\"!";
print FH "共发现 $clone 组重复文件!";
close FH;
$main->B2->Enable();
}
else {
$message = "指定的目录未发现重复文件!";
$main->B1->Enable();
$main->B2->Disable();
}
if (@error) {
open FH,">$error_file";
foreach (@error) { print FH "$_\n"; }
close FH;
my $errors = $#error+1;
$message .= "\n\n发生了 $errors 个错误,详见 \"$error_file\"!";
}
# display the time spent
my $TT1 = new Benchmark;
my $td = Benchmark::timediff($TT1, $TT0);
$td = Benchmark::timestr($td);
my ($sec) = ($td =~ /(\d+).*/);
my $time_display;
if ($sec >= 60) {
my $hour;
my $minute = int($sec/60);
if ($minute >= 60) {
$hour = int($minute/60);
$minute = $minute % 60;
}
my $second = $sec % 60;
$time_display .= " $hour 小时" if ($hour);
$time_display .= " $minute 分钟" if ($minute);
$time_display .= " $second 秒" if ($second);
}
else { $time_display = " $sec 秒"; }
$message .= "\n\n分析耗时:$time_display";
$self->MessageBox("$message","分析报告...",MB_ICONINFORMATION | MB_OK,);
return 0;
}
sub readsub
{
my $file_t = shift;
if (-f $file_t) {
push(@file_list,$file_t);
}
if (-d $file_t) {
opendir(AA,$file_t);
my @list = readdir(AA);
close (AA);
my $file_to_act;
foreach $file_to_act (sort @list)
{
if ($file_to_act =~ /^\.|\.$/) { next; }
else
{
readsub("$file_t/$file_to_act");
}
}
}
}
sub get_time {
my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday) = (localtime(time()));
#$sec = ($sec < 10)? "0$sec":$sec;
$min = ($min < 10)? "0$min":$min;
$hour = ($hour < 10)? "0$hour":$hour;
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? "0".($mon+1):($mon+1);
$year += 1900;
return("$year-$mon-$day $hour.$min");
}
sub time_transfer {
my $time_str = shift;
my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday) = (localtime($time_str));
#$sec = ($sec < 10)? "0$sec":$sec;
$min = ($min < 10)? "0$min":$min;
$hour = ($hour < 10)? "0$hour":$hour;
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? "0".($mon+1):($mon+1);
$year += 1900;
return("$year-$mon-$day $hour:$min");
} |
|