如何判断字节数组是否被gzip压缩

时间:2013-10-14 16:08:35

标签: c# vb.net gzip dotnetzip

如何查看字节数组是否包含gzip流?我的应用程序通过带有Base64编码的http post从其他应用程序获取文件。根据提供文件的应用程序的实现,可以对来自Base64字符串的字节数组进行gzip压缩。如何识别gzipped数组?我找到了一些方法,但我认为当有人上传zip文件或prepared "bad" zip file

时会出错

这是我发现和工作的东西,但是它可以以某种方式被利用吗?

C#

public static bool IsGZip(byte[] arr)
{
    return arr.Length >= 2 && arr[0] == 31 && arr[1] == 139;
}

VB.NET

Public Shared Function IsGZip(arr As Byte()) As Boolean
    Return arr.Length >= 2 AndAlso arr(0) = 31 AndAlso arr(1) = 139
End Function

如果IsGzip返回true,我的应用程序将解压缩字节数组。

1 个答案:

答案 0 :(得分:4)

执行您正在执行的操作,检查第三个字节是否为8,然后尝试将其压缩。最后一步是真正了解的唯一方法。如果gunzip失败,则按原样使用流。

相关问题