Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31896
  • 博文数量: 8
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-26 14:10
文章分类

全部博文(8)

文章存档

2014年(1)

2013年(3)

2012年(4)

我的朋友

分类: Python/Ruby

2012-11-28 23:35:20

CSDN解答,移来备忘

一个新文件夹中和一个旧文件夹,里面的大部分内容相同,因为是实现更新的,两个文件夹里面都分了很多的文件和子目录,现在要把新文件夹里面的文件和旧文件夹里面的文件不同的给抓下来,放到一个zip包里面,文件的路径不变,比如,旧文件夹中有1,2,3,4,5,6文件,新文件夹中有1,2,3,4,5,6,7,8文件,里面3,4文件名相同,但内容不同,要把3,4,7,8给copy下来放在一个zip包里面

点击(此处)折叠或打开

  1. use strict;
  2. use warnings;
  3. use File::Find;
  4. use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  5. use File::Basename;
  6. use Text::Diff;
  7.  
  8. my $oldroot = './test/old'; #old folder
  9. my $newroot = './test/new'; #new folder
  10. my $zip_file = './test/updated/updated.zip'; #zip folder
  11.  
  12. my %old;
  13. my @updated_lists;
  14.  
  15. find(\&handle_olddir, $oldroot);
  16. find(\&handle_newdir, $newroot);
  17. archive_files(@updated_lists);
  18.  
  19. sub handle_olddir
  20. {
  21.     my $file = $File::Find::name;
  22.     $old{$file} = 1 if -f $file;
  23. }
  24.  
  25. sub handle_newdir
  26. {
  27.     my $file = $File::Find::name;
  28.     return if -d $file;
  29.      
  30.     my $tmpfile = $file;
  31.     $tmpfile =~ s/new/old/;
  32.     if($old{$tmpfile})
  33.     {
  34.         my $diff = diff $_, $tmpfile, { STYLE => "Context" };
  35.         return unless length $diff;
  36.     }
  37.     push @updated_lists, $file;
  38. }
  39.  
  40. sub archive_files
  41. {
  42.     my @files = @_;
  43.     if ($#files == -1)
  44.     {
  45.         print "No updated files!";
  46.         return;
  47.     }
  48.      
  49.     my $zip = Archive::Zip->new();
  50.     for(@files)
  51.     {
  52.         my $achive_path = $_;
  53.         $achive_path =~ s/\.\/test\/new//;
  54.         $zip->addFile( $_, $achive_path);
  55.     }
  56.      
  57.     if (-e "$zip_file")
  58.     {
  59.         print "Could not write to $zip_file: $!" unless $zip->overwrite() == AZ_OK;
  60.     }
  61.     else
  62.     {
  63.         print "Could not write to $zip_file: $!" unless $zip->writeToFileNamed("$zip_file") == AZ_OK;
  64.     }
  65. }

阅读(895) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Perl插入sqlserver数据库方法比较

给主人留下些什么吧!~~