美文网首页
Gzip格式判断

Gzip格式判断

作者: 舍是境界 | 来源:发表于2021-09-05 12:29 被阅读0次

这里介绍Gzip格式判断代码,逻辑其实是一致的,对Gzip内部格式感兴趣的同学,可以参考Gzip Documention

  • 以java代码为例:
public static boolean isGzipped(byte[] input){
        if (input.length < 2) {
            return false;
        }
        
        return (input[0] & 0xff | (input[1] << 8 & 0xff00)) == GZIPInputStream.GZIP_MAGIC;
    }
  • 以go代码为例,其他同理:
func IsGzipped(input []byte) bool {
    if len(input) < 2 {
        return false
    }

    return (int64(input[0]) & 0xff | (int64(input[1] << 8) & 0xff00)) == 0x8b1f
}

相关文章

网友评论

      本文标题:Gzip格式判断

      本文链接:https://www.haomeiwen.com/subject/xmlpiltx.html