advait has asked for the wisdom of the Perl Monks concerning the following question:

Hi All My cgi script generates some files as output. I want to zip them all together so the user can download. Any suggestion how to do so?

Replies are listed 'Best First'.
Re: how to zip files using perl
by johngg (Canon) on Oct 10, 2007 at 15:41 UTC
Re: how to zip files using perl
by dynamo (Chaplain) on Oct 10, 2007 at 17:54 UTC
    If you're on a mac (or unix machine with the 'zip' utility installed), the simplest way to do it is with backticks. I'll assume you just generated file1.html, file2.css, and file2.png. I'm also assuming you'd want to use the name files.zip.
    my @files2zip = qw/file1.html file2.css file3.png/; my $fileStr = join(" ",@files2zip); my $zipfile = "files.zip"; my $output = `zip $zipfile $filesStr`; print "Progress:\n$output\n\nYour files are in [$zipfile].";
    If you don't have the zip cmdline util handy, consider tar -z, it's not the same but it compresses decently. Failing that, or if you prefer, check out the previous post about the CPAN module.
Re: how to zip files using perl
by swares (Monk) on Oct 10, 2007 at 19:20 UTC
    Since your script is creating the files you could use IO::Zlib to create the archive and write directly the compressed files. The IO::Zlib docs have some examples to that effect. OOPS... this would work for gzip files not zip files...