我想让我的Perl脚本从一个文件中加载二进制数据。然而,如果文件头_ISCOMPRESSED_
出现在文件的开头,这个文件可以直接加载或者需要解压缩(zlib)。
我已经能够成功加载未压缩的文件并识别标题:
(open my ($fh), "<", $fileName) or (return 0);
binmode $fh;
my $fileHeader;
sysread $fh, $fileHeader, 14;
if( $fileHeader eq "_ISCOMPRESSED_" ){
# Here, need to decompress the filestream and update the $fh to point toward uncompressed data
}
else{
# Read it from the begining
sysseek $fh,0,0;
}
# Read the data using the file handle
sysread $fh,$self->{'sig'},4;
sysread $fh,$self->{'version'},4;
现在我想用Zlib解压缩数据块,并更新文件句柄$fh
以分配未压缩的数据。
我应该怎么做,是否有可能做到这一点,而不写未压缩的数据在磁盘上?
1条答案
按热度按时间4smxwvx51#
perl自带的解压缩模块可以从一个已经打开的文件句柄中读取。阅读将从当前偏移量开始,这样就很容易跳过文件头。
IO::Uncompress::*
模块特别创建了文件句柄对象,可以与普通的I/O函数一起使用,以允许透明使用;在创建它之后,你的代码就不在乎它是压缩的还是普通的源文件了。如果你要做很多小的输入操作,我会使用
read
而不是sysread
来利用内部缓冲。在同一个文件句柄上混合使用这两种形式将导致看似丢失数据的问题。