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

Hi all, I've got a backup script that I'm using to zip the files after getting all the data copied to a backup directory. -With the code below, the zip file contains the full path of the uncompressed files, any way to not store directory names/structures for the zipped files? -How can I test the zip files, I can't seem to find a equiv' to the -t switch of regular unzip. Tnx!
my $bfile = "c:\\backups\\datafile_01.dbf"; my $fsize = floor((-s $bfile) / 1024); my $bfiledst = $bfile . ".zip"; my $zip = Archive::Zip->new(); my $member = $zip->addFile($bfile, $bfile); $member->desiredCompressionMethod( COMPRESSION_DEFLATED ); $member->desiredCompressionLevel( 9 ); push @summary, "Error compressing $bfile\n" if ($zip->writeToFileName +d($bfiledst) != AZ_OK); my $cfsize = floor((-s $bfiledst) / 1024); print "Compressed $bfile ($fsize K) to $bfiledst ($cfsize K)\n";

Replies are listed 'Best First'.
Re: Archive Zip, testing & more
by Thelonius (Priest) on Nov 19, 2002 at 21:52 UTC
    I think this will work for your first question:
    my $shortname = $bfile; $shortname =~ s!.*[\\/]!!; my $member = $zip->addFile($bfile, $shortname);
    As to your second question, I think (untested) you can read the file and just ignore the contents, but check to make sure you don't get errors:
    my $ziptest = Archive::Zip->new($bfiledst); if (!defined($ziptest)) { # big error; } my @members = $ziptest->members; #check to make sure it has what you expect #then check individual files: for (@members) { if (! defined($ziptest->contents($_)) # another error. } }
    Of course you should test that with some corrupted zip files to make sure it works.
Re: Archive Zip, testing & more
by John M. Dlugosz (Monsignor) on Nov 19, 2002 at 22:21 UTC
    The best verification is to extract the file again and compare it with the original, bit-for-bit.
Re: Archive Zip, testing & more
by Mr. Muskrat (Canon) on Nov 20, 2002 at 15:12 UTC

    If you are checking errors every time you addFile() and again when you writeToFileNamed, the zip file created should be good.

    There is one more thing that you can do to test it (besides extracting the files), and that is to read the zip file.
    die "Invalid zip file!" if $zip->read($bfiledst) != AZ_OK;