日期:2014-05-17 浏览次数:21044 次
#region 压缩解压object public static byte[] CompressionObject(object DataOriginal) { if (DataOriginal == null) return null; BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(); bFormatter.Serialize(mStream, DataOriginal); byte[] bytes = mStream.ToArray(); MemoryStream oStream = new MemoryStream(); DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress); zipStream.Write(bytes, 0, bytes.Length); zipStream.Flush(); zipStream.Close(); return oStream.ToArray(); } public static object DecompressionObject(byte[] bytes) { if (bytes == null) return null; MemoryStream mStream = new MemoryStream(bytes); mStream.Seek(0, SeekOrigin.Begin); DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true); object dsResult = null; BinaryFormatter bFormatter = new BinaryFormatter(); dsResult = (object)bFormatter.Deserialize(unZipStream); return dsResult; } #endregion
------解决方案--------------------
先确保你的GZip流是否正确,如果你是使用了DecompressionObject(byte[] bytes)方法还是出现这问题,那问题就是GZip不完整。原因有很多,如果你是通过网络传输的,那就是没有接收完整或者发送方没有发送完整,最好给出相关代码。