CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Dec 4, 2011, 8:29 PM

Post #1 of 2 (4842 views)
how to compress the zip file. Can't Post


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("$");

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.


Jan 9, 2012, 1:27 AM

Post #2 of 2 (4698 views)
Re: [kclai] how to compress the zip file. [In reply to] Can't Post

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

# 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 );


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives