Created
February 7, 2014 15:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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