单独测试没有问题代码如下。        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = File.OpenRead("IFormatter.dat");
            byte[] bt = new byte[fs.Length];
            fs.Read(bt, 0, bt.Length);
            FileStream fsNew = File.Create("Compress.dat");
            GZipStream gzipStream = new GZipStream(fsNew, CompressionMode.Compress, false);
            gzipStream.Write(bt, 0, bt.Length);
            gzipStream.Close();
            gzipStream.Dispose();
            fs.Close();
            fs.Dispose();
            fsNew.Close();
            fsNew.Dispose();
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = File.OpenRead("Compress.dat");
            GZipStream gzip = new GZipStream(fs, CompressionMode.Decompress, false);
            FileStream output = File.Create("Decompress.dat");
            byte[] buff = new byte[64];
            int read = -1;
            read = gzip.Read(buff, 0, buff.Length);
            while (read > 0)
            {
                output.Write(buff, 0, read);
                read = gzip.Read(buff, 0, buff.Length);
            }
            gzip.Close();
            gzip.Dispose();
            fs.Close();
            fs.Dispose();
            output.Close();
            output.Dispose();
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            FileStream fs = File.OpenRead("123.xls");
            byte[] bttemp = new byte[fs.Length];
            FileStream fsNew = File.Create("IFormatter.dat");
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            fs.Read(bttemp,0,bttemp.Length);
            formatter.Serialize(ms, bttemp);
            byte[] bt = ms.ToArray();
            fsNew.Write(bt,0,bt.Length);
            fs.Close();
            fs.Dispose();
            ms.Close();
            ms.Dispose();
            fsNew.Close();
            fs.Dispose();
        }
        /// <summary>
        /// 发序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            
            FileStream fs = File.OpenRead("Decompress.dat");
            byte[] bt = new byte[fs.Length];
            fs.Read(bt, 0, bt.Length);
            FileStream fsNew = File.Create("test.xls");
            MemoryStream ms = new MemoryStream(bt);
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            byte[] btarr = (byte[])formatter.Deserialize(ms);
            fsNew.Write(btarr, 0, btarr.Length);
            fsNew.Flush();
            fs.Close();
            fs.Dispose();
            ms.Close();
            ms.Dispose();
            fsNew.Close();
            fsNew.Dispose();
        }放到服务器上面接收压缩的数据解压,反序列化就出错。代码
Server while (true)
            {
                byte[] dataLength = new byte[8];
                SocketClient.Receive(dataLength);
                long lLength = BitConverter.ToInt64(dataLength, 0);
                long lLengths = 0;
                byte[] data = new byte[1024];
                MemoryStream ms = new MemoryStream();
                int count = 0;
                while (lLength != lLengths)
                {
                    count = SocketClient.Receive(data, data.Length, SocketFlags.None);
                    lLengths += count;
                    ms.Write(data, 0, count);
                }
                if (lLengths == 0)
                    break;
                ///
                //
                ms.Position = 0;
                System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, false);
                MemoryStream output = new MemoryStream();
                byte[] buff = new byte[64];
                int read = -1;
                while ((read = gzip.Read(buff, 0, buff.Length)) > 0)
                {
                    output.Write(buff, 0, read);
                }
                System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                output.Position = 0;
                GT.Socket.Class cl = (GT.Socket.Class)formatter.Deserialize(output);Clientif (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                byte[] filedate = File.ReadAllBytes(openFileDialog1.FileName);
                GT.Socket.Class cl = new GT.Socket.Class("XXX", "1.1", GT.Socket.Type.SendFile, DateTime.Now, Path.GetFileName(openFileDialog1.FileName), filedate);
                System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                formatter.Serialize(ms, cl);
                byte[] data = ms.ToArray();                MemoryStream mstemp = new MemoryStream();
                System.IO.Compression.GZipStream gzipStream = new System.IO.Compression.GZipStream(mstemp, System.IO.Compression.CompressionMode.Compress, false);
                gzipStream.Write(data, 0, data.Length);
                byte[] bt = mstemp.ToArray();
                ms.Close();
                ms.Dispose();
                mstemp.Close();
                mstemp.Dispose();                //byte[] data = GT.BinaryFormatter.SerializeCompress(new GT.Socket.Class("高仲星", "1.1", GT.Socket.Type.SendFile, DateTime.Now, Path.GetFileName(openFileDialog1.FileName), filedate));                ClientSocket.Send(BitConverter.GetBytes(bt.Length));
                Thread.Sleep(50);
                ClientSocket.Send(bt);
                byte[] btreceive = new byte[2];
                ClientSocket.Receive(btreceive);
                MessageBox.Show(Encoding.ASCII.GetString(btreceive));
            }

解决方案 »

  1.   

    估计要加一句Seek(0),把位置移动开始
      

  2.   

    反序列化前加上一句
    output.Seek(0, SeekOrigin.Begin);
      

  3.   

    流程
    客户端先给类,序列化然后压缩,发送给服务器,服务器接收解压反序列化。
    客户端先发给服务器数据长度,再发数据,再接收服务器是否成功接收数据。
    测试
    我测试少量数据没问题,比给发送的类的最后一个写入字符串,如果给类最后放入byte数组,这个数组是个文件,测试文件29M。就会出错。
    这是什么问题  有谁遇到过?
      

  4.   

    应该判断一下流的长度吧,而不应该根据你规定的固定的64来读取,如下 System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, false);
                    BinaryReader reader = new BinaryReader(gzip);
                    int len = reader.ReadInt32();//判断压缩流的长度
                    MemoryStream output = new MemoryStream();
                    byte[] buff = new byte[64];
                    int read = -1;
                    len = len > buff.Length ? buff.Length : len;
                    while ((read = gzip.Read(buff, 0, len)) > 0)
                    {
                        output.Write(buff, 0, read);
                    }