简体   繁体   English

perl gunzip到缓冲区和gunzip到文件具有不同的字节顺序

[英]perl gunzip to buffer and gunzip to file have different byte orders

I'm using Perl v5.22.1, Storable 2.53_01, and IO::Uncompress::Gunzip 2.068. 我正在使用Perl v5.22.1, Storable 2.53_01和IO::Uncompress::Gunzip 2.068。

I want to use Perl to gunzip a Storable file in memory, without using an intermediate file. 我想用Perl来用gunzip一个Storable在内存中的文件,而无需使用中间文件。

I have a variable $zip_file = '/some/storable.gz' that points to this zipped file. 我有一个变量$zip_file = '/some/storable.gz'指向此压缩文件。

If I gunzip directly to a file, this works fine, and %root is correctly set to the Storable hash. 如果我直接将文件压缩为文件,则可以正常工作,并且%root已正确设置为Storable哈希。

gunzip($zip_file, '/home/myusername/Programming/unzipped');
my %root = %{retrieve('/home/myusername/Programming/unzipped')};

However if I gunzip into memory like this: 但是,如果我像这样将其压缩到内存中:

my $file;
gunzip($zip_file, \$file);
my %root = %{thaw($file)};

I get the error 我得到错误

 Storable binary image v56.115 more recent than I am (v2.10)` 

so the Storable's magic number has been butchered: it should never be that high. 因此Storable的魔力数字被人宰了:永远不要那么高。

However, the strings in the unzipped buffer are still correct; 但是,解压缩缓冲区中的字符串仍然正确; the buffer starts with pst which is the correct Storable header. 缓冲区以pst开头, pst是正确的Storable标头。 It only seems to be multi-byte variables like integers which are being broken. 它似乎只是被破坏的多字节变量,例如整数。

Does this have something to do with byte ordering, such that writing to a file works one way while writing to a file buffer works in another? 这是否与字节顺序有关,例如,写入文件的一种方式起作用,而写入文件缓冲区的另一种方式? How can I gunzip to a buffer without it ruining my integers? 如何在不破坏整数的情况下将其压缩到缓冲区?

That's not related to unzip but to using retrieve vs. thaw . 这是不相关的解压缩,但使用retrievethaw They both expect different input, ie thaw expect the output from freeze while retrieve expects the output from store . 他们俩都期望输入不同,即thaw期望freeze的输出,而retrieve期望store的输出。 This can be verified with a simple test: 可以通过一个简单的测试来验证:

$ perl -MStorable -e 'my $x = {}; store($x,q[file.store])'
$ perl -MStorable=freeze -e 'my $x = {}; print freeze($x)' > file.freeze

On my machine this gives 24 bytes for the file created by store and 20 bytes for freeze . 在我的机器上,这将为store创建的文件提供24个字节,为freeze 20个字节。 If I remove the leading 4 bytes from file.store the file is equivalent to file.freeze , ie store just added a 4 byte header. 如果我从file.store删除前4个字节, file.store该文件等效于file.freeze ,即store仅添加了4个字节的标头。 Thus you might try to uncompress the file in memory, remove the leading 4 bytes and run thaw on the rest. 因此,您可以尝试解压缩内存中的文件,删除前4个字节,然后对其余字节进行thaw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM