我正在尝试即时构建一个压缩的 xml 文件并将其发送到浏览器。我正在使用IO::Compress::Zip
with IO::String
,但它不像文档中所期望的那样工作。
首先我的代码:
my $zipped_file;
foreach (@{$par{directory}}) {
my $input = IO::String->new($_->[0]);
zip $input => \$zipped_file, Append => 1, BinModeIn => 1|0; # Tried both for BinModeIn
}
return $zipped_file;
$par{directory}
是一个具有以下结构的arrayref:
[
['DATA', 'FILENAME'], ['DATA', 'FILENAME'], ...
]
中的数据$par{directory}
是这样的字符串:
<?xml version="1.0" encoding="UTF-8"?>
<derivatives>
<derivative ag="111520420" vhash="d6712f201155a9afb64dc5a5ba369c99">
//Some data
</derivative>
</derivatives>
当我尝试将文件下载为带有一个空文件的 .zip 存档时,我得到的只是。关于我在这里做错了什么的任何提示?
提前致谢。
编辑:这很奇怪。我现在已经尝试过了:
use strict;
use warnings;
use IO::Compress::Zip 'zip';
my $xml1 = <<END_XML;
<?xml version="1.0" encoding="UTF-8"?>
<derivatives>
<derivative ag="111520420" vhash="d6712f201155a9afb64dc5a5ba369c99">
//Some data
</derivative>
</derivatives>
END_XML
my $xml2 = <<END_XML;
<?xml version="1.0" encoding="UTF-8"?>
<derivatives2>
<derivative2 ag="111520420" vhash="d6712f201155a9afb64dc5a5ba369c99">
//Some data
</derivative2>
</derivatives2>
END_XML
my $files = [[$xml1, 'xml1'], [$xml2, 'xml2']];
my $zipped_file;
foreach (@$files) {
open my $input, '<', \$_->[0];
zip $input => \$zipped_file, Append => 1, BinModeIn => 1;
}
open my $fh, '>:raw', 'zip.zip';
print $fh $zipped_file;
close $fh;
存档仍然是空的。