爱思开
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.2 KiB

2 years ago
  1. using ICSharpCode.SharpZipLib.Checksums;
  2. using ICSharpCode.SharpZipLib.Core;
  3. using ICSharpCode.SharpZipLib.Zip;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. namespace NFine.Code
  12. {
  13. public class GZip
  14. {
  15. /// <summary>
  16. /// 压缩
  17. /// </summary>
  18. /// <param name="text">文本</param>
  19. public static string Compress(string text)
  20. {
  21. if (text.IsEmpty())
  22. return string.Empty;
  23. byte[] buffer = Encoding.UTF8.GetBytes(text);
  24. return Convert.ToBase64String(Compress(buffer));
  25. }
  26. /// <summary>
  27. /// 解压缩
  28. /// </summary>
  29. /// <param name="text">文本</param>
  30. public static string Decompress(string text)
  31. {
  32. if (text.IsEmpty())
  33. return string.Empty;
  34. byte[] buffer = Convert.FromBase64String(text);
  35. using (var ms = new MemoryStream(buffer))
  36. {
  37. using (var zip = new GZipStream(ms, CompressionMode.Decompress))
  38. {
  39. using (var reader = new StreamReader(zip))
  40. {
  41. return reader.ReadToEnd();
  42. }
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// 压缩
  48. /// </summary>
  49. /// <param name="buffer">字节流</param>
  50. public static byte[] Compress(byte[] buffer)
  51. {
  52. if (buffer == null)
  53. return null;
  54. using (var ms = new MemoryStream())
  55. {
  56. using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
  57. {
  58. zip.Write(buffer, 0, buffer.Length);
  59. }
  60. return ms.ToArray();
  61. }
  62. }
  63. /// <summary>
  64. /// 解压缩
  65. /// </summary>
  66. /// <param name="buffer">字节流</param>
  67. public static byte[] Decompress(byte[] buffer)
  68. {
  69. if (buffer == null)
  70. return null;
  71. return Decompress(new MemoryStream(buffer));
  72. }
  73. /// <summary>
  74. /// 压缩
  75. /// </summary>
  76. /// <param name="stream">流</param>
  77. public static byte[] Compress(Stream stream)
  78. {
  79. if (stream == null || stream.Length == 0)
  80. return null;
  81. return Compress(StreamToBytes(stream));
  82. }
  83. /// <summary>
  84. /// 解压缩
  85. /// </summary>
  86. /// <param name="stream">流</param>
  87. public static byte[] Decompress(Stream stream)
  88. {
  89. if (stream == null || stream.Length == 0)
  90. return null;
  91. using (var zip = new GZipStream(stream, CompressionMode.Decompress))
  92. {
  93. using (var reader = new StreamReader(zip))
  94. {
  95. return Encoding.UTF8.GetBytes(reader.ReadToEnd());
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 流转换为字节流
  101. /// </summary>
  102. /// <param name="stream">流</param>
  103. public static byte[] StreamToBytes(Stream stream)
  104. {
  105. stream.Seek(0, SeekOrigin.Begin);
  106. var buffer = new byte[stream.Length];
  107. stream.Read(buffer, 0, buffer.Length);
  108. return buffer;
  109. }
  110. /// <summary>
  111. /// 多文件压缩
  112. /// </summary>
  113. public static void ZipFileMainURL(string[] filenames, string[] filename, string name, int Level)
  114. {
  115. using (FileStream ZipFile = File.Create(name))
  116. {
  117. using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
  118. {
  119. int n = 0;
  120. foreach (string fileToZip in filenames)
  121. {
  122. string fileName = filename[n].ToString();
  123. var url = fileToZip;
  124. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  125. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  126. Stream responseStream = response.GetResponseStream();
  127. ZipEntry ZipEntry = new ZipEntry(fileName);
  128. ZipStream.PutNextEntry(ZipEntry);
  129. ZipStream.SetLevel(Level);
  130. var ms = new MemoryStream();
  131. responseStream.CopyTo(ms);
  132. byte[] bytes = ms.ToArray();
  133. ZipStream.Write(bytes, 0, bytes.Length);
  134. n++;
  135. responseStream.Close();
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 多文件压缩
  142. /// </summary>
  143. public static void ZipFileMain(string[] filenames, string[] filename, string name, int Level)
  144. {
  145. //ZipOutputStream s = new ZipOutputStream(File.Create(name));
  146. //Crc32 crc = new Crc32();
  147. ////压缩级别
  148. //s.SetLevel(Level);// 0 - store only to 9 - means best compression
  149. //try
  150. //{
  151. // int m = 0;
  152. // foreach (string file in filenames)
  153. // {
  154. // //打开压缩文件
  155. // FileStream fs = File.OpenRead(file);//文件地址
  156. // byte[] buffer = new byte[fs.Length];
  157. // fs.Read(buffer, 0, buffer.Length);
  158. // //建立压缩实体
  159. // ZipEntry entity = new ZipEntry(filename[m].ToString());//源文件名
  160. // //时间
  161. // entity.DateTime = DateTime.Now;
  162. // //控件大小
  163. // entity.Size = fs.Length;
  164. // fs.Close();
  165. // crc.Reset();
  166. // crc.Update(buffer);
  167. // entity.Crc = crc.Value;
  168. // s.PutNextEntry(entity);
  169. // s.Write(buffer, 0, buffer.Length);
  170. // m++;
  171. // }
  172. //}
  173. //catch (Exception)
  174. //{
  175. // throw;
  176. //}
  177. //finally
  178. //{
  179. // s.Finish();
  180. // s.Close();
  181. //}
  182. //生成的压缩文件为test.zip
  183. using (FileStream fsOut = File.Create(name))
  184. {
  185. //ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
  186. using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
  187. {
  188. int i = 0;
  189. foreach (string item in filenames)
  190. {
  191. string fileName = item;
  192. FileInfo fi = new FileInfo(fileName);
  193. //entryName就是压缩包中文件的名称。
  194. string entryName = filename[i];
  195. //ZipEntry类代表了一个压缩包中的一个项,可以是一个文件,也可以是一个目录。
  196. ZipEntry newEntry = new ZipEntry(entryName);
  197. newEntry.DateTime = fi.LastWriteTime;
  198. newEntry.Size = fi.Length;
  199. //把压缩项的信息添加到ZipOutputStream中。
  200. zipStream.PutNextEntry(newEntry);
  201. byte[] buffer = new byte[4096];
  202. //把需要压缩文件以文件流的方式复制到ZipOutputStream中。
  203. using (FileStream streamReader = File.OpenRead(fileName))
  204. {
  205. StreamUtils.Copy(streamReader, zipStream, buffer);
  206. }
  207. zipStream.CloseEntry();
  208. i++;
  209. }
  210. //使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
  211. zipStream.IsStreamOwner = false;
  212. zipStream.Finish();
  213. zipStream.Close();
  214. }
  215. }
  216. }
  217. }
  218. }