C#使用GZipStream实现压缩和解压缩

前言

我们在项目中,有可能会遇到存入到数据库或者传输的数据量比较大,这个时候,就可以考虑在存入数据库或者发送传输之前,将数据压缩下,当从数据库中取出时,再解压还原数据。

正文

废话不多说,我封装了一个工具类用于压缩和解压数据,代码如下:

  1 using System;
  2 using System.IO;
  3 using System.IO.Compression;
  4 using System.Text;
  5 
  6 namespace Test
  7 {
  8     public class GZipUtils
  9     {
 10         /// 
 11         /// 压缩指定字符串
 12         /// 
 13         /// 待压缩字符串
 14         /// 返回压缩后的字节数组
 15         public static byte[] Compress(string str)
 16         {
 17             if (string.IsNullOrEmpty(str)) return null;
 18 
 19             byte[] bytes = Encoding.UTF8.GetBytes(str);
 20             return Compress(bytes);
 21         }
 22 
 23         /// 
 24         /// 压缩指定字节数组
 25         /// 
 26         /// 待压缩字节数组
 27         /// 返回压缩后的字节数组
 28         public static byte[] Compress(byte[] bytes)
 29         {
 30             if (bytes == null || bytes.Length <= 0) return bytes;
 31 
 32             using (var compressedStream = new MemoryStream())
 33             {
 34                 using (var compressionStream = new GZipStream(compressedStream, CompressionMode.Compress))
 35                 {
 36                     compressionStream.Write(bytes, 0, bytes.Length);
 37                 }
 38                 return compressedStream.ToArray();
 39             }
 40         }
 41 
 42         /// 
 43         /// 压缩指定字符串到指定文件中
 44         /// 
 45         /// 待压缩字符串
 46         /// 压缩后的文件路径
 47         public static void CompressToFile(string compressData, string zipFilePath)
 48         {
 49             if (!string.IsNullOrEmpty(compressData))
 50             {
 51                 byte[] bytes = Encoding.UTF8.GetBytes(compressData);
 52                 using (var originalStream = new MemoryStream(bytes))
 53                 {
 54                     using (FileStream compressedStream = File.Create(zipFilePath))
 55                     {
 56                         using (GZipStream compressionStream = new GZipStream(compressedStream, CompressionMode.Compress))
 57                         {
 58                             originalStream.CopyTo(compressionStream);
 59                         }
 60                     }
 61                 }
 62             }
 63         }
 64 
 65         /// 
 66         /// 从指定字节数组解压出字符串
 67         /// 
 68         /// 待解压的字节数组
 69         /// 返回解压后的字符串
 70         public static string DecompressToString(byte[] bytes)
 71         {
 72             var result = Decompress(bytes);
 73             if (result == null || result.Length <= 0) return string.Empty;
 74 
 75             return Encoding.UTF8.GetString(result);
 76         }
 77 
 78         /// 
 79         /// 从指定字节数组解压出字节数组
 80         /// 
 81         /// 待解压的字节数组
 82         /// 返回解压后的字节数组
 83         public static byte[] Decompress(byte[] bytes)
 84         {
 85             if (bytes == null || bytes.Length <= 0) return bytes;
 86 
 87             using (var originalStream = new MemoryStream(bytes))
 88             {
 89                 using (var decompressedStream = new MemoryStream())
 90                 {
 91                     using (var decompressionStream = new GZipStream(originalStream, CompressionMode.Decompress))
 92                     {
 93                         decompressionStream.CopyTo(decompressedStream);
 94                     }
 95                     return decompressedStream.ToArray();
 96                 }
 97             }
 98         }
 99 
100         /// 
101         /// 从指定文件中解压出字符串
102         /// 
103         /// 待解压的文件路径
104         /// 返回解压后的字符串
105         public static string DecompressFromFile(string zipFilePath)
106         {
107             if (File.Exists(zipFilePath))
108             {
109                 using (FileStream originalStream = File.Open(zipFilePath, FileMode.Open))
110                 {
111                     using (MemoryStream decompressedStream = new MemoryStream())
112                     {
113                         using (GZipStream decompressionStream = new GZipStream(originalStream, CompressionMode.Decompress))
114                         {
115                             decompressionStream.CopyTo(decompressedStream);
116                         }
117                         byte[] bytes = decompressedStream.ToArray();
118                         return Encoding.UTF8.GetString(bytes);
119                     }
120                 }
121             }
122             return string.Empty;
123         }
124     }
125 }
View Code

参考文档

  1. C# 关于GZipStream压缩与解压缩数据丢失问题

  2. GZipStream 类 (System.IO.Compression)

转载于:https://www.cnblogs.com/CCHUncle/p/9550181.html

你可能感兴趣的:(C#使用GZipStream实现压缩和解压缩)