Number41
Number41

Reputation: 1

Adding files to zipped folder in perl

I have the following perl script that is intended to accept command line arguments that will archive all of a users data files into a zip file and then delete the original data. The script does alright, but when run again with a different user as the argument, it overwrites the previous data in the userData.zip file. I have searched and not been able to find how to perform this task. It should continue to accept users as an argument and append their folders to the userData.zip file.

Any help is appreciated.

use 5.14.2;
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Path;
my ($DATAFILEIN, $DATAFILEOUT);
my ($new,$zip);
use constant COLUMNS => 6;

sub main {
    verifyArguments();
    setDataFileIn();
    zipFiles();
    deleteUserFiles();
    #setDataFileOut();
    #printData();
    #writeData();  
}


sub verifyArguments {
if (!(@ARGV) || !(-e $ARGV[0])) {
    die "\n\nYou must specify correct file name upon command invocation.\n\n";
}
}

sub setDataFileIn {
    $DATAFILEIN = $ARGV[0];
}

sub zipFiles {
    print "\nBacking up ".$DATAFILEIN."\n";
    sleep 1;

    $zip = Archive::Zip->new();
    opendir (DIR, $DATAFILEIN) or die $!;

     while (my $file = readdir(DIR)) {

    # Use -f to look for a file
    next unless (-f $DATAFILEIN."\\".$file);

 $zip->addFile($DATAFILEIN."\\".$file, );
print "Added $file to zip\n";
   }
closedir(DIR);
my $fileName = $DATAFILEIN;
unless ( $zip->writeToFileNamed('userData.zip') == AZ_OK ) {
    die 'write error';
   }
print "Successfully backed up $fileName to userData.zip\n";
}

sub deleteUserFiles{
    rmtree($DATAFILEIN);

}


main();

Upvotes: 0

Views: 968

Answers (1)

BowlOfRed
BowlOfRed

Reputation: 349

Have you read this portion of the Archive::Zip FAQ?

Can't Read/modify/write same Zip file

Q: Why can't I open a Zip file, add a member, and write it back? I get an error message when I try.

A: Because Archive::Zip doesn't (and can't, generally) read file contents into memory, the original Zip file is required to stay around until the writing of the new file is completed.

The best way to do this is to write the Zip to a temporary file and then rename the temporary file to have the old name (possibly after deleting the old one).

Archive::Zip v1.02 added the archive methods overwrite() and overwriteAs() to do this simply and carefully.

See examples/updateZip.pl for an example of this technique.

I don't see $zip->overwrite() in your code.

The best place to find information on CPAN modules is http://metacpan.org. In this case, the Archive::Zip page. That page has a documentation link to Archive::Zip::FAQ. You can read it there, or you can probably just type perldoc Archive::Zip::FAQ on your system where you have the module installed.

The examples are part of the downloaded package. If you used the cpan command to install Archive::Zip, then the examples would be in the build location. By default, that would be ~/.cpan/build/Archive-Zip-*/examples.

Upvotes: 1

Related Questions