ZipHelper.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSharpCode.SharpZipLib.Zip;
  6. using System.IO;
  7. namespace UAS_MES.PublicMethod
  8. {
  9. class ZipHelper
  10. {
  11. /// <summary>
  12. /// 用于解压缩Zip文件
  13. /// </summary>
  14. /// <param name="ZipFilePath"></param>
  15. /// <param name="UnZipPath"></param>
  16. public static void UnZip(string ZipFilePath, string UnZipPath)
  17. {
  18. if (!File.Exists(ZipFilePath))
  19. {
  20. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  21. }
  22. if (!Directory.Exists(UnZipPath))
  23. {
  24. Directory.CreateDirectory(UnZipPath);
  25. }
  26. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  27. {
  28. ZipEntry theEntry;
  29. while ((theEntry = s.GetNextEntry()) != null)
  30. {
  31. if (theEntry.IsDirectory)
  32. {
  33. continue;
  34. }
  35. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  36. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  37. if (!Directory.Exists(directorName))
  38. {
  39. Directory.CreateDirectory(directorName);
  40. }
  41. if (!String.IsNullOrEmpty(fileName))
  42. {
  43. using (FileStream streamWriter = File.Create(fileName))
  44. {
  45. int size = 4096;
  46. byte[] data = new byte[size];
  47. while (size > 0)
  48. {
  49. size = s.Read(data, 0, data.Length);
  50. streamWriter.Write(data, 0, size);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }