1

我正在尝试将远程文件添加到本地 zip 存档中。目前,我正在做这样的事情。

use Modern::Perl;
use Archive::Zip;
use File::Remote;

my $remote = File::Remote->new(rsh => "/usr/bin/ssh", rcp => "/usr/bin/scp");
my $zip = Archive::Zip->new();

$remote->open(*FH,'host2:/file/to/add.txt');
my $fh = IO::File->new_from_fd(*FH,'r');

#this is what I want to do.
$zip->addFileHandle($fh,'add.txt');

...

不幸的是,Archive::Zip 没有 addFileHandle 方法。

还有其他方法可以做到吗?

谢谢。

4

2 回答 2

2

做这样的事情(复制到本地路径):

$remote->copy("host:/remote/file", "/local/file");

并将 Archive::Zip 提供的 addFile 方法与本地文件一起使用

于 2013-01-30T21:55:43.693 回答
1

Archive::Zip 可能不支持写入 zip 文件的文件句柄,但 Archive::Zip::SimpleZip 支持。

这是一个独立的示例,展示了如何从文件句柄读取并直接写入 zip 文件,而不需要任何临时文件。

use warnings;
use strict;

use Archive::Zip::SimpleZip;
use File::Remote;

# create a file to add to the zip archive
system "echo hello world >/tmp/hello" ;

my $remote = File::Remote->new(rsh => "/usr/bin/ssh", rcp => "/usr/bin/scp");
my $zip = Archive::Zip::SimpleZip->new("/tmp/r.zip");

$remote->open(*FH,'/tmp/hello');

# Create a filehandle to write to the zip fiule.
my $member = $zip->openMember(Name => 'add.txt');

my $buffer;
while (read(FH, $buffer, 1024*16))
{
    print $member $buffer ;
}

$member->close();
$zip->close();

# dump the contents of the zipo file to stdout
system "unzip -p /tmp/r.zip" ;    
于 2013-01-31T23:06:36.393 回答