gmaniac has asked for the wisdom of the Perl Monks concerning the following question:

I have several folders and files in it. What I am trying to do is to zip only several (unspecific) of them, delete the files after they got zipped and execute the script again - as long as I have all folders and files in the zip file. This is to avoid script timeouts, when I have a lot of files to zip (I don't want to use fork proc::deamon btw). But each time I execute the script, ALL of the folders and files are written to the zip-file. I don't know why, because @file only holds the name of the files from 1 to 50. Here's the code:
use Archive::Zip; my @errors; Archive::Zip::setErrorHandler(sub { push @errors, $_[0] }); my $zippath = "../backups/$username"; my $dest_path = '../backups'; my $zip = Archive::Zip->new(); $status =$zip->writeToFileNamed("$dest_path/$username.zip"); @files = $zip->addTree( "$zippath" ); @files = $zip->memberNames(); @file = splice @files, 1, 50; foreach $line (@file) { $fileend = (split(/\./,"$line"))[-1]; if (($fileend eq "jpg") || ($fileend eq "txt")) { $zip->addFile("$line"); #$status = $zip->writeToFileNamed("$dest_path/$username.zip"); } } # Save the Zip file unless ( $zip->writeToFileNamed("../backups/$username.zip") == AZ_OK +) { die 'write error'; }
Thanks in advance!

Replies are listed 'Best First'.
Re: Archive::ZIP - Zip selection of files
by Corion (Patriarch) on Apr 10, 2015 at 09:09 UTC
    @files = $zip->addTree( "$zippath" );
    $zip->addTree( $root, $dest [, $pred, $compressionLevel ] ) -- Add tre +e of files to a zip

    Maybe you want to use the select predicate as documented in Archive::Zip? Or just leave that line out?

    Also, personally, I would run the archival in the background and then send the user an email on completion instead of repeatedly trying to run a script, which can fail half-way.

      Thank you for your answer. By using your solution I would not be able to get the names of the files, I guess. But I need them (including the path) to delete them afterwards. I'm not happy with executing a script on and on also, but I'm using it for just ten of my users. They have stored a huge amount of pictures. That is why I'm constantly running in a script timeout.

        Why would you use Archive::Zip to get the names of the files not yet in the archive?

        Use File::Find to get the names of the files not in the archive.