使用 perl 压缩文件会导致存档无效

Zipping a file with perl results in an invalid archive

我目前正在尝试使用 perl 压缩一些文件。打印生成的文件,因此调用执行脚本的页面的用户可以下载或打开 zip 文件。

查看 zip 文件的大小似乎一切正常,但如果我尝试在服务器上打开文件,则不会显示任何内容。如果我下载后打开文件,存档是无效的。

代码如下:

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;

你能告诉我错误在哪里吗?

我是 运行 在本地 windows 机器上使用 xampp 的代码。

编辑:当我使用

时也会发生同样的情况
use strict;
use warnings;
use autodie;

编辑:第一个问题已由 ysth 解决,谢谢。现在下载后存档没有失效,但是打开还是没有文件显示,而zip文件的大小似乎是正确的。

你在这里破坏它:

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

打开它,将“\r\n”翻译成“\n”。

试试这个:

打开(DLFILE, '<:raw', $zipFilePath );