在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间。压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式。压缩与解压缩的方法也很多,在.NET 2.0开始,在System.IO.Compression中微软已经给我们提供了解压缩的方法GZipStream。对于GZipStream的使用以及优缺点网上已经有非常多的文章,本文主要讲的是利用三方开源组件ICSharpCode.SharpZipLib进行文件的解压缩。

  SharpZipLib地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

  SharpZipLib是一个使用C#编写的Zip操作类库,是一个开源的C#压缩解压库,应用非常广泛。在VB.NET、C#或其他的.NET语言中都可以使用它创建Zip文件、并进行读取和更新等操作。SharpZipLib是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩。SharpZipLib目前的版本为0.86,我们可以直接从上面提供的网站下载dll文件再添加到项目引用中,也可以通过VS提供的包管理工具NuGet把SharpZipLib添加到项目中。NuGet能更方便地把一些dll和文件添加到项目中,而不需要从文件中复制拷贝,推荐使用。使用NuGet添加SharpZipLib到项目中的方法如下图所示,在我们需要SharpZipLib的项目中右键单击“引用”,在弹出的快捷菜单中选择“管理NuGet程序包(N)…”。

  在打开的“管理NuGet程序包”对话框,搜索SharpZipLib找到后单击安装即可。

  引用SharpZipLib到项目中后,我们就可以编写相应的加压缩方法,下面将对常用的方法一一分享。

  在使用前必须先添加引用如下:

  1. using ICSharpCode.SharpZipLib.Checksums;
  2. using ICSharpCode.SharpZipLib.Zip;  

  一、压缩文件夹

  1.      /// <summary>
  2. /// 压缩文件夹
  3. /// </summary>
  4. /// <param name="dirToZip"></param>
  5. /// <param name="zipedFileName"></param>
  6. /// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
  7. public static void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9)
  8. {
  9. if (Path.GetExtension(zipedFileName) != ".zip")
  10. {
  11. zipedFileName = zipedFileName + ".zip";
  12. }
  13. using (var zipoutputstream = new ZipOutputStream(File.Create(zipedFileName)))
  14. {
  15. zipoutputstream.SetLevel(compressionLevel);
  16. Crc32 crc = new Crc32();
  17. Hashtable fileList = GetAllFies(dirToZip);
  18. foreach (DictionaryEntry item in fileList)
  19. {
  20. FileStream fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  21. byte[] buffer = new byte[fs.Length];
  22. fs.Read(buffer, 0, buffer.Length);;
  23. ZipEntry entry = new ZipEntry(Path.GetFileName(item.Key.ToString()))
  24. {
  25. DateTime = (DateTime)item.Value,
  26. Size = fs.Length
  27. };
  28. fs.Close();
  29. crc.Reset();
  30. crc.Update(buffer);
  31. entry.Crc = crc.Value;
  32. zipoutputstream.PutNextEntry(entry);
  33. zipoutputstream.Write(buffer, 0, buffer.Length);
  34. }
  35. }
  36. }

  二、解压文件夹

  1.      /// <summary>
  2. /// 功能:解压zip格式的文件。
  3. /// </summary>
  4. /// <param name="zipFilePath">压缩文件路径</param>
  5. /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
  6. /// <returns>解压是否成功</returns>
  7. public static void UnZip(string zipFilePath, string unZipDir)
  8. {
  9. if (zipFilePath == string.Empty)
  10. {
  11. throw new Exception("压缩文件不能为空!");
  12. }
  13. if (!File.Exists(zipFilePath))
  14. {
  15. throw new FileNotFoundException("压缩文件不存在!");
  16. }
  17. //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
  18. if (unZipDir == string.Empty)
  19. unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
  20. if (!unZipDir.EndsWith("/"))
  21. unZipDir += "/";
  22. if (!Directory.Exists(unZipDir))
  23. Directory.CreateDirectory(unZipDir);
  24.  
  25. using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
  26. {
  27.  
  28. ZipEntry theEntry;
  29. while ((theEntry = s.GetNextEntry()) != null)
  30. {
  31. string directoryName = Path.GetDirectoryName(theEntry.Name);
  32. string fileName = Path.GetFileName(theEntry.Name);
  33. if (!string.IsNullOrEmpty(directoryName))
  34. {
  35. Directory.CreateDirectory(unZipDir + directoryName);
  36. }
  37. if (directoryName != null && !directoryName.EndsWith("/"))
  38. {
  39. }
  40. if (fileName != String.Empty)
  41. {
  42. using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
  43. {
  44.  
  45. int size;
  46. byte[] data = new byte[2048];
  47. while (true)
  48. {
  49. size = s.Read(data, 0, data.Length);
  50. if (size > 0)
  51. {
  52. streamWriter.Write(data, 0, size);
  53. }
  54. else
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }

  三、压缩单个文件

  1.      /// <summary>
  2. /// 压缩单个文件
  3. /// </summary>
  4. /// <param name="fileToZip">要进行压缩的文件名,全路径</param>
  5. /// <param name="zipedFile">压缩后生成的压缩文件名,全路径</param>
  6. public static void ZipFile(string fileToZip, string zipedFile)
  7. {
  8. // 如果文件没有找到,则报错
  9. if (!File.Exists(fileToZip))
  10. {
  11. throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
  12. }
  13. using (FileStream fileStream = File.OpenRead(fileToZip))
  14. {
  15. byte[] buffer = new byte[fileStream.Length];
  16. fileStream.Read(buffer, 0, buffer.Length);
  17. fileStream.Close();
  18. using (FileStream zipFile = File.Create(zipedFile))
  19. {
  20. using (ZipOutputStream zipOutputStream = new ZipOutputStream(zipFile))
  21. {
  22. // string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
  23. string fileName = Path.GetFileName(fileToZip);
  24. var zipEntry = new ZipEntry(fileName)
  25. {
  26. DateTime = DateTime.Now,
  27. IsUnicodeText = true
  28. };
  29. zipOutputStream.PutNextEntry(zipEntry);
  30. zipOutputStream.SetLevel(5);
  31. zipOutputStream.Write(buffer, 0, buffer.Length);
  32. zipOutputStream.Finish();
  33. zipOutputStream.Close();
  34. }
  35. }
  36. }
  37. }

  三、压缩单个文件

  1.      /// <summary>
  2. /// 压缩多个目录或文件
  3. /// </summary>
  4. /// <param name="folderOrFileList">待压缩的文件夹或者文件,全路径格式,是一个集合</param>
  5. /// <param name="zipedFile">压缩后的文件名,全路径格式</param>
  6. /// <param name="password">压宿密码</param>
  7. /// <returns></returns>
  8. public static bool ZipManyFilesOrDictorys(IEnumerable<string> folderOrFileList, string zipedFile, string password)
  9. {
  10. bool res = true;
  11. using (var s = new ZipOutputStream(File.Create(zipedFile)))
  12. {
  13. s.SetLevel(6);
  14. if (!string.IsNullOrEmpty(password))
  15. {
  16. s.Password = password;
  17. }
  18. foreach (string fileOrDir in folderOrFileList)
  19. {
  20. //是文件夹
  21. if (Directory.Exists(fileOrDir))
  22. {
  23. res = ZipFileDictory(fileOrDir, s, "");
  24. }
  25. else
  26. {
  27. //文件
  28. res = ZipFileWithStream(fileOrDir, s);
  29. }
  30. }
  31. s.Finish();
  32. s.Close();
  33. return res;
  34. }
  35. }

  五、递归压缩文件夹

  1.      /// <summary>
  2. /// 递归压缩文件夹方法
  3. /// </summary>
  4. /// <param name="folderToZip"></param>
  5. /// <param name="s"></param>
  6. /// <param name="parentFolderName"></param>
  7. private static bool ZipFileDictory(string folderToZip, ZipOutputStream s, string parentFolderName)
  8. {
  9. bool res = true;
  10. ZipEntry entry = null;
  11. FileStream fs = null;
  12. Crc32 crc = new Crc32();
  13. try
  14. {
  15. //创建当前文件夹
  16. entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
  17. s.PutNextEntry(entry);
  18. s.Flush();
  19. //先压缩文件,再递归压缩文件夹
  20. var filenames = Directory.GetFiles(folderToZip);
  21. foreach (string file in filenames)
  22. {
  23. //打开压缩文件
  24. fs = File.OpenRead(file);
  25. byte[] buffer = new byte[fs.Length];
  26. fs.Read(buffer, 0, buffer.Length);
  27. entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));
  28. entry.DateTime = DateTime.Now;
  29. entry.Size = fs.Length;
  30. fs.Close();
  31. crc.Reset();
  32. crc.Update(buffer);
  33. entry.Crc = crc.Value;
  34. s.PutNextEntry(entry);
  35. s.Write(buffer, 0, buffer.Length);
  36. }
  37. }
  38. catch
  39. {
  40. res = false;
  41. }
  42. finally
  43. {
  44. if (fs != null)
  45. {
  46. fs.Close();
  47. }
  48. if (entry != null)
  49. {
  50. }
  51. GC.Collect();
  52. GC.Collect(1);
  53. }
  54. var folders = Directory.GetDirectories(folderToZip);
  55. foreach (string folder in folders)
  56. {
  57. if (!ZipFileDictory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip))))
  58. {
  59. return false;
  60. }
  61. }
  62. return res;
  63. }

  利用ICSharpCode.SharpZipLib解压缩辅助类全部代码如下:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. namespace RDIFramework.Utilities
  7. {
  8. using ICSharpCode.SharpZipLib.Checksums;
  9. using ICSharpCode.SharpZipLib.Zip;
  10.  
  11. /// <summary>
  12. /// ZipHelper.cs
  13. /// Zip解压缩帮助类
  14. ///
  15. /// 修改纪录
  16. ///
  17. /// 2017-03-05 EricHu 创建。
  18. ///
  19. /// 版本:1.0
  20. ///
  21. /// <author>
  22. /// <name>EricHu</name>
  23. /// <date>2017-03-05</date>
  24. /// </author>
  25. /// </summary>
  26. public class ZipHelper
  27. {
  28. /// <summary>
  29. /// 压缩文件夹
  30. /// </summary>
  31. /// <param name="dirToZip"></param>
  32. /// <param name="zipedFileName"></param>
  33. /// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
  34. public static void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9)
  35. {
  36. if (Path.GetExtension(zipedFileName) != ".zip")
  37. {
  38. zipedFileName = zipedFileName + ".zip";
  39. }
  40. using (var zipoutputstream = new ZipOutputStream(File.Create(zipedFileName)))
  41. {
  42. zipoutputstream.SetLevel(compressionLevel);
  43. Crc32 crc = new Crc32();
  44. Hashtable fileList = GetAllFies(dirToZip);
  45. foreach (DictionaryEntry item in fileList)
  46. {
  47. FileStream fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  48. byte[] buffer = new byte[fs.Length];
  49. fs.Read(buffer, 0, buffer.Length);;
  50. ZipEntry entry = new ZipEntry(Path.GetFileName(item.Key.ToString()))
  51. {
  52. DateTime = (DateTime)item.Value,
  53. Size = fs.Length
  54. };
  55. fs.Close();
  56. crc.Reset();
  57. crc.Update(buffer);
  58. entry.Crc = crc.Value;
  59. zipoutputstream.PutNextEntry(entry);
  60. zipoutputstream.Write(buffer, 0, buffer.Length);
  61. }
  62. }
  63. }
  64.  
  65. /// <summary>
  66. /// 获取所有文件
  67. /// </summary>
  68. /// <returns></returns>
  69. public static Hashtable GetAllFies(string dir)
  70. {
  71. Hashtable filesList = new Hashtable();
  72. DirectoryInfo fileDire = new DirectoryInfo(dir);
  73. if (!fileDire.Exists)
  74. {
  75. throw new FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");
  76. }
  77.  
  78. GetAllDirFiles(fileDire, filesList);
  79. GetAllDirsFiles(fileDire.GetDirectories(), filesList);
  80. return filesList;
  81. }
  82.  
  83. /// <summary>
  84. /// 获取一个文件夹下的所有文件夹里的文件
  85. /// </summary>
  86. /// <param name="dirs"></param>
  87. /// <param name="filesList"></param>
  88. public static void GetAllDirsFiles(IEnumerable<DirectoryInfo> dirs, Hashtable filesList)
  89. {
  90. foreach (DirectoryInfo dir in dirs)
  91. {
  92. foreach (FileInfo file in dir.GetFiles("*.*"))
  93. {
  94. filesList.Add(file.FullName, file.LastWriteTime);
  95. }
  96. GetAllDirsFiles(dir.GetDirectories(), filesList);
  97. }
  98. }
  99.  
  100. /// <summary>
  101. /// 获取一个文件夹下的文件
  102. /// </summary>
  103. /// <param name="dir">目录名称</param>
  104. /// <param name="filesList">文件列表HastTable</param>
  105. public static void GetAllDirFiles(DirectoryInfo dir, Hashtable filesList)
  106. {
  107. foreach (FileInfo file in dir.GetFiles("*.*"))
  108. {
  109. filesList.Add(file.FullName, file.LastWriteTime);
  110. }
  111. }
  112.  
  113. /// <summary>
  114. /// 功能:解压zip格式的文件。
  115. /// </summary>
  116. /// <param name="zipFilePath">压缩文件路径</param>
  117. /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
  118. /// <returns>解压是否成功</returns>
  119. public static void UnZip(string zipFilePath, string unZipDir)
  120. {
  121. if (zipFilePath == string.Empty)
  122. {
  123. throw new Exception("压缩文件不能为空!");
  124. }
  125. if (!File.Exists(zipFilePath))
  126. {
  127. throw new FileNotFoundException("压缩文件不存在!");
  128. }
  129. //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
  130. if (unZipDir == string.Empty)
  131. unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
  132. if (!unZipDir.EndsWith("/"))
  133. unZipDir += "/";
  134. if (!Directory.Exists(unZipDir))
  135. Directory.CreateDirectory(unZipDir);
  136.  
  137. using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
  138. {
  139.  
  140. ZipEntry theEntry;
  141. while ((theEntry = s.GetNextEntry()) != null)
  142. {
  143. string directoryName = Path.GetDirectoryName(theEntry.Name);
  144. string fileName = Path.GetFileName(theEntry.Name);
  145. if (!string.IsNullOrEmpty(directoryName))
  146. {
  147. Directory.CreateDirectory(unZipDir + directoryName);
  148. }
  149. if (directoryName != null && !directoryName.EndsWith("/"))
  150. {
  151. }
  152. if (fileName != String.Empty)
  153. {
  154. using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
  155. {
  156.  
  157. int size;
  158. byte[] data = new byte[2048];
  159. while (true)
  160. {
  161. size = s.Read(data, 0, data.Length);
  162. if (size > 0)
  163. {
  164. streamWriter.Write(data, 0, size);
  165. }
  166. else
  167. {
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176.  
  177. /// <summary>
  178. /// 压缩单个文件
  179. /// </summary>
  180. /// <param name="filePath">被压缩的文件名称(包含文件路径),文件的全路径</param>
  181. /// <param name="zipedFileName">压缩后的文件名称(包含文件路径),保存的文件名称</param>
  182. /// <param name="compressionLevel">压缩率0(无压缩)到 9(压缩率最高)</param>
  183. public static void ZipFile(string filePath, string zipedFileName, int compressionLevel = 9)
  184. {
  185. // 如果文件没有找到,则报错
  186. if (!File.Exists(filePath))
  187. {
  188. throw new FileNotFoundException("文件:" + filePath + "没有找到!");
  189. }
  190. // 如果压缩后名字为空就默认使用源文件名称作为压缩文件名称
  191. if (string.IsNullOrEmpty(zipedFileName))
  192. {
  193. string oldValue = Path.GetFileName(filePath);
  194. if (oldValue != null)
  195. {
  196. zipedFileName = filePath.Replace(oldValue, "") + Path.GetFileNameWithoutExtension(filePath) + ".zip";
  197. }
  198. }
  199. // 如果压缩后的文件名称后缀名不是zip,就是加上zip,防止是一个乱码文件
  200. if (Path.GetExtension(zipedFileName) != ".zip")
  201. {
  202. zipedFileName = zipedFileName + ".zip";
  203. }
  204. // 如果指定位置目录不存在,创建该目录 C:\Users\yhl\Desktop\大汉三通
  205. string zipedDir = zipedFileName.Substring(0, zipedFileName.LastIndexOf("\\", StringComparison.Ordinal));
  206. if (!Directory.Exists(zipedDir))
  207. {
  208. Directory.CreateDirectory(zipedDir);
  209. }
  210. // 被压缩文件名称
  211. string filename = filePath.Substring(filePath.LastIndexOf("\\", StringComparison.Ordinal) + 1);
  212. var streamToZip = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  213. var zipFile = File.Create(zipedFileName);
  214. var zipStream = new ZipOutputStream(zipFile);
  215. var zipEntry = new ZipEntry(filename);
  216. zipStream.PutNextEntry(zipEntry);
  217. zipStream.SetLevel(compressionLevel);
  218. var buffer = new byte[2048];
  219. Int32 size = streamToZip.Read(buffer, 0, buffer.Length);
  220. zipStream.Write(buffer, 0, size);
  221. try
  222. {
  223. while (size < streamToZip.Length)
  224. {
  225. int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
  226. zipStream.Write(buffer, 0, sizeRead);
  227. size += sizeRead;
  228. }
  229. }
  230. finally
  231. {
  232. zipStream.Finish();
  233. zipStream.Close();
  234. streamToZip.Close();
  235. }
  236. }
  237.  
  238. /// <summary>
  239. /// 压缩单个文件
  240. /// </summary>
  241. /// <param name="fileToZip">要进行压缩的文件名,全路径</param>
  242. /// <param name="zipedFile">压缩后生成的压缩文件名,全路径</param>
  243. public static void ZipFile(string fileToZip, string zipedFile)
  244. {
  245. // 如果文件没有找到,则报错
  246. if (!File.Exists(fileToZip))
  247. {
  248. throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
  249. }
  250. using (FileStream fileStream = File.OpenRead(fileToZip))
  251. {
  252. byte[] buffer = new byte[fileStream.Length];
  253. fileStream.Read(buffer, 0, buffer.Length);
  254. fileStream.Close();
  255. using (FileStream zipFile = File.Create(zipedFile))
  256. {
  257. using (ZipOutputStream zipOutputStream = new ZipOutputStream(zipFile))
  258. {
  259. // string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
  260. string fileName = Path.GetFileName(fileToZip);
  261. var zipEntry = new ZipEntry(fileName)
  262. {
  263. DateTime = DateTime.Now,
  264. IsUnicodeText = true
  265. };
  266. zipOutputStream.PutNextEntry(zipEntry);
  267. zipOutputStream.SetLevel(5);
  268. zipOutputStream.Write(buffer, 0, buffer.Length);
  269. zipOutputStream.Finish();
  270. zipOutputStream.Close();
  271. }
  272. }
  273. }
  274. }
  275.  
  276. /// <summary>
  277. /// 压缩多个目录或文件
  278. /// </summary>
  279. /// <param name="folderOrFileList">待压缩的文件夹或者文件,全路径格式,是一个集合</param>
  280. /// <param name="zipedFile">压缩后的文件名,全路径格式</param>
  281. /// <param name="password">压宿密码</param>
  282. /// <returns></returns>
  283. public static bool ZipManyFilesOrDictorys(IEnumerable<string> folderOrFileList, string zipedFile, string password)
  284. {
  285. bool res = true;
  286. using (var s = new ZipOutputStream(File.Create(zipedFile)))
  287. {
  288. s.SetLevel(6);
  289. if (!string.IsNullOrEmpty(password))
  290. {
  291. s.Password = password;
  292. }
  293. foreach (string fileOrDir in folderOrFileList)
  294. {
  295. //是文件夹
  296. if (Directory.Exists(fileOrDir))
  297. {
  298. res = ZipFileDictory(fileOrDir, s, "");
  299. }
  300. else
  301. {
  302. //文件
  303. res = ZipFileWithStream(fileOrDir, s);
  304. }
  305. }
  306. s.Finish();
  307. s.Close();
  308. return res;
  309. }
  310. }
  311.  
  312. /// <summary>
  313. /// 带压缩流压缩单个文件
  314. /// </summary>
  315. /// <param name="fileToZip">要进行压缩的文件名</param>
  316. /// <param name="zipStream"></param>
  317. /// <returns></returns>
  318. private static bool ZipFileWithStream(string fileToZip, ZipOutputStream zipStream)
  319. {
  320. //如果文件没有找到,则报错
  321. if (!File.Exists(fileToZip))
  322. {
  323. throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
  324. }
  325. //FileStream fs = null;
  326. FileStream zipFile = null;
  327. ZipEntry zipEntry = null;
  328. bool res = true;
  329. try
  330. {
  331. zipFile = File.OpenRead(fileToZip);
  332. byte[] buffer = new byte[zipFile.Length];
  333. zipFile.Read(buffer, 0, buffer.Length);
  334. zipFile.Close();
  335. zipEntry = new ZipEntry(Path.GetFileName(fileToZip));
  336. zipStream.PutNextEntry(zipEntry);
  337. zipStream.Write(buffer, 0, buffer.Length);
  338. }
  339. catch
  340. {
  341. res = false;
  342. }
  343. finally
  344. {
  345. if (zipEntry != null)
  346. {
  347. }
  348.  
  349. if (zipFile != null)
  350. {
  351. zipFile.Close();
  352. }
  353. GC.Collect();
  354. GC.Collect(1);
  355. }
  356. return res;
  357.  
  358. }
  359.  
  360. /// <summary>
  361. /// 递归压缩文件夹方法
  362. /// </summary>
  363. /// <param name="folderToZip"></param>
  364. /// <param name="s"></param>
  365. /// <param name="parentFolderName"></param>
  366. private static bool ZipFileDictory(string folderToZip, ZipOutputStream s, string parentFolderName)
  367. {
  368. bool res = true;
  369. ZipEntry entry = null;
  370. FileStream fs = null;
  371. Crc32 crc = new Crc32();
  372. try
  373. {
  374. //创建当前文件夹
  375. entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
  376. s.PutNextEntry(entry);
  377. s.Flush();
  378. //先压缩文件,再递归压缩文件夹
  379. var filenames = Directory.GetFiles(folderToZip);
  380. foreach (string file in filenames)
  381. {
  382. //打开压缩文件
  383. fs = File.OpenRead(file);
  384. byte[] buffer = new byte[fs.Length];
  385. fs.Read(buffer, 0, buffer.Length);
  386. entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));
  387. entry.DateTime = DateTime.Now;
  388. entry.Size = fs.Length;
  389. fs.Close();
  390. crc.Reset();
  391. crc.Update(buffer);
  392. entry.Crc = crc.Value;
  393. s.PutNextEntry(entry);
  394. s.Write(buffer, 0, buffer.Length);
  395. }
  396. }
  397. catch
  398. {
  399. res = false;
  400. }
  401. finally
  402. {
  403. if (fs != null)
  404. {
  405. fs.Close();
  406. }
  407. if (entry != null)
  408. {
  409. }
  410. GC.Collect();
  411. GC.Collect(1);
  412. }
  413. var folders = Directory.GetDirectories(folderToZip);
  414. foreach (string folder in folders)
  415. {
  416. if (!ZipFileDictory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip))))
  417. {
  418. return false;
  419. }
  420. }
  421. return res;
  422. }
  423. }
  424. }

  

       欢迎关注RDIFramework.net框架官方公众微信微信号:guosisoft),及时了解最新动态。

       扫描二维码立即关注

RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper的更多相关文章

  1. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2版本正式发布

     RDIFramework.NET .NET快速信息化系统开发框架 V3.2版本 正式发布 精益求精求完美! 1.RDIFramework.NET框架介绍 RDIFramework.NET,基于.NE ...

  2. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增消息管理

    在V3.0版本的Web(Mvc.WebForm)与WinForm中我们新增了“消息管理”模块.“消息管理”模块是对框架的所有消息进行管理.通过左侧的消息分类可以查看所选分类的所有消息列表.在主界面上我 ...

  3. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增查询引擎管理

    欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 RDIFramework.NET — 基于.NET的快速信 ...

  4. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增系统参数管理

    欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 在V3.0版本的Web(Mvc.WebForm)与WinF ...

  5. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本新增序列管理

    欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 在V3.0版本的Web(Mvc.WebForm)与WinF ...

  6. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.3版本全新发布

    1.RDIFramework.NET框架介绍 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,为企业或个人快速开发系统提供了强大的支持,开发人员不需要开发系统的基础功能和 ...

  7. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->WinForm版本新增新的角色授权管理界面效率更高、更规范

    角色授权管理模块主要是对角色的相应权限进行集中设置.在角色权限管理模块中,管理员可以添加或移除指定角色所包含的用户.可以分配或授予指定角色的模块(菜单)的访问权限.可以收回或分配指定角色的操作(功能) ...

  8. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->Web版本新增新的角色授权管理界面效率更高、更规范

    角色授权管理模块主要是对角色的相应权限进行集中设置.在角色权限管理模块中,管理员可以添加或移除指定角色所包含的用户.可以分配或授予指定角色的模块(菜单)的访问权限.可以收回或分配指定角色的操作(功能) ...

  9. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->WinForm版本重构岗位授权管理界面更规范、高效与美观

    岗位(职位)管理模块主要是针对组织机构的岗位(职位)进行管理,包括:增加.修改.删除.移动.对岗位设置用户,设置岗位的权限等.岗位管理在企业应用中是一个普遍应用的模块,也属于其他业务应用的基础.合理的 ...

随机推荐

  1. Mysql更新关联子查询报错

    报错内容:sql  1093 - You can't specify target table 'u' for update in FROM clause 错误原因: if you're doing ...

  2. Hive参数的临时设置和永久性设置

    Hive中有一些参数是系统给提供给用户的,我们可以通过这些参数的设置可以让Hive在不同的模式下工作,或者改变显示的效果. 1.通过set对参数值进行设定,这种设置只能是在本次会话有效,退出Hive就 ...

  3. vue 实现图片上传与预览,以及清除图片

    vue写原生的上传图片并预览以及清除图片的效果,下面是demo,其中里面有vue获取input框value值的方法以及vue中函数之间的调用 <!DOCTYPE html> <htm ...

  4. Java 将键盘中的输入保存到数组

    import java.util.Scanner; import java.util.InputMismatchException; public class saveInputToArr { pub ...

  5. CSS3 神器总结

    1. 选择类 1.1 /* 鼠标选中区域,改变背景/字体颜色 */ /*遍历写法*/ div::selection { background-color: red; color: #fff; /* f ...

  6. Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    提示哪个引用修改哪个引用的属性: Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, ...

  7. php基础-cookie&session

    设置cookie //设置cookie setcookie('key', 'value', time() + 60, '/'); 设置session //必须开启session session_sta ...

  8. hadoop环境搭建及Wordcount案例实验

    1.Linux环境变量设置 在/etc/profile中添加环境变量 sudo vim /etc/profile PATH=$PATH:/usr/local/hadoop/bin source /et ...

  9. 818C.soft thief

    Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has ...

  10. Socket看法

    Socket通常也称做”套接字“,用于描述IP地址和端口,废话不多说,它就是网络通信过程中端点的抽象表示. Socket又称"套接字",应用程序通常通过"套接字" ...