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