2015-07-22 137 views
0

我有一個腳本,可以打開一個大文本文件並提取數據來創建報告。Perl:解析壓縮文件

問:

有沒有辦法打開在Perl文件,即使該文件已被壓縮與gzip

Gunzip打開之前的文件將會變慢。

例子:

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

回答

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規定,在由gzip程序所使用的格式操縱文件PerlIO的層。壓縮和解壓縮被實現,但不在一起。如果您嘗試打開文件進行讀取和寫入打開將失敗。