我需要壓縮字符串以減小Web服務響應的大小。我在SharpZipLib示例中看到了單元測試,但並不是我需要的示例。SharpZipLib壓縮字符串
在下面的代碼,用於ZipOutputStream構造函數返回異常:「沒有開放准入」
byte[] buffer = Encoding.UTF8.GetBytes(SomeLargeString);
Debug.WriteLine(string.Format("Original byes of string: {0}", buffer.Length));
MemoryStream ms = new MemoryStream();
using (ZipOutputStream zipStream = new ZipOutputStream(ms))
{
zipStream.Write(buffer, 0, buffer.Length);
Debug.WriteLine(string.Format("Compressed byes: {0}", ms.Length));
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
string compressedString = Convert.ToBase64String (gzBuffer);
哪裏我下車的軌道?我是否讓這件事比應該更復雜?