我想从大型ish(3+GB,gzipped)FTP下载中提取一些数据,并实时执行,以避免在磁盘上转储然后完全下载。
要提取所需的数据,我需要逐行检查未压缩的流。
所以我正在寻找与在道德上相当的东西
use PerlIO::gzip;
my $handle = open '<:gzip', 'ftp://ftp.foobar.com/path/to/blotto.txt.gz'
or die $!;
for my $line (<$handle>) {
# etc.
}
close($handle);
FWIW:我知道如何打开ftp://ftp.foobar.com/path/to/blotto.txt.gz
的读句柄(使用Net::FTP::repr
),但我还没有弄清楚如何向这个打开的句柄添加:gzip
层。
我花了很长时间才找到上面问题的答案,所以我想我应该把它发布给下一个需要它的人。
好的,答案是(IMO)一点也不明显:binmode($handle, ':gzip')
。
下面是一个充实的例子:
use strict;
use Net::FTP;
use PerlIO::gzip;
my $ftp = Net::FTP->new('ftp.foobar.com') or die $@;
$ftp->login or die $ftp->message; # anonymous FTP
my $handle = $ftp->retr('/path/to/blotto.txt.gz') or die $ftp->message;
binmode($handle, ':gzip');
for my $line (<$handle>) {
# etc.
}
close($handle);
下面的代码来自IO::压缩常见问题
use Net::FTP;
use IO::Uncompress::Gunzip qw(:all);
my $ftp = new Net::FTP ...
my $retr_fh = $ftp->retr($compressed_filename);
gunzip $retr_fh => $outFilename, AutoClose => 1
or die "Cannot uncompress '$compressed_file': $GunzipErrorn";
要逐行获取数据,请将其更改为此
use Net::FTP;
use IO::Uncompress::Gunzip qw(:all);
my $ftp = new Net::FTP ...
my $retr_fh = $ftp->retr($compressed_filename);
my $gunzip = new IO::Uncompress::Gunzip $retr_fh, AutoClose => 1
or die "Cannot uncompress '$compressed_file': $GunzipErrorn";
while(<$gunzip>)
{
...
}