GZip对字符串压缩和解压

 1         /// <summary>
 2         /// 压缩
 3         /// </summary>
 4         /// <param name="value">需要压缩字符串</param>
 5         /// <returns>结果</returns>
 6         public static string Compression(string value)
 7         {
 8             byte[] data = Encoding.UTF8.GetBytes(value);
 9             using (MemoryStream ms = new MemoryStream())
10             {
11                 using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true))
12                 {
13                     compressedzipStream.Write(data, 0, data.Length);
14                 }
15                 return Convert.ToBase64String(ms.ToArray());
16             }
17         }
18 
19         /// <summary>
20         /// 解压
21         /// </summary>
22         /// <param name="value">需要解压字符串</param>
23         /// <returns>结果</returns>
24         public static string Decompress(string value)
25         {
26             byte[] data = Convert.FromBase64String(value);
27             using (MemoryStream ms = new MemoryStream(data))
28             {
29                 using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress))
30                 {
31                     using (MemoryStream outBuffer = new MemoryStream())
32                     {
33                         byte[] block = new byte[1024];
34                         int bytesRead = compressedzipStream.Read(block, 0, block.Length);
35                         outBuffer.Write(block, 0, bytesRead);
36                         return Encoding.UTF8.GetString(outBuffer.ToArray());
37                     }
38                 }
39             }
40         }

 

        /// <summary>
        /// 解压(数据量过大的时候解压)
        /// </summary>
        /// <param name="value">需要解压字符串</param>
        /// <returns>结果</returns>
        public static string Decompress(string value)
        {
            byte[] data = Convert.FromBase64String(value);
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream outBuffer = new MemoryStream())
                    {
                        byte[] block = new byte[1024];
                        int bytesRead;
                        do
                        {
                            bytesRead = compressedzipStream.Read(block, 0, block.Length);
                            outBuffer.Write(block, 0, bytesRead);
                        } while (bytesRead > 0);
                        return Encoding.UTF8.GetString(outBuffer.ToArray());
                    }
                }
            }
        }

 

 

 

1         /// <summary>
2         /// 测试
3         /// </summary>
4         public static void TT()
5         {
6             var json = "{\"Key\":\"Value\"}";
7             var ys = Compression(json);
8             var jy = Decompress(ys);
9         }

 

posted @ 2016-05-19 15:00  刘小吉  阅读(1936)  评论(0编辑  收藏  举报