Skip to content

Instantly share code, notes, and snippets.

@stevelippert
Created February 7, 2014 15:45
Show Gist options
  • Select an option

  • Save stevelippert/8865181 to your computer and use it in GitHub Desktop.

Select an option

Save stevelippert/8865181 to your computer and use it in GitHub Desktop.
Using Archive::Zip to create a ZIP file from an array of files and output to download via browser, removing ZIP file afterwards.
#!/usr/local/bin/perl
use Archive::Zip;
my $zipFile = "/temp/$user_$title.zip"
my $zipFileName = "$user_title";
my $zip = Archive::Zip->new();
foreach my $file (@files){
my $storedDir = '/some/path/with/files/';
chdir $storedDir;
$zip->addFile($file)->desiredCompressionMethod(0);
}
$zip->writeToFileNamed($zipFile);
if (-e $zipFile){
my $fileSize = -s $zipFile;
print "Content-type:application/zip\n";
print "Content-disposition:attachment; filename=\"$zipFileName.zip\"\n";
print "Content-Length:$fileSize\n\n";
open $fh,'<',$zipFile;
while (<$fh>){
print $_;
}
close $fh;
unlink($zipFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment