C# 验证字符串是否已压缩

C# 验证字符串是否已压缩,c#,string,compression,C#,String,Compression,我从服务器端应用程序接收压缩的字符串数据,然后解压字符串,如何验证我接收的数据是否压缩 string compressedData = Getting from some function; //What can i add here to check if the compressedData string contain already compressed string or not. string decompressedData = Decompress(compresse

我从服务器端应用程序接收压缩的字符串数据,然后解压字符串,如何验证我接收的数据是否压缩

string compressedData = Getting from some function;

//What can i add here to check if the compressedData string contain already compressed string or not.

    string decompressedData = Decompress(compressedData);


    //Decompress Function
        static string Decompress(string compressedText)
            {
                if (string.IsNullOrEmpty(compressedText))
                    return "";

                byte[] gzBuffer = Convert.FromBase64String(compressedText);
                using (MemoryStream ms = new MemoryStream())
                {
                    int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                    ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                    byte[] buffer = new byte[msgLength];

                    ms.Position = 0;
                    using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        zip.Read(buffer, 0, buffer.Length);
                    }

                    return Encoding.UTF8.GetString(buffer);
                }
            }

如果解压缩成功,您可以尝试

string compressedData = "Getting from some function";
string decompressedData = null;

bool compressed = true; //your result
try
{
    decompressedData = Decompress(compressedData);
}
catch
{
    compressed = false;
}