简体   繁体   中英

How to zip individual folders present in a particular folder using perl?

Can please someone help me out with this.....

I have my folders say A,B,C inside D:\\Zip. I need to zip A.zip, B.zip & C. zip. The below is the code i tried. CODE:

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;
}

Does this work for you?

#!/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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM