压缩文件并在base perl中删除它们

时间:2016-06-02 05:56:27

标签: perl zip

我想压缩一定长度的文件,并在压缩成功后删除它们。我也只是删除没有压缩的文件。我正在使用下面的代码。删除部分工作正常,但没有发生压缩。

我使用的是base perl,无法使用Archive::ZipIO:Compress::Zip等模块。

    sub actionOnFile {
        my($fileArray_ref, $action, $directory, $zipname) = @_;
        my @fileArray = @$fileArray_ref;
        $action = uc($action);  #set action string to uppercase
        my $cDT = &currentDateTime;
        foreach my $filename (@fileArray) {
            if ($action eq "ZIPDEL") {  # zip and delete file
                my $zipCommand = "WZZIP -p -m \"$directory\\$zipname.$cDT.zip\" \"$directory\\$filename\"";
                open ZIP_PROCESS, "$zipCommand |" or $rcode=5;
                print LOGFILE "     ERROR:  Opening zip delete process for $filename: $! $?\n" if ($rcode == 5 || $!);
                while (<ZIP_PROCESS>) {
                    $rcode=3 if ($_=~/error/i or $_=~/warning/i);   
                    # the next line prevents standard WinZip version info from printing to log
                    my $resultLine = &ltrim($_) unless ($_=~/WinZip\(R\) Command Line Support Add-On/ || $_=~/Copyright \(c\) WinZip International LLC/);
                    chomp($resultLine);
                    print LOGFILE "     ...$resultLine\n" if ($resultLine);         
                }
                my $zrc = close ZIP_PROCESS or $rcode=7;
                print LOGFILE "     ERROR:  Closing zip delete process for $filename: $! $?\n" if ($rcode == 7);  # this is a necessary error check for open/close zip process
                if ($rcode == 3) { #if zip error
                    print LOGFILE "     ERROR: Warning(s) and/or Error(s) during zip/delete process for $filename\n";
                    $rcode = 6;
                    next;
                }
                undef $zipCommand;
            } elsif ($action eq "DELETE" && -f "$directory/$filename") {    # also verifies file and not directory > unlink will NOT work correctly for dirs
                my $dresult = 0;
                $dresult = unlink "$directory\\$filename";
                if ($dresult == 1) {  #if unlink(delete) was successful
                    print LOGFILE "     ...deleted $filename successfully from $directory.\n";
                } else {
                    print LOGFILE "     ERROR: During delete of $filename from $directory: $! $?\n";
                    $rcode = 4;
                }
                undef $dresult;
            } elsif ($action eq "LIST" ) {
                print LOGFILE "     ...file found: $filename\n";
            }
        }
    }

0 个答案:

没有答案
相关问题