简体   繁体   English

NET版本之间的GZipStream标头是否可靠?

[英]Is GZipStream header reliable across .NET versions?

I came to the Q&A Is there a way to know if the byte[] has been compressed by gzipstream? 我来参加问答环节, 有没有办法知道byte []是否已被gzipstream压缩? and some author states (and it's true) that GZipStream puts {0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 4, 0} characters as header to know if a byte array is a compressed string. 一些作者指出(的确是这样), GZipStream{0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 4, 0}字符用作标头,以了解字节数组是否为压缩字符串。

And my question is, is GZipStream header reliable across .NET versions? 我的问题是, .NET版本的GZipStream标头是否可靠?

It should be reliable, because this header is from the GZip specification and therefore not .NET specific. 它应该是可靠的,因为此标头来自GZip规范,因此不特定于.NET。 See here for an explanation of these values. 有关这些值的说明,请参见此处

However, according to the specification, only the two first bytes are actually always the same. 但是,根据规范,实际上只有两个前字节始终相同。 The third byte is practically always the same, because currently only one valid value exists. 第三个字节实际上总是相同的,因为当前仅存在一个有效值。 The following bytes might change. 以下字节可能会更改。

With any GZip format stream you are guarnateed: 对于任何GZip格式的流,您都可以保证:

First two bytes: 1f , 8b 前两个字节: 1f8b

Next byte: 00 for store (no compression), 01 for compress algorithm, 02 for pack, 03 for lzf and 08 for deflate. 下一个字节: 00用于存储(无压缩), 01用于压缩算法, 02用于压缩, 03用于lzf, 08用于压缩。 .NET so-far always uses deflate and many situations expect only deflate (only deflate-based gzip is expected by web clients as a transfer or content encoding marked as gzip ) so it would be unlikely to change without some sort of option to specify it being added. 到目前为止,.NET始终使用deflate,许多情况下只希望使用deflate(Web客户端仅将基于deflate的gzip作为传输或内容编码标记为gzip ),因此如果没有某种选项来指定它,就不太可能更改被添加。

The next is the file type, with 00 meaning "probably some sort of text file" Since GZipStream has no information on the file type, it always uses that. 下一个是文件类型, 00表示“可能是某种文本文件”。由于GZipStream没有有关文件类型的信息,因此它始终使用该类型。

The next four are file-modification time in Unix format. 接下来的四个是Unix格式的文件修改时间。 Again, since the class has no information about the file–as it receives a stream, not a file with metadata, these are always set to 0. 同样,由于该类没有有关文件的信息-当它接收流而不是包含元数据的文件时,它们始终设置为0。

The next byte depends on the compression method. 下一个字节取决于压缩方法。 With deflate it could be 2 to indicate heavy compression or 4 to indicate light compression. 使用deflate ,可能是2表示重压或4表示轻压。

The next (last in your sequence) depends on the OS type in use. 下一个(最后一个)取决于所使用的OS类型。 0 means "FAT Filesystem" but has continued to be used by Windows as Windows has moved to use other file systems like NTFS. 0表示“ FAT文件系统”,但是Windows继续使用它,因为Windows已移至使用其他文件系统(例如NTFS)。 It could potentially have a different value if used with Mono on a non-Windows file system, though that situation could also potentially decide to match the .NET behaviour. 如果在非Windows文件系统上与Mono一起使用,则它可能具有不同的值,尽管这种情况也可能决定与.NET行为匹配。 (Update: At least some versions of Mono will set the file-system flag to something other than 0 on non-Windows systems). (更新:在非Windows系统上,至少某些版本的Mono将文件系统标志设置为非0 )。

A gzip stream is assured to start with 0x1f 0x8b 0x08 . 确保gzip流以0x1f 0x8b 0x08开头。 There is no other compression method supported than the 0x08 in the third byte. 除了第三个字节中的0x08 ,没有其他压缩方法受支持。

So if you don't see 0x1f 0x8b 0x08 , then it's not a gzip stream. 因此,如果您没有看到0x1f 0x8b 0x08 ,那么它不是gzip流。 However if you do see 0x1f 0x8b 0x08 , then it may or may not be a gzip stream. 但是,如果确实看到0x1f 0x8b 0x08 ,则它可能是gzip流, 可能不是 gzip流。 It probably is, but you can't assume that. 可能是这样,但您不能假设那样。

What you should do with a candidate gzip file is to simply start decompressing it as such. 您应该对候选gzip文件执行的操作就是简单地开始将其解压缩。 The decoder will immediately recognize if there is no gzip header, and will furthermore soon detect a problem in the compressed data if there is an accidental gzip header. 解码器将立即识别出是否没有gzip标头,并且如果有意外的gzip标头,将很快检测到压缩数据中的问题。 You shouldn't have to check for the header, since the decoder already does, as well as check for valid compressed data after that. 您不必检查标题,因为解码器已经检查过了,然后再检查有效的压缩数据。

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

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