开发者

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

Is there a way to know if the byte[] has been compressed (or开发者_C百科 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)


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.


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);
    }
}


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#...


The answers posted above do the job most of the times, but be aware that the last byte in the header denotes the operation system id. This means that this code might work locally, but might not work when deployed to Azure. In my case, that last byte was not 0 as in the example, but 0xA (10).

See als https://en.wikipedia.org/wiki/Gzip for a little explanation of the header.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜