Exemplo n.º 1
0
        /// <summary>
        /// �������л���ѹ��
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] Compress(object obj, Type type)
        {
            byte[] compressedBuf;
            CompressionSerialize compressionSerialize = new CompressionSerialize();

            #region serialize
            byte[] buf = compressionSerialize.Serialize(obj, type);
            #endregion

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);

            gs.Write(buf, 0, buf.Length);
            gs.Close();

            compressedBuf = ms.ToArray();

            return compressedBuf;
        }
Exemplo n.º 2
0
        /// <summary>
        /// �����ѹ���������л�
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static object Decompress(byte[] buffer, Type type)
        {
            System.IO.MemoryStream ms3 = new System.IO.MemoryStream();
            System.IO.MemoryStream ms2 = new System.IO.MemoryStream(buffer);
            System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms2, System.IO.Compression.CompressionMode.Decompress);

            byte[] writeData = new byte[4096];

            while (true)
            {
                int size = gs.Read(writeData, 0, writeData.Length);
                if (size > 0)
                {
                    ms3.Write(writeData, 0, size);
                }
                else
                {
                    break;
                }
            }

            gs.Close();
            ms3.Flush();
            byte[] DecompressBuf = ms3.ToArray();

            #region deserialize
            CompressionSerialize compressionSerialize = new CompressionSerialize();
            return compressionSerialize.Deserialize(DecompressBuf);

            #endregion
        }