package ZipUnzip;
#实例化一个对象
sub new
{
my $class = shift;
#一个空的引用
my $ref = {};
#将引用和对象绑定
bless($ref,$class);
#返回引用
return $ref;
}
#压缩文件(注意在桌面调用会失败,不知道为啥????)
sub ZipFile
{
#获得对象引用
my $ref = shift;
#获得压缩的文件名
my $ZipFileName = shift;
#要压缩的文件
my @Content = @_;
#文件名字处理
$ZipFileName =~ s/^\s+//;
$ZipFileName =~ s/\s+$//;
$ZipFileName =~ s/\\/\\\\/g;
#不是已zip结尾报错
unless($ZipFileName =~ m/\.zip$/i)
{
die "This file is not end with zip\n";
}
#没有要压缩的文件
my $count = @Content;
if(0 == $count)
{
die "there is no file to zip\n";
}
my $ConnectFile;
foreach my $File (@Content)
{
#文件名字处理
$File =~ s/^\s+//;
$File =~ s/\s+$//;
$File =~ s/\\/\\\\/g;
#要压缩的文件是否存在
unless(-e $File)
{
die "The File to zip does not exit\n";
}
$ConnectFile = $ConnectFile." ".$File;
}
my $commandline = "zip -j -D -q ". $ZipFileName." ".$ConnectFile;
print "command is $commandline\n";
system($commandline);
}
#解压缩命令
sub UnZipFIle
{
#获得对象引用
my $ref = shift;
#获得要解压的文件名
my $UnzipFile = shift;
#解压后保存的目录
my $SavaPath = shift;
#格式化路径
$UnzipFile =~ s/^\s+//;
$UnzipFile =~ s/\s+$//;
$UnzipFile =~ s/\\/\\\\/g;
$SavaPath =~ s/^\s+//;
$SavaPath =~ s/\s+$//;
$SavaPath =~ s/\\/\\\\/g;
#存在且是目录
unless(-e $SavaPath && -d $SavaPath)
{
die "the save path $SavaPath is wrong\n";
}
unless(-e $UnzipFile)
{
die "the unzip file does not exit $UnzipFile\n";
}
#是否以ZIP结尾
unless($UnzipFile =~ m/\.zip$/i)
{
die "the $Unzipfile does not exist\n";
}
#解压文件
my $commandline = "unzip -o -q -d ".$SavaPath."  ".$UnzipFile;
system($commandline);
}
#模块以1结尾
1;

 

 

 

 

use ZipUnzip;
#获取对象引用
my $ZIPFILE = ZipUnzip->new();
my $ZIPNAME = "E:\\压包解压包模块\\test.zip";
my @ZIPContent;
#要压缩的文件内容
push @ZIPContent,"E:\\压包解压包模块\\工参.xlsx";
push @ZIPContent,"E:\\压包解压包模块\\无错误工参.xlsx";
#压缩文件
ZipUnzip->ZipFile($ZIPNAME,@ZIPContent);
#解压文件
my $UNZIPPath = "E:\\压包解压包模块\\新建文件夹";
my $UNzipFile = "E:\\压包解压包模块\\test.zip";
ZipUnzip->UnZipFIle($UNzipFile,$UNZIPPath);