我从服务器端应用程序接收压缩字符串数据,然后我正在解压缩字符串,如何验证我收到的数据是否被压缩。
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);
}
}
答案 0 :(得分:2)
如果解压缩成功,您可以尝试
else