在perl中压缩文件

时间:2015-12-08 05:49:58

标签: perl

我一直试图在远程Windows服务器上压缩文件但是没有通过我尝试的任何方式取得成功。下面是一小段代码。请告诉我哪里出错了。此代码不会产生任何错误,只是不生成zip文件。

use strict;
use warnings;

# before running check perl module is installed in your PC.

use Archive::Zip;
use File::Basename 'basename';

my @files = ('D:\Scripts\Testing\abc.txt');    
    # if it is more than one file add it by using comma as separator

my $zip = Archive::Zip->new;

foreach my $file (@files) {
    my $member = basename $file;
    printf qq{Adding file "%s" as archive member "%s"\n}, $file, $member;
    $zip->addFile( $file, $member );
    printf "Member added\n";
}
printf "Writing to zip\n";
$zip->writeToFileNamed('zippedFolders.zip');
    #zip file name change it as u want

1 个答案:

答案 0 :(得分:0)

你可以请:

use Cwd;
use strict;
use warnings;
# before running check perl module is installed in your PC.
use Archive::Zip;
use File::Basename;

my (@files,$dirname,$bsename) = "";
my $inFile = "D:\\Scripts\\Testing\\abc.txt"; # if it is more than one file add it by using comma as separator
my $curdir = getcwd();

#Need to open file here and to be read the file
open(IN, $inFile) || die "Cant \n";
while(<IN>) {
    my $sngfile = $_;
    chomp($sngfile);
    push(@files, $sngfile);
}
my $zip = Archive::Zip->new();
foreach my $file (@files)
{
    $dirname = dirname($file);
    $bsename = basename($file);

    #Check file exist here your code
    if($dirname!~m/\.$/) {
    print "$dirname\t$bsename\n";
    #printf qq{Adding file "%s" as archive member "%s"\n}, $dirname, $bsename;
    $zip->addFile("$dirname/$bsename"); }
}
printf "$curdir\\Writing to zip\n";
$zip->writeToFileNamed("$curdir/zippedFolders.zip"); #zip file name change it as u want