Hi there Monks!
I am looking all over for something like this code, but I still can't figure this one out, I need to zip files inside directories but the zip file can not be larger than 5MB each, but all the files inside of these directories will be more than that, I need to find a way to create multiple .zips files in case I read directories and the sum of files is greater than 5MB zipped. This code is almost there but I am stuck on the logic of making multiple zips if necessary.
Here is the code and thanks for the help!
#!/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;
my $limit = 5*MB;
my $FileCount = 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 $!;
$zip->addMember($member);
$total += $member->compressedSize;
#die "$total bytes exceeds archive size limit" if $total > $limit;
my $new_total;
if($total > $limit) {
$FileCount++;
$total = $total/2;
print "\n$filecount - $total - $file\n\n";
}
#die "$total bytes exceeds archive size limit" if $total > $limit;
}
print "Total archive size: $total bytes\n\n";
$zip->writeToFileNamed('zipped.zip') == AZ_OK or die $!;
Thanks!
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.