我目前正在尝试用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文件的大小似乎是正确的。
答案 0 :(得分:2)
你在这里腐败了:
open (DLFILE, "<$zipFilePath");
@fileholder = <DLFILE>;
close (DLFILE);
通过打开它来翻译&#34; \ r \ n&#34;只是&#34; \ n&#34;。
试试这个:
open(DLFILE,&#39;&lt;:raw&#39;,$ zipFilePath);