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

Hi Monks!
I am creating a zip file after reading them from a directory. If all files exceed the limit size of 100MB it will be spliced into parts during the zipping process, like if reading all the files from this directory and if them all together add up to 500MB, the code will split them into 5 zip files about 100MB each. My problem is that I need to add a text file in each zip file listing all the names of files zipped in there, I am stuck on this, if anyone has worked with Archive::Zip and could point me in the right direction would be really helpful!
Here is a code sample of what I am trying to do: (zipping part works fine, I just can't add this report file into the zip files reporting on what is included into them)
... use strict; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); ... # create zip files my $limit = 100*MB; my $zip_file = "zipfile.zip"; my $file_name = "file_file"; my $f_count = 1; $f_count = sprintf("%02d", $f_count); my $count_files; my $count_zip=0; # all the files will be in this @files (lets say all files in @files a +dd up to 500MB) array after reading from a directory foreach my $files (@files) { chomp($files); $count_zip++; $count_zip = sprintf("%02d", $count_zip); my $fsize = -s $files; #add some logic for multiple zip files::: my $temp = Archive::Zip->new; my $member = $temp->addFile($files); next unless $member->compressedSize; my $fh = tempfile(); $temp->writeToFileHandle($fh) == AZ_OK or die $!; if ($total + $member->compressedSize > $limit) { if($total > 0){ $zip_file =~ s/\.[^.]+$//; $zip_file .= "_$f_count.zip"; $zip->writeToFileNamed("temp/".$zip_file) == AZ_OK or die $!; $f_count++; $count_zip--; $total = 0; $zip = Archive::Zip->new; } } $total += $member->compressedSize; #add files to the zip files $zip->addFile($files,$file_name); # now lists all members in the archive my @rfiles = $zip->memberNames(); my $c=0; # read all the files inside of each zip file listing their names to + crerate a report open REPL, ">report_list.txt" or die $!; foreach my $rfiles (@rfiles) { $c++; print REPL "$c - $rfiles\n"; } close (REPL); $zip->addFile('report_list.txt') unless $zip->memberNamed('report_l +ist.txt'); }
Thanks for the Help!!!

Replies are listed 'Best First'.
Re: Adding files to ZIP files!
by Lotus1 (Vicar) on Jan 13, 2012 at 19:03 UTC
Re: Adding files to ZIP files!
by InfiniteSilence (Curate) on Jan 13, 2012 at 19:07 UTC

    I think this may be homework and the reason it comes up so often is that it is reassigned to a new class every year/semester.

    There are all kinds of things missing from the code like a file handle is being returned from tempfile() but I don't see it getting closed anywhere...be honest, is this homework?

    Celebrate Intellectual Diversity

      Why instead of writing stupid answers you people don't point someone in to the right direction:
      You can start here;
      #!/usr/bin/perl -w use strict; use warnings; use Archive::Zip qw( :ERROR_CODES ); my $zipfile = "your_zip.zip"; my $zip = Archive::Zip->new(); unless ( $zip->read( "$zipfile" ) == AZ_OK ) { die 'read error'; } my @files = $zip->memberNames(); print $_, "\n" for @files;

        Because that stuff is in the perldoc. RTFM.

        Celebrate Intellectual Diversity