PERL 压缩与解压缩模块
发布时间:2020-12-16 00:02:37 所属栏目:大数据 来源:网络整理
导读:package ZipUnzip;#实例化一个对象sub new{my $class = shift;#一个空的引用my $ref = {};#将引用和对象绑定bless($ref,$class);#返回引用return $ref;}#压缩文件(注意在桌面调用会失败,不知道为啥????)sub ZipFile{#获得对象引用my $ref = shift;#获得
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 zipn"; } #没有要压缩的文件 my $count = @Content; if(0 == $count) { die "there is no file to zipn"; } 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 exitn"; } $ConnectFile = $ConnectFile." ".$File; } my $commandline = "zip -j -D -q ". $ZipFileName." ".$ConnectFile; print "command is $commandlinen"; 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 wrongn"; } unless(-e $UnzipFile) { die "the unzip file does not exit $UnzipFilen"; } #是否以ZIP结尾 unless($UnzipFile =~ m/.zip$/i) { die "the $Unzipfile does not existn"; } #解压文件 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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |