Perl:解析压缩文件

时间:2015-07-22 04:35:06

标签: perl compression gzip

我有一个脚本可以打开一个大文本文件并提取数据来创建报告。

问题:

即使文件已使用gzip进行压缩,有没有办法在perl中打开文件?

Gunzip打开前的文件会变慢。

示例:

open(my $fh, "<", "input.txt.gz")

1 个答案:

答案 0 :(得分:2)

IO::Uncompress::Unzip模块可以帮助您。

use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
unzip $input_filename_or_reference => $output_filename_or_reference [,OPTS] 
    or die "unzip failed: $UnzipError\n";

功能界面需要Perl5.005或更高版本。

或者您可以使用CPAN模块PerlIO::gzip

use PerlIO::gzip;
open FOO, "<:gzip", "file.gz" or die $!;
print while <FOO>; # And it will be uncompressed...

binmode FOO, ":gzip(none)" # Starts reading deflate stream from here on

PerlIO::gzip提供了一个PerlIO层,以gzip程序使用的格式处理文件。压缩和解压缩是实现的,但不是一起实现的。如果您尝试打开文件进行读取和写入,则打开将失败。