轉:MD5檢查程式碼 (perl)

來源:互聯網
上載者:User

使用了兩個不同的函數,在執行效率上checkmd5sum.pl 比checkmd5sum_find.pl要更快。

 

1.checkmd5sum.pl #!/usr/bin/perl # # File: checkmd5sum.pl # Author: amxku.net # Date: 2008-12- ...
兩個使用了不同的函數,在執行效率上checkmd5sum.pl 比checkmd5sum_find.pl要更快。
1.checkmd5sum.pl

#!/usr/bin/perl     
# File: checkmd5sum.pl   
# Author: amxku.net   
# Date: 2008-12-02   
# License: GPL-2   
  
use Digest::MD5;   
use IO::File;   
use POSIX qw(strftime);   
  
if( @ARGV != 1 ){   
     print "check files md5sum/nusage:<rootdir>/nexample:c://n";   
     exit;   
}   
  
$now_time=strftime "%Y-%m-%d %H:%M:%S", localtime;   
$md5sumfile_name=strftime "%Y_%m_%d_%H_%M_%S", localtime;   
  
open MD5FILE,">$md5sumfile_name.txt" or die "$!/n/n";   
print MD5FILE "# check files md5sum/n/n";   
print MD5FILE "# Created on $now_time/n";   
print MD5FILE "# amxku_at_msn.com/n/n";   
my $root = $ARGV[0];   
sub lsr_s($) {   
     my $cwd = shift;   
     my @dirs = ($cwd.'/');   
  
     my ($dir, $file);   
     while ($dir = pop(@dirs)) {   
         local *DH;   
         if (!opendir(DH, $dir)) {   
             warn "Cannot opendir $dir: $! $^E";   
             next;   
         }   
         foreach (readdir(DH)) {   
             if ($_ eq '.' || $_ eq '..') {   
                 next;   
             }   
             $file = $dir.$_;            
             if (!-l $file && -d _) {   
                 $file .= '/';   
                 push(@dirs, $file);   
             }   
             process($file, $dir);   
         }   
         closedir(DH);   
     }   
}   
  
my ($size, $dircnt, $filecnt) = (0, 0, 0);   
  
sub process($$) {   
     my $file = shift;   
     print $file, "/n";   
     if (substr($file, length($file)-1, 1) eq '/') {   
         $dircnt++;   
     }   
     else {   
         $filecnt++;   
         $size += -s $file;   
         my $fileinfo = genchksuminfo($file);   
         print MD5FILE "$fileinfo/n";   
     }   
}   
  
sub genchksuminfo   
{   
     my ($file) = @_;   
     my $chk = Digest::MD5->new();   
     my (@statinfo) = stat($file);   
     $chk->add(@statinfo[0,1,2,3,4,5,7,9,10]);   
     $chk->addfile(IO::File->new($file));   
     return sprintf("md5:%s/t%s",$chk->hexdigest,$file);   
}   
  
lsr_s("$root");   
print "/n$filecnt files, $dircnt directory. $size bytes./n";   
my $dateinfo ="/n$filecnt files, $dircnt directory. $size bytes./n";   
print MD5FILE "$dateinfo/n";   
close(MD5FILE);  
2.checkmd5sum_find.pl

#!/usr/bin/perl   
#   
# File: checkmd5sum_find.pl  
# Author: p0150 & amxku   
# License: GPL-2   
  
use File::Find;   
use Digest::MD5;   
use IO::File;   
use POSIX qw(strftime);   
  
if( @ARGV != 1 ){   
     print "usage:   <rootdir>   /n";   
     print "example:    c:/      /n";   
      exit;   
}   
$now_time=strftime "%Y-%m-%d %H:%M:%S", localtime;   
$now_string=strftime "%Y_%m_%d_%H_%M_%S", localtime;   
  
open MD5FILE,">$now_string.txt" or die "$!/n";   
print MD5FILE "#/n";   
print MD5FILE "# Created on $now_time/n";   
print MD5FILE "#/n";   
print MD5FILE "/n";   
chomp(my $dir=$ARGV[0]);   
find(/&wanted, $dir);          
sub wanted   
{   
unless (-d $_)   
{   
print $File::Find::dir.'/'.$_."/n";   
my $name=$File::Find::dir.'/'.$_;   
my $fileinfo = genchksuminfo($name);   
print MD5FILE "$fileinfo/n";   
}   
}   
  
sub genchksuminfo   
{   
     my ($file) = @_;   
     my $chk = Digest::MD5->new();   
     my (@statinfo) = stat($file);   
     $chk->add(@statinfo[0,1,2,3,4,5,7,9,10]);   
     $chk->addfile(IO::File->new($file));   
     return sprintf("md5:%s/t%s",$chk->hexdigest,$file);   
}   
close(MD5FILE);  

 

 

 原文來自連結:http://www.hackbase.com/tech/2009-11-03/57649.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.