将 Archive::Zip zip 文件打印到 Apache2::RequestIO 对象

发布于 2024-11-05 07:32:04 字数 313 浏览 1 评论 0原文

我有一个使用 mod_perl 的网站。

我正在内存中创建一个 zip 文件(使用 Archive::Zip),并且我希望无需将其写入磁盘即可提供该文件。

Archive::Zip 只会输出到指定的文件句柄,我不认为 Apache2::RequestIO 为我提供了一个。

目前,我只需将 Zip 文件打印到 *STDOUT 即可。但我确信有更好的方法来做到这一点。我通过 RequestRec 对象打印其他所有内容,例如 $r->print(...)

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 think 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(...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉怎暖 2024-11-12 07:32:04

像这样的事情应该有帮助......

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

编辑:
以上已作废。
来自 Archive::Zip 文档:

尽量避免 IO::Scalar

使用 Archive::Zip 最常见的方法之一是生成 Zip
内存中的文件。大多数人都使用 IO::Scalar 来达到此目的。

不幸的是,从 1.11 开始,该模块不再适用于 IO::Scalar
因为它错误地实现了搜索。

任何使用 IO::Scalar 的人都应该考虑移植到 IO::String,这
更小、更轻,并且实现完美兼容
具有常规的可查找文件句柄。

对 IO::Scalar 的支持将来很可能不会恢复,
因为 IO::Scalar 本身无法改变它的实现方式,因为
向后兼容性问题。

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.

划一舟意中人 2024-11-12 07:32:04

在 Perl 5.8+ 版本中,似乎您可以一起跳过 IO::Scalar 和 IO::String 。

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);

我认为可能有一种更优化的方法来做到这一点,但它有效......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文