Home: Perl Programming Help: Advanced:
how to compress the zip file.



kclai
New User

Dec 4, 2011, 8:29 PM


Views: 4762
how to compress the zip file.

Hi,

I have a perl script that gets the content from a repository and then try to zip the content before sending as an email attachment. But it is not working need some expert advise from this forum.

Below is the extract of the perl script:

my $attachmentName = $_[0];
my $fileContents = $_[1];

my $zip = Archive::Zip->new() || die $logPrefix . "Create zip: $!\n";
my $zipString = $zip->addString($fileContents,$attachmentName);
my $status = $zip->writeToFileNamed("$attachmentName.zip");

The content of the file is actually an excel file which is 3MB in size. So after zipping it, it should be less then 3MB but in this case the content of the file size is the same after zipping it.

I did some research regarding addString and addFile, seems to me it requires the addFile in order for zipping to work. But in my scenario here, we need to extract the content from the repository thus perhaps the previous programmer uses the addString.

The question here is how to extract the content from the repository and at the same time zip up the file.

Would appreciate some advise on this issue.


rovf
Veteran

Jan 9, 2012, 1:27 AM


Views: 4618
Re: [kclai] how to compress the zip file.

Did you read the documentation for Archive::Zip? You have to explicitly compress the string:


SYNOPSIS
# Create a Zip file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();

# Add a directory
my $dir_member = $zip->addDirectory( 'dirname/' );

# Add a file from a string with compression
my $string_member = $zip->addString( 'This is a test', 'stringMember.txt' );
$string_member->desiredCompressionMethod( COMPRESSION_DEFLATED );