2016-12-20 1 views
1

서버 측 응용 프로그램에서 압축 된 문자열 데이터를 수신하고 있습니다. 그런 다음 문자열 압축을 풀고 있습니다.받은 데이터가 압축되어 있는지 어떻게 확인할 수 있습니까?문자열이 이미 압축되어 있는지 확인합니다.

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

답변

1

압축 해제가 작동

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

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

예를 성공하면 당신은 시도 할 수 있습니다. 덕분에 –

+0

당신을 환영합니다! – fubo

관련 문제