2012-11-06 3 views
0

간단한 프로그램을 만들었습니다.바이너리를 문자열로 변환하지 않습니다.

문자열을 만들고 다음 방법으로 압축 한 다음 SQL Server 2008 (binary(1000) 필드 형식)의 이진 데이터 필드 형식에 저장합니다.

바이너리 데이터와 결과 문자열이 동일한 길이와 데이터를 가진 원래 문자열 데이터와 동일하지만 압축을 풀 때 오류가 발생했습니다.

System.Text.ASCIIEncoding.ASCII.GetBytes(mystring) 

그리고 문자열을 얻을 수있는이 방법 :

나는 바이트 얻기 위해이 방법을 사용 VS2012 편집기에서 하드 코드에서

System.Text.ASCIIEncoding.ASCII.GetString(binarydata) 

는, 결과 문자열이 잘 작동을하지만 난 그것을 읽을 때 sql에서 압축 해제 방법의 첫 번째 줄에이 오류가 표시됩니다.

The input is not a valid Base-64 string as it contains a 
non-base 64 character, more than two padding characters, 
or a non-white space character among the padding characters. 

내 코드에 어떤 문제가 있습니까? 이 두 문자열은 동일하지만

string test1=Decompress("mystring"); 

...이 방법은 잘 작동하지만 나에게 그 오류를주고 검색된 문자열을 압축 해제 할 수없는 이러한 문자열을하지 않는 비교하는 어떤 존중을 보여줍니다

string temp=System.Text.ASCIIEncoding.ASCII.GetString(get data from sql) ; 
string test2=Decompress(temp); 

int result = string.Compare(test1, test2); // result=0 

내 압축 방법 :

public static string Compress(string text) 
    { 
     byte[] buffer = Encoding.UTF8.GetBytes(text); 
     var memoryStream = new MemoryStream(); 
     using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) 
     { 
      gZipStream.Write(buffer, 0, buffer.Length); 
     } 

     memoryStream.Position = 0; 

     var compressedData = new byte[memoryStream.Length]; 
     memoryStream.Read(compressedData, 0, compressedData.Length); 

     var gZipBuffer = new byte[compressedData.Length + 4]; 
     Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length); 
     Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4); 
     return Convert.ToBase64String(gZipBuffer); 
    } 
,536,913,632 10

내 압축 해제 방법 :

public static string Decompress(string compressedText) 
    { 
     byte[] gZipBuffer = Convert.FromBase64String(compressedText); 
     using (var memoryStream = new MemoryStream()) 
     { 
      int dataLength = BitConverter.ToInt32(gZipBuffer, 0); 
      memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); 

      var buffer = new byte[dataLength]; 

      memoryStream.Position = 0; 
      using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) 
      { 
       gZipStream.Read(buffer, 0, buffer.Length); 
      } 

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

시험하여 압축/압축 해제 방법. 그들은 서로 독립적으로 올바르게 작동합니까? – prprcupofcoffee

+0

나는이 메소드들을 별도로 테스트한다. – motevalizadeh

+0

바이너리 필드에 저장하는 이유는 무엇입니까? 왜 Convert.ToBase64String (gZipBuffer); ?, 또한 ASCII.GetBytes'가 아닌'UTF8.GetBytes'를 사용하고 있습니다 –

답변

2

가능성이 가장 높은 문제는 당신이 제출 한 SQL 바이너리에서 문자열을 받고있는 방법이다. 현재

(나는 당신이 저장하는 방법을 보여 주었다 또는 SQL에서 데이터를 검색하지 않은 추측)

  • 압축 : Text -> UTF8.GetBytes -> compress -> base64 string-> Send to Sql (transformed to binary)
  • 압축 해제가 : 귀하의 문제는 String representation of binary 단계

입니다 Binary -> String representation of binary -> base64 decode -> decompress -> UTF8.GetStringSend to Sql (transformed to binary)과 같지 않습니다. 이것을 varbinary으로 저장하는 경우에는 압축에서 바이트 배열을 반환해야하며 압축 해제는 바이트 배열을 가져야합니다.

public byte[] string Compress(string text) 
{ 
    //Snip 
} 

public static string Decompress(byte[] compressedText) 
{ 
    //Snip 
} 

  • 압축하는 프로세스를 변경합니다 Text -> UTF8.GetBytes -> compress -> Send to Sql
  • 압축 해제 : 데이터베이스 저장/검색 방법 별도로 Binary -> decompress -> UTF8.GetString
관련 문제