Is there a way to know if the byte[] has been compressed by gzipstream?

只愿长相守 提交于 2020-02-02 04:54:42

问题


Is there a way to know if the byte[] has been compressed (or not) by GzipStream .net class?

EDIT: Just want to know if the byte[] array has been compressed (since I will always be using GzipStream to compress and decompress)


回答1:


A GZipStream is a DeflateStream with an additional header and trailer.

The format is specified in RFC 1952.


The .NET 4.0 GZipStream class writes the following bytes as header:

byte[] headerBytes = new byte[] { 0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 4, 0 };
if (compressionLevel == 10)
{
    headerBytes[8] = 2;
}

The trailer consists of a CRC32 checksum and the length of the uncompressed data.




回答2:


Thanks to @dtd's explaination, this works for me: (@stackoverflowuser, you may want this?)

public static class CompressionHelper
{
    public static byte[] GZipHeaderBytes = {0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 4, 0};
    public static byte[] GZipLevel10HeaderBytes = {0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 2, 0};

    public static bool IsPossiblyGZippedBytes(this byte[] a)
    {
        var yes = a.Length > 10;

        if (!yes)
        {
            return false;
        }

        var header = a.SubArray(0, 10);

        return header.SequenceEqual(GZipHeaderBytes) || header.SequenceEqual(GZipLevel10HeaderBytes);
    }
}



回答3:


you could look at the first few bytes for the magic header to see if it is gzipped, but unless the .net compressor writes additional info into one of the comment or other optional fields, you probably can't tell who the compressor was.

http://www.onicos.com/staff/iz/formats/gzip.html

you could also look at the OS type field to see if it was FAT or NTFS, but that still doesn't tell you it was written by C#...



来源:https://stackoverflow.com/questions/4662821/is-there-a-way-to-know-if-the-byte-has-been-compressed-by-gzipstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!