我试图压缩和解压缩内存流发送它通过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