in reply to Re^4: newbie question to archive::zip
in thread newbie question to archive::zip

ok .. i understand what you're going after now and the problem ..

first, change
@values = split(/\|/,$case); $driveltr = $values[0]; $dira = $values[1]; $dirb = $values[2]; $unique_id = $values[3]; $case_num = $values[4]; $case_part = $values[5];
to something like:
my ($driveltr, $dira, $dirb, $unique_id, $case_num, $case_part) = spli +t(/\|/,$case);

again, PLEASE use strict;

Now, to approach your problem .. One way would be, in the for loop, to populate a hash w/keys of the zip_file_name's, and values of a list (or hash) of files to put in there. Then, loop over that hash and write out the zips. Here's (untested) code that uses this approach, has the above cleanup, plus additional refactoring (note the simplications/reductions):
use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $file_path = "c:/temp/zip"; my @files = <c:/temp/zip/*.tif>; my $fileCount = scalar @files; print "We have $fileCount files to work with .. lets begin\n"; my %zips; my @items = map { chomp; s#(/|-|.tif)#|#g; lc } @files; # could just +do this in the foreach parens .. foreach my $case (@items) { my ($driveltr, $dira, $dirb, $unique_id, $case_num, $case_part) = +split(/\|/,$case); my $zip_file_name = "$file_path/$case_num.zip"; my $casePartStr = length($case_part) ? "-$case_part" : ""; my $file_name = "$unique_id-$case_num$casePartStr.tif"; my $file_to_zip = "$file_path/$unique_id-$case_num$casePartStr.tif +"; print "to the '$zip_file_name' zip file I will add '$file_name'\n" +; $zips{ $zip_file_name }->{ $file_to_zip } = $file_name; } while( my ($zip_file_name, $contents) = each %zips ){ my $zip = Archive::Zip->new(); while( my ($file_to_zip, $file_name) = each %$contents ){ zip->addFile( $file_to_zip, $file_name ); } die 'write error' unless $zip->writeToFileNamed( $zip_file_name ) == + AZ_OK; }

Replies are listed 'Best First'.
Re^6: newbie question to archive::zip
by jashv (Initiate) on Apr 14, 2006 at 20:01 UTC
    i'll have to give this a try, time to go home - THANK YOU, I will reply again soon
      This code is WAY neeter, I can't wait till I get this good. The output on the screen is correct but it will not create the zip files:
      We have 14 files to work with .. lets begin to the 'c:/temp/zip/030905temp.zip' zip file I will add '30589-030905t +emp-one.tif' to the 'c:/temp/zip/305cv26.zip' zip file I will add '30590-305cv26.ti +f' to the 'c:/temp/zip/306cv26.zip' zip file I will add '30591-306cv26.ti +f' to the 'c:/temp/zip/304cv743.zip' zip file I will add '30592-304cv743. +tif' to the 'c:/temp/zip/304cv264.zip' zip file I will add '30593-304cv264. +tif' to the 'c:/temp/zip/305cv87.zip' zip file I will add '30594-305cv87.ti +f' to the 'c:/temp/zip/305cv87.zip' zip file I will add '30595-305cv87-1o +f2.tif' to the 'c:/temp/zip/305cv87.zip' zip file I will add '30595-305cv87-2o +f2.tif' to the 'c:/temp/zip/305mj66.zip' zip file I will add '30596-305mj66-1o +f2.tif' to the 'c:/temp/zip/305mj66.zip' zip file I will add '30596-305mj66-2o +f2.tif' to the 'c:/temp/zip/305mj65.zip' zip file I will add '30597-305mj65.ti +f' to the 'c:/temp/zip/398cv560.zip' zip file I will add '30598-398cv560. +tif' to the 'c:/temp/zip/504cvmdl227.zip' zip file I will add '30599-504cvm +dl227-1of2.tif' to the 'c:/temp/zip/504cvmdl227.zip' zip file I will add '30599-504cvm +dl227-2of2.tif' Can't locate object method "addFile" via package "zip" (perhaps you fo +rgot to load "zip"?) at C:\work\2statfile_apha.pl line 31.
        Never mind, I had left out a $ (sometimes the resolutions are so easy) THANK YOU !!!!