这里介绍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
}
网友评论