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!!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.