如何使用perl压缩特定文件夹中的单个文件夹?

时间:2014-11-25 03:40:52

标签: regex

请有人帮我解决这个问题.....

我的文件夹在D:\ Zip中说A,B,C。 我需要输入A.zip,B.zip& C.拉链。以下是我试过的代码。 代码:

enter code here
#!/usr/bin/perl
use strict;
use Cwd;
use File::Copy;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use IO::Compress::Zip qw(zip $ZipError) ;

use File::Basename 'basename';
my $path1=getcwd;
my $zipfile=getcwd."\.zip";
my $zip;

opendir(DIR, "$path1\/");
my $mainFolder=grep(/"$path1\/"/,readdir(DIR));
close DIR;

#### Zip Module #####
my $f2;
my $newzipname=$mainFolder;
print $mainFolder."\n";

foreach my $f($mainFolder)
{
print $f."\n";  
print $mainFolder."asfsd\n";    
my $zip=Archive::Zip->new();
$zip->addTree( "$f" );
die 'write error' unless $zip->writeToFileNamed($path1.'.zip') == AZ_OK;
exit 0;
}

1 个答案:

答案 0 :(得分:1)

这对你有用吗?

#!/usr/bin/perl
use strict;
use Cwd;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Spec;

my $path=getcwd;

opendir(DIR, $path);

while (my $entry = readdir DIR) {
    my $fullpath = File::Spec->catdir($path,$entry);
    next unless -d $fullpath;
    next if $entry eq '.' or $entry eq '..';
    my $zip=Archive::Zip->new();
    $zip->addTree($fullpath);
    die 'write error' unless $zip->writeToFileNamed($fullpath.'.zip') == AZ_OK;
}

close DIR;