如何制作刚刚提取的zip文件的输出日志文件

时间:2017-10-20 14:28:07

标签: perl

我有一个程序将提取一个名为" output.zip"的zip文件。并为它创建一个目录。我想创建一个日志文件,如果我在这种情况下提取的任何文件中的某个单词是单词是Error。我得到的错误是说该文件不存在。我怎样才能解决这个问题?

#!/usr/bin/perl

 use strict;
 use warnings;

 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

 my $sSource = "/home/glork/output.zip";
 my $sDest = "/home/glork/zipped";



x_unzip($sSource,$sDest);

sub x_unzip {
my ($zip_file, $out_file, $filter) = @_;
my $zip = Archive::Zip->new($zip_file);
unless ($zip->extractTree($filter || '', $out_file) == AZ_OK) {
    warn "unzip not successful: $!\n";
  }
 }


open(LOGFILE, "/home/glork/zipped/var/log/*.log") or die "can't find file";

while(<LOGFILE>) {
    print "Error in line $.\n" if(/ERROR/);
}
close LOGFILE;

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效。您提取所有文件,保存名称。然后浏览每个文件以查找错误:

use strict;
use warnings;

use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

my $sSource = "/home/glork/output.zip";
my $sDest = "/home/glork/zipped";

my @extractedFiles;
my $zip = Archive::Zip->new($sSource);
foreach my $member ($zip->members) {
    next if $member->isDirectory;
    (my $extractName = $member->fileName) =~ s{.*/}{};
    $member->extractToFileNamed($sDest.'/'.$extractName);
    push @extractedFiles, $extractName;
    print "Extracted $sDest/$extractName\n";
}

foreach my $logFile (@extractedFiles) {
    open(LOGFILE, "$sDest/$logFile") or die "can't find file";
    while(<LOGFILE>) {
        print "Error in line $.\n" if(/ERROR/);
    }
    close LOGFILE;
}