zoukankan      html  css  js  c++  java
  • Compress a Folder/Directory via Perl5

    Compress a Folder/Directory via Perl5

    tested in Windows, Mac OS X, Ubuntu16.04

    #!/usr/bin/perl    
    #压缩指定目录    
    #nultiple folder    
    use IO::Dir;    
    use Archive::Tar;    
    use v5.16;    
        
    die "Give me some Folder
    " if ! @ARGV;    
    &tar_bz2(@ARGV);    
        
    sub tar_bz2 {
    	my @haystack = ();
    	my $fname = join ',', map { $_=~s//$//;$_ } @_;
        #the compressed file name
    	$fname = @_.";".$fname;
    	while(my $dir = shift){ #iterate the given list
    		$dir =~ s|/$||;
            if (! -d $dir){
                push @haystack, $dir;
                #not a dir/folder, file hodoooor
                next
            }
    	    sub haystack {
    		    my $dir  = shift; # current dir name
    		    my $pat  = shift; # under where
    		    my $d = IO::Dir->new("$dir");
    		    if (defined($d)) {
    				chdir $dir;
    		        while (defined($_ = $d->read)) {
    		            next if ($_ =~ /^..?$/);
    				    if (-f "$_"){ 
    						push @haystack, "$pat/$dir/$_";
    					}
    		            if (-d "$_") {
    		                chmod 0755, "$_";
    		                &haystack($_,"$pat/$dir");
                            # __SUB__ not work in windows
                            # no matter `use feature "current_sub"`
                            # or `use v5.16`
                            # run under perl v5.18.2
    		            }
    		        }
    				chdir("..");    
    		    }
    		}    
    		&haystack("$dir",'.');    
        }    
    	$fname =~ s/[^A-Za-z0-9_-.;]+/_/g;    
    	$fname = substr($fname,0,15) if $fname > 14;
        # create a Archive::Tar object
        my $tar = Archive::Tar->new;
        # add files
    	$tar->add_files(@haystack) or die "$!";    
    	# say STDERR "$_" for (@haystack);    
    	$tar->write("$fname.tbz",COMPRESS_BZIP);    
    	say "Saved to $fname.tbz";    
    }
    

    So,

    $ perl ./backupRenLab.pl font/
    Saved to 1;font.tbz
    
    font/
      |-- A/
          |-- A File 2.txt
          |-- AFile1.txt
      |-- B/
          |-- C/
              |-- CFile.txt
     The sturcture
    

    More,

    • Options Getopt::Long -o(outdir),-t(compress method,gzip bz2,7z,xz),-f(tar filename), -l(compress level)
    天和地是灰色的,砖和瓦也是灰色的。临街的墙几经风化,几经修补,刷过黑灰、白灰,涂过红漆,书写过不同内容的标语,又终于被覆盖;风雨再把覆盖层胡乱地揭下来,形成一片斑驳的杂色,融汇于灰色的笼罩之中。路旁的树木苍黑,瓦楞中芳草青青。 远处,炊烟缭绕。迷蒙的曙色中,矗立着...
  • 相关阅读:
    coredump分析
    Sword LRU算法
    C++ STL迭代器失效问题
    Sword DB主从一致性的解决方法
    Sword CRC算法原理
    C语言 按位异或实现加法
    Linux 等待信号(sigsuspend)
    C语言 宏定义之可变参数
    Linux shell字符串操作
    C++ *和&
  • 原文地址:https://www.cnblogs.com/raybiolee/p/5635542.html
Copyright © 2011-2022 走看看