使用 Archive::Zip 判断成员是否为文本文件

Use Archive::Zip to determine if a member is a text file or not

我正在编写一个脚本,当成员名称与模式匹配时,使用给定的搜索字符串将 grep zip 存档成员的内容。

我有以下处理单个存档的子程序(该脚本可以在命令行上获取多个存档):

sub processArchive($$$$) {
    my ($zip, $searchstr, $match, $zipName) = @_;
    print "zip[$zip] searchstr[$searchstr] match[$match] zipName[$zipName]\n";
    my @matchingList = $zip->membersMatching($match);
    my $len = @matchingList;
    if ($len > 0) {
       print $zipName . ":\n";
       for my $member (@matchingList) {
          print "member[$member]\n";
          print "textfile[" . $member->isTextFile() . "] contents[" . $member->contents() . "]\n";
          if ($member->isTextFile()) {
             print "Is a text file.\n";
          }
          else {
             print "Is not a text file.\n";
          }
          my @matchingLines = grep /$searchstr/, $member->contents();
          my $len = @matchingLines;
          if ($len > 0) {
             print @matchingLines;              
          }
      }
   }
}

逻辑还没有完成。我首先尝试调用 "isTextFile()" 以查看其作用。我一定是做错了什么,因为我得到了至少一个显然是文本文件的成员的 "Is not a text file"。

我还注意到,当我从 "isTextFile()" 打印 return 的值时,它始终是一个空字符串。这是我应该期望打印 "true" 或 "false" 值,还是这里有其他问题?

"text file" 状态是从 ZIP 文件中的标志中读取的。许多归档工具没有正确设置此标志,因为它很少使用并且不影响正常使用。

如果您确实需要检查文件是否包含文本,则需要将其解压缩并亲自查看。