开发者

Print Archive::Zip zip file to Apache2::RequestIO object

I have a website using mod_perl.

I'm creating a zip file in memory (using Archive::Zip), and I want to serve that file without having to write it to disk.

Archive::Zip will only output to a specified file handle, and I don't 开发者_如何学Cthink Apache2::RequestIO provides me with one.

At the moment I just print the Zip file to *STDOUT and that works. But I'm sure there's a better way to do it. I'm printing everything else through the RequestRec object, e.g. $r->print(...)


Something like this should help...

use Archive::Zip;
my $zip = Archive::Zip->new();
#create your zip here

use IO::Scalar;
my $memory_file = '';   #scalar as a file
my $memfile_fh = IO::Scalar->new(\$memory_file); #filehandle to the scalar

# write to the scalar $memory_file
my $status = $zip->writeToFileHandle($memfile_fh);
$memfile_fh->close;

#print with apache
#$r->content_type(".......");
$r->print($memory_file);    #the content of a file-in-a-scalar

EDIT: The above is obsoloted. from the Archive::Zip docs:

Try to avoid IO::Scalar

One of the most common ways to use Archive::Zip is to generate Zip files in-memory. Most people have use IO::Scalar for this purpose.

Unfortunately, as of 1.11 this module no longer works with IO::Scalar as it incorrectly implements seeking.

Anybody using IO::Scalar should consider porting to IO::String, which is smaller, lighter, and is implemented to be perfectly compatible with regular seekable filehandles.

Support for IO::Scalar most likely will not be restored in the future, as IO::Scalar itself cannot change the way it is implemented due to back-compatibility issues.


In versions of Perl 5.8+, it seems like you can skip IO::Scalar and IO::String all together.

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();

my $memory_file = '';   #scalar as a file
open(my $fh, '>', \$memory_file) || die "Couldn't open memory file: $!";

my $status = $zip->writeToFileHandle($fh);
$fh->close;

$r->print($memory_file);

I think there is probably a more optimal way of doing this, but it works...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜