加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[Perl]对*.zip文件操作的记录<一>

发布时间:2020-12-15 23:42:39 所属栏目:大数据 来源:网络整理
导读:[Perl]对*.zip文件操作的记录一 今天要对*.zip文件进行简单的操作,如解压缩,提取某只文件等。来这里记录下,以免忘记。 今天寫的腳本,大概會用到 new( ) 讀取一個zip文件( 要分析的zip 文件目錄和文件比較固定 ), 再用memberNames( ) 來列出zip中的文

[Perl]对*.zip文件操作的记录<一>

今天要对*.zip文件进行简单的操作,如解压缩,提取某只文件等。来这里记录下,以免忘记。


今天寫的腳本,大概會用到

  1. new( ) 讀取一個zip文件(要分析的zip 文件目錄和文件比較固定),
  2. 再用memberNames( ) 來列出zip中的文件[名],
  3. 再來判斷某個文件是否存在。
  4. 如果存在,就用contents( )讀取其內容,
  5. 最後進行解析。

以下開始用 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压缩的文件内容如下图:

someZip.zip压缩的文件


memberNames( ) 列出zip文件中的文件及文件名,包括目录名。

#Return a list of the (internal) file names of the zip members
my @nameOfmembers = $somezip->memberNames();
print "n nameOfmembers :@nameOfmembersn";

显示结果:
nameOfmembers :
a.txt
folder/
folder/b.txt
folder/c


numberOfMembers( )返回zip文件中的文件及文件名的個數。

#Return the number of members I have
my $memberOfmembers = $somezip->numberOfMembers();
print "n memberOfmembers : $memberOfmembersn";

顯示結果:
memberOfmembers : 4


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

顯示結果:
memberMatching: 2


members( ) 返回zip文件中的文件及文件名的HASH。

#Return a copy of the members array
my @members = $somezip->members();
print "n members: @membersn";

显示结果:
members:
Archive::Zip::ZipFileMember=HASH(0x2d59594)
Archive::Zip::DirectoryMember=HASH(0x1e51104)
Archive::Zip::ZipFileMember=HASH(0x2d5ad64)
Archive::Zip::ZipFileMember=HASH(0x2d5afd4)


membersMatching( ) 返回zip文件中的匹配的文件及文件名的HASH。

my @textFileMembers = $somezip->membersMatching( '.*.txt' );
print "n textFileMembers: @textFileMembersn";

顯示結果:
textFileMembers:
Archive::Zip::ZipFileMember=HASH(0x2d59594)
Archive::Zip::ZipFileMember=HASH(0x2d5ad64)


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();

顯示結果:
fileName:someZip.zip


zipfileComment( ) 返回comment, 個人感覺沒有什麼用處,先記一筆。

print "n zipfileComment:" . $somezip->zipfileComment();
$somezip->zipfileComment( 'New Comment' );
print "n zipfileComment:" . $somezip->zipfileComment();

顯示結果:
zipfileComment:
zipfileComment:New Comment


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();

顯示結果:
centralDirectorySize:360


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();

顯示結果:
diskNumber:0


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();

显示结果:
status:0


会将zip 档案中的folder目录下的文件解压缩出来。

print "n status:" . $somezip->extractTree("folder");

显示结果:
status:0


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' );

顯示結果:
status:0


不是匹配的文件,返回错误2
(AZ_ERROR (2) There was some generic kind of error.)

print "n status:" . $somezip->extractMember( 'folder/d' );

显示结果:
status:2
error: member not found
at C:/Perl/lib/Archive/Zip/Archive.pm line 186.


contents( )將匹配文件的內容讀出來。這個才是今天我要用到的功能。

# now read the contents from a file
print "n contents:" . $somezip->contents( 'folder/c' );

顯示結果:
contents: writtem by ChampWang,20150708,HZ.


最後,有一個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:
a/x as ./x
a/b/c as ./b/c


$somezip->extractTree( 'a','d/e' );

will extract:
a/x to d/e/x
a/b/c to d/e/b/c and ignore ax/d/e and d/e


$somezip->extractTree( 'a','d/e','f:' );

will extract:
a/x to f:d/e/x
a/b/c to f:d/e/b/c and ignore ax/d/e and d/e


$somezip->extractTree( 'a','/d/e','f:' );

will extract:
a/x to f:dex
a/b/c to f:debc and ignore ax/d/e and d/e


以下為部分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:
http://search.cpan.org/~phred/Archive-Zip-1.48/lib/Archive/Zip.pm

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读