The first thing that occurs to me is to add files until you are about to pass 5 MB, and then start a new archive. Perhaps something like (untested):
#!/usr/bin/perl -w
use strict;
use Archive::Zip qw/AZ_OK/;
use File::Temp qw/tempfile/;
use constant MB => 1024 * 1024;
my $dir = qw( zip_this/);
my @files = do {
opendir my $fd, "$dir" or die $! or die $!;
grep -f, map "$dir$_", readdir $fd;
};
my $zip = Archive::Zip->new;
my $total = 0;
my $limit = 5*MB;
my $FileCount = 0;
my $archiveCount = 0;
foreach my $file (@files) {
my $temp = Archive::Zip->new;
my $member = $temp->addFile($file);
next unless $member->compressedSize;
my $fh = tempfile();
$temp->writeToFileHandle($fh) == AZ_OK or die $!;
if ($total + $member->compressedSize > $limit) {
$zip->writeToFileNamed("zipped_$archiveCount.zip") == AZ_OK or
+ die $!;
$archiveCount++;
print "Total archive size: $total bytes\n\n";
$total = 0;
$zip = Archive::Zip->new;
}
$zip->addMember($member);
$total += $member->compressedSize;
}
print "Total archive size: $total bytes\n";
$zip->writeToFileNamed("zipped_$archiveCount.zip") == AZ_OK or die $!;
Update: Fixed copy/paste scoping bug; see Re^3: Multiple zip files from directories! for information. Also initialized $total as per Anonymous Monk's suggestion below.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|