0

I am currently trying to zip some files with perl. The resulting file is printed, so a user who calls the page which executes the script can download or open the zip file.

Looking at the size of the zip file it seems everything worked ok, but if I try to open the file on the server no contents are shown. If I open the file after downloading it, the archive is invalid.

Here's the code:

my $zip = Archive::Zip->new();  

my $i;
foreach $i(@files)
{
    my $fh = $zip->addFile("$directoryPath$i") if (-e "$directoryPath$i");
}

my $zipFilePath = "Test.zip";

die 'Cannot create $zip_file_name: $!\n' if $zip->writeToFileNamed("$zipFilePath") != AZ_OK;

open (DLFILE, "<$zipFilePath");
@fileholder = <DLFILE>;
close (DLFILE);

print "Content-Type:application/x-download\n";   
print "Content-Disposition:attachment;filename=$zipFilePath\n\n";  
print @fileholder;

Can you please tell me where the error is?

I am running the code using xampp on my local windows machine.

Edit: The same happens when I use

use strict;
use warnings;
use autodie;

Edit: The first problem is solved by ysth, thanks for that. Now the archive is not invalid after downloading, but still no files are shown if I open it, while the zip-file's size seems to be correct.

derBasti
  • 325
  • 3
  • 9
  • What's `$directoryPath` - my guess would be that it's somehow not valid, and so you're not adding any files to the zip in the first place. – Sobrique Jan 12 '15 at 11:04
  • $directoryPath is the folder of the files. It's an valid path as downloading the file directly with $directoryPath works. – derBasti Jan 12 '15 at 11:10
  • OK. It's just I've been caught out in the past with relative paths/permissions. My guess would have been that `$directoryPath` doesn't contain a trailing slash. – Sobrique Jan 12 '15 at 11:16

1 Answers1

2

You are corrupting it here:

open (DLFILE, "<$zipFilePath");
@fileholder = <DLFILE>;
close (DLFILE);

by opening it such that it translates "\r\n" to just "\n".

Try this:

open( DLFILE, '<:raw', $zipFilePath );

ysth
  • 96,171
  • 6
  • 121
  • 214
  • Thanks for your reply. It solves half the problem: The archive is no longer invalid, but still no files are shown when I open it, while the size seems to be right (>100kb). – derBasti Jan 12 '15 at 11:15