我試圖壓縮和解壓縮內存流發送它通過TCP連接。 在下面的代碼中,我會在壓縮之後立即進行解壓縮,以使其首先工作。 我做了什麼我最終與一個devompressed緩衝區機智全零和在行 int read = Decompress.Read(buffie,0,buffie.Length); 似乎讀取了0個字節。壓縮和解壓在.net結束與零解壓數組
有沒有人有線索有什麼問題?
bytesRead = ms.Read(buf, 0, i);
MemoryStream partialMs = new MemoryStream();
GZipStream gZip = new GZipStream(partialMs, CompressionMode.Compress);
gZip.Write(buf, 0, buf.Length);
partialMs.Position = 0;
byte[] compressedBuf = new byte[partialMs.Length];
partialMs.Read(compressedBuf, 0, (int)partialMs.Length);
partialMs.Close();
byte[] gzBuffer = new byte[compressedBuf.Length + 4];
System.Buffer.BlockCopy(compressedBuf, 0, gzBuffer, 4, compressedBuf.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buf.Length), 0, gzBuffer, 0, 4);
using (MemoryStream mems = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
byte[] buffie = new byte[msgLength];
mems.Write(gzBuffer, 4, gzBuffer.Length - 4);
mems.Flush();
mems.Position = 0;
using (GZipStream Decompress = new GZipStream(mems, CompressionMode.Decompress, true))
{
int read = Decompress.Read(buffie, 0, buffie.Length);
Decompress.Close();
}
}
PS:發佈審查斷碼的預期和接受。發佈_incomplete_ _broken_代碼通常會讓你無處可尋。這就像打電話給醫生,問他你的貓有什麼問題。只是說... ;-) – 2010-02-16 08:07:37