File Size Limit in Archive::Perl

2007-11-10 Thread San
Hi All,

Is there any way to limit the file size while zipping using
Archive::Zip so that it will stop processing a zip operation on a file
list when it crosses the maximum file size.

Thanks in advance.

-A


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File Size Limit in Archive::Perl

2007-11-10 Thread Rob Dixon

San wrote:


Is there any way to limit the file size while zipping using
Archive::Zip so that it will stop processing a zip operation on a file
list when it crosses the maximum file size.


Hey San

Unfortunately Archive::Zip requires that an archive be written to disk
before the compression is performed and the final size can be determined.
But it is possible to compress files individually and write them to a
temporary file to determine their size, and then add the same archive
member to the final archive without repeating the compression.

Take a look at the program below, which is written for a Windows system
but should be easily portable.

HTH,

Rob


use strict;
use warnings;

use Archive::Zip qw/AZ_OK/;
use File::Temp qw/tempfile/;

use constant MB = 1024 * 1024;

my $dir = 'C:';
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 = 50*MB;

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;
}

print Total archive size: $total bytes\n\n;

$zip-writeToFileNamed('archive.zip') == AZ_OK or die $!;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/