in reply to Zipping an array problem

Not quite sure what you want to achieve but I'm guessing that you want to add text files to a zip archive without preserving the original directory structure.

Zip::Archive needs to know the path as well as the file name, how else can it find it? You don't need to keep a separate array of the short names, you calculate it when required.

use Archive::Zip; use File::Find; use File::Basename; my $sourceDir = 'C:/sourceDirectory/One'; my $destDir = 'C:/destinationDirectory/Two/'; my @files; find( \&files_to_archive, $sourceDir ); my $zip = Archive::Zip->new(); foreach my $file (@files) { $zip->addFile( $file, basename $file ) } if ($zip->writeToFileNamed("$destDir/textFiles.zip") != AZ_OK) { print "Error creating archive!\n"; } else { print "Archive successfully created!\n"; } sub files_to_archive { push @files, $File::Find::name if (/\.txt$/); }

Replies are listed 'Best First'.
Re^2: Zipping an array problem
by mountaingoat (Initiate) on Feb 17, 2008 at 03:31 UTC
    hipowls,

    You rock. That did it. Thanks so much for your help. You've saved me a lot of hours of trying to get that to work. Thanks again.

Re^2: Zipping an array problem
by mountaingoat (Initiate) on Feb 17, 2008 at 03:35 UTC
    hipowls,

    Yes, I did want to add text files to a zip archive without preserving the original directory structure. Thanks again. You fixed it with less that half the lines of code, and in about 1/100th the time it took me to write that horrible looking stuff I was calling code.

Re^2: Zipping an array problem
by mountaingoat (Initiate) on Feb 17, 2008 at 04:03 UTC
    One more question: with using the foreach loop, how do I create a loop so that I only zip 3000 files at a time, and never zip the same files more than once? I know that I need to use a loop, and add a number to the end of the zipped file (ex. - textFiles1.zip, textFiles2.zip, etc.) to differentiate them so the zipped files are not overwritten in the destination directory (it will ultimately be a MySQL db) each time.

    I've been working today for 14 hours, and I'm getting more confused the more I think about it.

    I'll next be working on unzipping the textFiles.zip and dropping them in the db.

      Get some rest;) You'll work better for it.

      You need to chop @files into chunks of 3,000, or less for the end. I'll not duplicate all the code, this is just an outline.

      use List::Util qw(min); foreach my $seq ( 0 .. $#files/3_000 ) { my $zip = Archive::Zip->new(); my $start = $seq * 3_000; my $end = min ( $#files, $start + 2_999 ); foreach my $i ( $start .. $end ) { $zip->addFile( $files[$i], basename $files[$i] ); } $zip->writeToFileNamed("test-$seq.zip"); }