Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

newbie question to archive::zip

by jashv (Initiate)
on Apr 14, 2006 at 16:23 UTC ( [id://543387]=perlquestion: print w/replies, xml ) Need Help??

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

Greeetings. I have a windows folder full of .tif files, with names like: 123-monday.tif 234-monday-1of2.tif 345-monday-2of2.tif I have pooly crafted a script to read all the filenames in the directory, pull out the day and create a zip called monday.zip My problem is it willput these 3 files in, but each time it moves to the next file it removes the previous file. So as an example once the script is run the only file I have in the zip is 345-monday-2of2.tif. I tested by putting in a sleep command, stopping the script and looking in the zip, at the point I stopped it I could see it was putting the previous file in, but once it goes forward it kills the contents. I am not declaring the zip name, I read the filename, extract the day and name it monday.zip.
my $zip = Archive::Zip->new(); my $member = $zip->addDirectory( 'dirname/' ); $member->desiredCompressionMethod(); $member = $zip->addFile( "$file_to_zip", "$file_name" ); + die 'write error' unless $zip->writeToFileNamed( $zip_file_name ) +== AZ_OK;
is there a way to make it not clear the zip file? I want this so each day it can scan a directory and add files to the already existing zip. Did I explain well enough?

Replies are listed 'Best First'.
Re: newbie question to archive::zip
by RollyGuy (Chaplain) on Apr 14, 2006 at 16:33 UTC
    You seem to be following the example in the Archive::Zip documentation pretty well. I question your usage of the second parameter to addFile: $file_name. This is an optional parameter to specify a new name to be used within the zip archive.

    I imagine that you have the second parameter ($file_name) set to the zip file name, i.e. monday.zip. Try your code without this parameter.

    Enjoy.
    -Rg
Re: newbie question to archive::zip
by davidrw (Prior) on Apr 14, 2006 at 16:42 UTC
    Can you post your loop structure as well? From you indenting, I assume that my $zip = Archive::Zip->new(); is before the loop but $zip->writeToFileNamed() is in it? I didn't see anything in the Archive::Zip docs to support this idea, but maybe just move the $zip->writeToFileNamed() to after the for loop (seems a waste of write's to do it each time anyways)?
      once again, probably not the neetest code but
      foreach $case (@items) { chomp($case); @values = split(/\|/,$case); $driveltr = $values[0]; $dira = $values[1]; $dirb = $values[2]; $unique_id = $values[3]; $case_num = $values[4]; $case_part = $values[5]; $tester = length($case_part); $zip_file_name = "$file_path/$case_num.zip"; if ($tester > 0) { $file_name = "$unique_id-$case_num-$case_part.tif"; $file_to_zip = "$file_path/$unique_id-$case_num-$case_part.tif +"; #$zip_file_name = "$file_path/$case_num-$case_part.zip"; } else { $file_name = "$unique_id-$case_num.tif"; $file_to_zip = "$file_path/$unique_id-$case_num.tif"; } print "to that zip file I will add $file_name\n"; my $zip = Archive::Zip->new(); my $member = $zip->addDirectory( 'dirname/' ); #$member = $zip->addString( 'This is a test', 'stringMember.txt' ) +; #$member->desiredCompressionMethod( COMPRESSION_DEFLATED ); $member->desiredCompressionMethod(); $member = $zip->addFile( "$file_to_zip", "$file_name" ); + die 'write error' unless $zip->writeToFileNamed( $zip_file_name ) +== AZ_OK; #sleep(2); }
        it's because my $zip = Archive::Zip->new(); AND writeToFileNamed are both in the loop .. so you're not appending to the same zip object -- you're creating, adding one file, writing out to disk, and then $zip is destroyed before the next iteration. Move my $zip = Archive::Zip->new(); to before the forloop, and the write to after.
        my $zip = Archive::Zip->new(); foreach $case (@items) { ... my $member = $zip->addDirectory( 'dirname/' ); $member->desiredCompressionMethod(); $member = $zip->addFile( "$file_to_zip", "$file_name" ); + } die 'write error' unless $zip->writeToFileNamed( $zip_file_name ) == A +Z_OK;
        btw, are you doing use strict; and use warnings; somewhere in your code?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://543387]
Approved by RollyGuy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-20 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found