[Perl]对*.zip文件操作的记录<一>
[Perl]对*.zip文件操作的记录<一>今天要对*.zip文件进行简单的操作,如解压缩,提取某只文件等。来这里记录下,以免忘记。 今天寫的腳本,大概會用到
以下開始用 code 記錄要用到的一些知識點。 先加个帽子 Archive::Zip : #!perl
use strict;
use warnings;
#報告更詳細的問題描述,等同 perl - Mdiagnostics ./my-program.pl
use diagnostics;
#Provide an interface to ZIP archive files.
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
read( )读取某个zip文件,这里读取文件someZip.zip: # Read a Zip file
my $somezip = Archive::Zip->new();
unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {
die 'read error';
}
someZip.zip压缩的文件内容如下图: memberNames( ) 列出zip文件中的文件及文件名,包括目录名。 #Return a list of the (internal) file names of the zip members
my @nameOfmembers = $somezip->memberNames();
print "n nameOfmembers :@nameOfmembersn";
显示结果: numberOfMembers( )返回zip文件中的文件及文件名的個數。 #Return the number of members I have
my $memberOfmembers = $somezip->numberOfMembers();
print "n memberOfmembers : $memberOfmembersn";
顯示結果: membersMatching( ) 查找匹配的個數。 #Return ref to member whose filename equals given filename or undef.
#'$string' must be in Zip (Unix) filename format.
my $memberMatching = $somezip->membersMatching('.*.txt');
print "n memberMatching: $memberMatchingn";
顯示結果: members( ) 返回zip文件中的文件及文件名的HASH。 #Return a copy of the members array
my @members = $somezip->members();
print "n members: @membersn";
显示结果: membersMatching( ) 返回zip文件中的匹配的文件及文件名的HASH。 my @textFileMembers = $somezip->membersMatching( '.*.txt' );
print "n textFileMembers: @textFileMembersn";
顯示結果: fileName( )返回 zip檔案名 #Returns the name of the file last read from.
#If nothing has been read yet,returns an empty string;
#if read from a file handle,returns the handle in string form.
print "n fileName:" . $somezip->fileName();
顯示結果: zipfileComment( ) 返回comment, 個人感覺沒有什麼用處,先記一筆。 print "n zipfileComment:" . $somezip->zipfileComment();
$somezip->zipfileComment( 'New Comment' );
print "n zipfileComment:" . $somezip->zipfileComment();
顯示結果: centralDirectorySize( ) 估計很少用不到這個功能。 #Returns central directory size,
#as read from an external zip file.
#Not used for writing zips,
#but might be interesting if you read a zip in.
print "n centralDirectorySize:" . $somezip->centralDirectorySize();
顯示結果: diskNumber( ) 只是記錄下有這個方法。 #Return the disk that I start on.
#Not used for writing zips,#but might be interesting if you read a zip in.
#This should be 0,as Archive::Zip does not handle multi-volume archives.
print "n diskNumber:" . $somezip->diskNumber();
顯示結果: extractTree( ) 将zip 档案中的所有文件解压缩出来,这功能会经常用到。 #If you don't give any arguments at all,#will extract all the files in the zip with their original names.
print "n status:" . $somezip->extractTree();
显示结果: 会将zip 档案中的folder目录下的文件解压缩出来。 print "n status:" . $somezip->extractTree("folder");
显示结果: extractMember( ) 解压缩匹配的文件。 # now extract the some files into the current directory
# Returns undef if member does not exist in this Zip
$somezip->extractMember( 'a.txt' );
$somezip->extractMember( 'folder/b.txt' );
print "n status:" . $somezip->extractMember( 'folder/c' );
顯示結果: 不是匹配的文件,返回错误2 print "n status:" . $somezip->extractMember( 'folder/d' );
显示结果: contents( )將匹配文件的內容讀出來。這個才是今天我要用到的功能。 # now read the contents from a file
print "n contents:" . $somezip->contents( 'folder/c' );
顯示結果: 最後,有一個extractTree( )的某一项功能的介紹,并無卵用,但还是附上格式和 instance。 $somezip->extractTree( [ $root,$dest,$volume } ] )
$somezip->extractTree( [ { root => $root,zipName => $dest,volume => $volume } ] )
For instance $somezip->extractTree( 'a' );
when applied to a zip containing the files: a/x a/b/c ax/d/e d/e will extract: $somezip->extractTree( 'a','d/e' );
will extract: $somezip->extractTree( 'a','d/e','f:' );
will extract: $somezip->extractTree( 'a','/d/e','f:' );
will extract: 以下為部分code: #!perl
use strict;
use warnings;
use diagnostics;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
#function for extract files and read the contents from the zip file
#writtem by ChampWang,HZ.
#ver:0.01
# Read a Zip file
my $somezip = Archive::Zip->new();
unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {
die 'read error';
}
#Return a list of the (internal) file names of the zip members
my @nameOfmembers = $somezip->memberNames();
print "n nameOfmembers :@nameOfmembersn";
# now read the contents from a file
print "n contents:" . $somezip->contents( 'folder/c' ) . "n";
創建,更新,追加zip文件的有關內容,以後有空再寫,這次記錄到此,打住。 Reference: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |