在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前言我们在项目中,有可能会遇到存入到数据库或者传输的数据量比较大,这个时候,就可以考虑在存入数据库或者发送传输之前,将数据压缩下,当从数据库中取出时,再解压还原数据。 正文废话不多说,我封装了一个工具类用于压缩和解压数据,代码如下: 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 /// <summary> 11 /// 压缩指定字符串 12 /// </summary> 13 /// <param name="str">待压缩字符串</param> 14 /// <returns>返回压缩后的字节数组</returns> 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 /// <summary> 24 /// 压缩指定字节数组 25 /// </summary> 26 /// <param name="bytes">待压缩字节数组</param> 27 /// <returns>返回压缩后的字节数组</returns> 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 /// <summary> 43 /// 压缩指定字符串到指定文件中 44 /// </summary> 45 /// <param name="compressData">待压缩字符串</param> 46 /// <param name="zipFilePath">压缩后的文件路径</param> 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 /// <summary> 66 /// 从指定字节数组解压出字符串 67 /// </summary> 68 /// <param name="bytes">待解压的字节数组</param> 69 /// <returns>返回解压后的字符串</returns> 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 /// <summary> 79 /// 从指定字节数组解压出字节数组 80 /// </summary> 81 /// <param name="bytes">待解压的字节数组</param> 82 /// <returns>返回解压后的字节数组</returns> 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 /// <summary> 101 /// 从指定文件中解压出字符串 102 /// </summary> 103 /// <param name="zipFilePath">待解压的文件路径</param> 104 /// <returns>返回解压后的字符串</returns> 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 } 参考文档 |
请发表评论