ZipUtility.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using ICSharpCode.SharpZipLib.Zip;
  2. using System.Collections;
  3. using System.IO;
  4. namespace ProjectBase.Data.Zip
  5. {
  6. public static class ZipUtility
  7. {
  8. /// <summary>
  9. /// 压缩文件中的文件,可设置密码
  10. /// </summary>
  11. /// <param name="inputFolderPath">输入的文件夹</param>
  12. /// <param name="outputPathAndFile">输出的压缩文件全名</param>
  13. /// <param name="password">压缩密码</param>
  14. public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
  15. {
  16. ArrayList ar = GenerateFileList(inputFolderPath);
  17. int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
  18. // find number of chars to remove // from orginal file path
  19. TrimLength += 1; //remove '\'
  20. FileStream ostream;
  21. byte[] obuffer;
  22. string outPath =/* inputFolderPath + @"\" + */outputPathAndFile;
  23. if (!Directory.Exists(Path.GetDirectoryName(outPath)))
  24. {
  25. Directory.CreateDirectory(Path.GetDirectoryName(outPath));
  26. }
  27. if (File.Exists(outPath))
  28. {
  29. return;
  30. }
  31. ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath));
  32. if (!string.IsNullOrEmpty(password))
  33. {
  34. oZipStream.Password = password;
  35. }
  36. oZipStream.SetLevel(9); // 设置最大压缩率
  37. ZipEntry oZipEntry;
  38. foreach (string Fil in ar)
  39. {
  40. oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
  41. oZipStream.PutNextEntry(oZipEntry);
  42. if (!Fil.EndsWith(@"/")) // 如果文件以 '/' 结束,则是目录
  43. {
  44. ostream = File.OpenRead(Fil);
  45. obuffer = new byte[ostream.Length];
  46. ostream.Read(obuffer, 0, obuffer.Length);
  47. oZipStream.Write(obuffer, 0, obuffer.Length);
  48. }
  49. }
  50. oZipStream.Finish();
  51. oZipStream.Close();
  52. }
  53. /// <summary>
  54. /// 根据文件夹生成文件列表
  55. /// </summary>
  56. /// <param name="Dir"></param>
  57. /// <returns></returns>
  58. private static ArrayList GenerateFileList(string Dir)
  59. {
  60. ArrayList fils = new ArrayList();
  61. bool Empty = true;
  62. foreach (string file in Directory.GetFiles(Dir))
  63. {
  64. fils.Add(file);
  65. Empty = false;
  66. }
  67. if (Empty)
  68. {
  69. //加入完全为空的目录
  70. if (Directory.GetDirectories(Dir).Length == 0)
  71. {
  72. fils.Add(Dir + @"/");
  73. }
  74. }
  75. foreach (string dirs in Directory.GetDirectories(Dir)) // 递归目录
  76. {
  77. foreach (object obj in GenerateFileList(dirs))
  78. {
  79. fils.Add(obj);
  80. }
  81. }
  82. return fils;
  83. }
  84. /// <summary>
  85. /// 解压文件到指定的目录,可设置密码、删除原文件等
  86. /// </summary>
  87. /// <param name="zipPathAndFile">压缩文件全名</param>
  88. /// <param name="outputFolder">解压输出文件目录</param>
  89. /// <param name="password">解压密码</param>
  90. /// <param name="deleteZipFile">是否删除原文件(压缩文件)</param>
  91. public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
  92. {
  93. using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile)))
  94. {
  95. if (password != null && password != string.Empty)
  96. {
  97. s.Password = password;
  98. }
  99. ZipEntry theEntry;
  100. string tmpEntry = string.Empty;
  101. while ((theEntry = s.GetNextEntry()) != null)
  102. {
  103. #region 遍历每个Entry对象进行解压处理
  104. string directoryName = outputFolder;
  105. string fileName = Path.GetFileName(theEntry.Name);
  106. if (directoryName != "")
  107. {
  108. Directory.CreateDirectory(directoryName);
  109. }
  110. if (fileName != string.Empty)
  111. {
  112. if (theEntry.Name.IndexOf(".ini") < 0)
  113. {
  114. string fullPath = directoryName + "\\" + theEntry.Name;
  115. fullPath = fullPath.Replace("\\ ", "\\");
  116. string fullDirPath = Path.GetDirectoryName(fullPath);
  117. if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
  118. using (FileStream streamWriter = File.Create(fullPath))
  119. {
  120. #region 写入文件流
  121. int size = 2048;
  122. byte[] data = new byte[2048];
  123. while (true)
  124. {
  125. size = s.Read(data, 0, data.Length);
  126. if (size > 0)
  127. {
  128. streamWriter.Write(data, 0, size);
  129. }
  130. else
  131. {
  132. break;
  133. }
  134. }
  135. #endregion
  136. }
  137. }
  138. }
  139. #endregion
  140. }
  141. }
  142. if (deleteZipFile)
  143. {
  144. File.Delete(zipPathAndFile);
  145. }
  146. }
  147. }
  148. }