ZipOperate.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //using ICSharpCode.SharpZipLib.Checksums;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BaseLibRWFile
  10. {
  11. /// <summary>
  12. /// 压缩类
  13. /// </summary>
  14. public class ZipOperate
  15. {
  16. /// <summary>
  17. /// 压缩文件
  18. /// </summary>
  19. /// <param name="strFile"></param>
  20. /// <param name="strZip"></param>
  21. public void ZipFile(string strFile, string strZip)
  22. {
  23. var len = strFile.Length;
  24. var strlen = strFile[len - 1];
  25. if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
  26. {
  27. strFile += Path.DirectorySeparatorChar;
  28. }
  29. ZipOutputStream outstream = new ZipOutputStream(File.Create(strZip));
  30. outstream.SetLevel(6);
  31. Zip(strFile, outstream, strFile);
  32. outstream.Finish();
  33. outstream.Close();
  34. }
  35. public void Zip(string strFile, ZipOutputStream outstream, string staticFile)
  36. {
  37. //if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
  38. //{
  39. // strFile += Path.DirectorySeparatorChar;
  40. //}
  41. //Crc32 crc = new Crc32();
  42. ////获取指定目录下所有文件和子目录文件名称
  43. //string[] filenames = Directory.GetFileSystemEntries(strFile);
  44. ////遍历文件
  45. //foreach (string file in filenames)
  46. //{
  47. // if (Directory.Exists(file))
  48. // {
  49. // Zip(file, outstream, staticFile);
  50. // }
  51. // //否则,直接压缩文件
  52. // else
  53. // {
  54. // //打开文件
  55. // FileStream fs = File.OpenRead(file);
  56. // //定义缓存区对象
  57. // byte[] buffer = new byte[fs.Length];
  58. // //通过字符流,读取文件
  59. // fs.Read(buffer, 0, buffer.Length);
  60. // //得到目录下的文件(比如:D:\Debug1\test),test
  61. // string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
  62. // ZipEntry entry = new ZipEntry(tempfile);
  63. // entry.DateTime = DateTime.Now;
  64. // entry.Size = fs.Length;
  65. // fs.Close();
  66. // crc.Reset();
  67. // crc.Update(buffer);
  68. // entry.Crc = crc.Value;
  69. // outstream.PutNextEntry(entry);
  70. // //写文件
  71. // outstream.Write(buffer, 0, buffer.Length);
  72. // }
  73. //}
  74. }
  75. }
  76. /// <summary>
  77. /// 解压方法
  78. /// </summary>
  79. public class UnZipFloClass
  80. {
  81. public string UnZipFile(string TargetFile, string fileDir, ref string msg)
  82. {
  83. string rootFile = "";
  84. msg = "";
  85. try
  86. {
  87. //读取压缩文件(zip文件),准备解压缩
  88. ZipInputStream inputstream = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
  89. ZipEntry entry;
  90. string path = fileDir;
  91. //解压出来的文件保存路径
  92. string rootDir = "";
  93. //根目录下的第一个子文件夹的名称
  94. while ((entry = inputstream.GetNextEntry()) != null)
  95. {
  96. rootDir = Path.GetDirectoryName(entry.Name);
  97. //得到根目录下的第一级子文件夹的名称
  98. if (rootDir.IndexOf("\\") >= 0)
  99. {
  100. rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
  101. }
  102. string dir = Path.GetDirectoryName(entry.Name);
  103. //得到根目录下的第一级子文件夹下的子文件夹名称
  104. string fileName = Path.GetFileName(entry.Name);
  105. //根目录下的文件名称
  106. if (dir != "")
  107. {
  108. //创建根目录下的子文件夹,不限制级别
  109. if (!Directory.Exists(fileDir + "\\" + dir))
  110. {
  111. path = fileDir + "\\" + dir;
  112. //在指定的路径创建文件夹
  113. Directory.CreateDirectory(path);
  114. }
  115. }
  116. else if (dir == "" && fileName != "")
  117. {
  118. //根目录下的文件
  119. path = fileDir;
  120. rootFile = fileName;
  121. }
  122. else if (dir != "" && fileName != "")
  123. {
  124. //根目录下的第一级子文件夹下的文件
  125. if (dir.IndexOf("\\") > 0)
  126. {
  127. //指定文件保存路径
  128. path = fileDir + "\\" + dir;
  129. }
  130. }
  131. if (dir == rootDir)
  132. {
  133. //判断是不是需要保存在根目录下的文件
  134. path = fileDir + "\\" + rootDir;
  135. }
  136. //以下为解压zip文件的基本步骤
  137. //基本思路:遍历压缩文件里的所有文件,创建一个相同的文件
  138. if (fileName != String.Empty)
  139. {
  140. FileStream fs = File.Create(path + "\\" + fileName);
  141. int size = 2048;
  142. byte[] data = new byte[2048];
  143. while (true)
  144. {
  145. size = inputstream.Read(data, 0, data.Length);
  146. if (size > 0)
  147. {
  148. fs.Write(data, 0, size);
  149. }
  150. else
  151. {
  152. break;
  153. }
  154. }
  155. fs.Close();
  156. }
  157. }
  158. inputstream.Close();
  159. msg = "解压成功!";
  160. return rootFile;
  161. }
  162. catch (Exception ex)
  163. {
  164. msg = "解压失败,原因:" + ex.Message;
  165. return "1;" + ex.Message;
  166. }
  167. }
  168. }
  169. //压缩文件
  170. //private void btnZipFlo_Click(object sender, EventArgs e)
  171. // {
  172. // string[] strs = new string[2];
  173. // //待压缩文件目录
  174. // strs[0] = "D:\\DeBug1\\";
  175. // //压缩后的目标文件
  176. // strs[1] = "D:\\Debug2\\FrpTest.zip";
  177. // ZipFloClass zc = new ZipFloClass();
  178. // zc.ZipFile(strs[0], strs[1]);
  179. // }
  180. /// <summary>
  181. /// 批量解压事件
  182. /// </summary>
  183. /// <param name="sender"></param>
  184. /// <param name="e"></param>
  185. //private void btnBatchUnZipFlo_Click(object sender, EventArgs e)
  186. //{
  187. // string msg = "";
  188. // string path2 = "D:\\Debug2\\";
  189. // string path3 = "D:\\Debug3\\";
  190. // //获取指定目录下所有文件和子文件名称(所有待解压的压缩文件)
  191. // string[] files = Directory.GetFileSystemEntries(path2);
  192. // UnZipFloClass uzc = new UnZipFloClass();
  193. // //遍历所有压缩文件路径
  194. // foreach (string file in files)
  195. // {
  196. // //获取压缩包名称(包括后缀名)
  197. // var filename = file.Substring(file.LastIndexOf("\\") + 1);
  198. // //得到压缩包名称(没有后缀)
  199. // filename = filename.Substring(0, filename.LastIndexOf("."));
  200. // //判断解压的路径是否存在
  201. // if (!Directory.Exists(path3 + filename))
  202. // {
  203. // //没有,则创建这个路径
  204. // Directory.CreateDirectory(path3 + filename);
  205. // }
  206. // //调用解压方法(参数1:待解压的压缩文件路径(带后缀名),参数2:解压后存放的文件路径,参数3:返工是否解压成功)
  207. // uzc.unZipFile(file, path3 + filename, ref msg);
  208. // }
  209. // MessageBox.Show("批量解压成功");
  210. //}
  211. }