SharpZip.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using Microsoft.Win32;
  5. using ICSharpCode.SharpZipLib.Checksums;
  6. using ICSharpCode.SharpZipLib.Zip;
  7. ///压缩、解压缩类
  8. namespace Ant.Service.Utilities
  9. {
  10. public class SharpZip
  11. {
  12. public SharpZip()
  13. { }
  14. /// <summary>
  15. /// 压缩
  16. /// </summary>
  17. /// <param name="filename"> 压缩后的文件名(包含物理路径)</param>
  18. /// <param name="directory">待压缩的文件夹(包含物理路径)</param>
  19. public static void PackFiles(string filename, string directory)
  20. {
  21. try
  22. {
  23. FastZip fz = new FastZip();
  24. fz.CreateEmptyDirectories = true;
  25. fz.CreateZip(filename, directory, true, "");
  26. fz = null;
  27. }
  28. catch (Exception)
  29. {
  30. throw;
  31. }
  32. }
  33. /// <summary>
  34. /// 解压缩
  35. /// </summary>
  36. /// <param name="file">待解压文件名(包含物理路径)</param>
  37. /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param>
  38. public static bool UnpackFiles(string file, string dir)
  39. {
  40. try
  41. {
  42. if (!Directory.Exists(dir))
  43. {
  44. Directory.CreateDirectory(dir);
  45. }
  46. ZipInputStream s = new ZipInputStream(File.OpenRead(file));
  47. ZipEntry theEntry;
  48. while ((theEntry = s.GetNextEntry()) != null)
  49. {
  50. string directoryName = Path.GetDirectoryName(theEntry.Name);
  51. string fileName = Path.GetFileName(theEntry.Name);
  52. if (directoryName != String.Empty)
  53. {
  54. Directory.CreateDirectory(dir + directoryName);
  55. }
  56. if (fileName != String.Empty)
  57. {
  58. FileStream streamWriter = File.Create(dir + theEntry.Name);
  59. int size = 2048;
  60. byte[] data = new byte[2048];
  61. while (true)
  62. {
  63. size = s.Read(data, 0, data.Length);
  64. if (size > 0)
  65. {
  66. streamWriter.Write(data, 0, size);
  67. }
  68. else
  69. {
  70. break;
  71. }
  72. }
  73. streamWriter.Close();
  74. }
  75. }
  76. s.Close();
  77. return true;
  78. }
  79. catch (Exception)
  80. {
  81. throw;
  82. }
  83. }
  84. }
  85. public class ClassZip
  86. {
  87. #region 私有方法
  88. /// <summary>
  89. /// 递归压缩文件夹方法
  90. /// </summary>
  91. private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
  92. {
  93. bool res = true;
  94. string[] folders, filenames;
  95. ZipEntry entry = null;
  96. FileStream fs = null;
  97. Crc32 crc = new Crc32();
  98. try
  99. {
  100. entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
  101. s.PutNextEntry(entry);
  102. s.Flush();
  103. filenames = Directory.GetFiles(FolderToZip);
  104. foreach (string file in filenames)
  105. {
  106. fs = File.OpenRead(file);
  107. byte[] buffer = new byte[fs.Length];
  108. fs.Read(buffer, 0, buffer.Length);
  109. entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
  110. entry.DateTime = DateTime.Now;
  111. entry.Size = fs.Length;
  112. fs.Close();
  113. crc.Reset();
  114. crc.Update(buffer);
  115. entry.Crc = crc.Value;
  116. s.PutNextEntry(entry);
  117. s.Write(buffer, 0, buffer.Length);
  118. }
  119. }
  120. catch
  121. {
  122. res = false;
  123. }
  124. finally
  125. {
  126. if (fs != null)
  127. {
  128. fs.Close();
  129. fs = null;
  130. }
  131. if (entry != null)
  132. {
  133. entry = null;
  134. }
  135. GC.Collect();
  136. GC.Collect(1);
  137. }
  138. folders = Directory.GetDirectories(FolderToZip);
  139. foreach (string folder in folders)
  140. {
  141. if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
  142. {
  143. return false;
  144. }
  145. }
  146. return res;
  147. }
  148. /// <summary>
  149. /// 压缩目录
  150. /// </summary>
  151. /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
  152. /// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
  153. private static bool ZipFileDictory(string FolderToZip, string ZipedFile, int level)
  154. {
  155. bool res;
  156. if (!Directory.Exists(FolderToZip))
  157. {
  158. return false;
  159. }
  160. ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
  161. s.SetLevel(level);
  162. res = ZipFileDictory(FolderToZip, s, "");
  163. s.Finish();
  164. s.Close();
  165. return res;
  166. }
  167. /// <summary>
  168. /// 压缩文件
  169. /// </summary>
  170. /// <param name="FileToZip">要进行压缩的文件名</param>
  171. /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
  172. private static bool ZipFile(string FileToZip, string ZipedFile, int level)
  173. {
  174. if (!File.Exists(FileToZip))
  175. {
  176. throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
  177. }
  178. FileStream ZipFile = null;
  179. ZipOutputStream ZipStream = null;
  180. ZipEntry ZipEntry = null;
  181. bool res = true;
  182. try
  183. {
  184. ZipFile = File.OpenRead(FileToZip);
  185. byte[] buffer = new byte[ZipFile.Length];
  186. ZipFile.Read(buffer, 0, buffer.Length);
  187. ZipFile.Close();
  188. ZipFile = File.Create(ZipedFile);
  189. ZipStream = new ZipOutputStream(ZipFile);
  190. ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
  191. ZipStream.PutNextEntry(ZipEntry);
  192. ZipStream.SetLevel(level);
  193. ZipStream.Write(buffer, 0, buffer.Length);
  194. }
  195. catch
  196. {
  197. res = false;
  198. }
  199. finally
  200. {
  201. if (ZipEntry != null)
  202. {
  203. ZipEntry = null;
  204. }
  205. if (ZipStream != null)
  206. {
  207. ZipStream.Finish();
  208. ZipStream.Close();
  209. }
  210. if (ZipFile != null)
  211. {
  212. ZipFile.Close();
  213. ZipFile = null;
  214. }
  215. GC.Collect();
  216. GC.Collect(1);
  217. }
  218. return res;
  219. }
  220. #endregion
  221. /// <summary>
  222. /// 压缩
  223. /// </summary>
  224. /// <param name="FileToZip">待压缩的文件目录</param>
  225. /// <param name="ZipedFile">生成的目标文件</param>
  226. /// <param name="level">6</param>
  227. public static bool Zip(String FileToZip, String ZipedFile, int level)
  228. {
  229. if (Directory.Exists(FileToZip))
  230. {
  231. return ZipFileDictory(FileToZip, ZipedFile, level);
  232. }
  233. else if (File.Exists(FileToZip))
  234. {
  235. return ZipFile(FileToZip, ZipedFile, level);
  236. }
  237. else
  238. {
  239. return false;
  240. }
  241. }
  242. /// <summary>
  243. /// 解压
  244. /// </summary>
  245. /// <param name="FileToUpZip">待解压的文件</param>
  246. /// <param name="ZipedFolder">解压目标存放目录</param>
  247. public static void UnZip(string FileToUpZip, string ZipedFolder)
  248. {
  249. if (!File.Exists(FileToUpZip))
  250. {
  251. return;
  252. }
  253. if (!Directory.Exists(ZipedFolder))
  254. {
  255. Directory.CreateDirectory(ZipedFolder);
  256. }
  257. ZipInputStream s = null;
  258. ZipEntry theEntry = null;
  259. string fileName;
  260. FileStream streamWriter = null;
  261. try
  262. {
  263. s = new ZipInputStream(File.OpenRead(FileToUpZip));
  264. while ((theEntry = s.GetNextEntry()) != null)
  265. {
  266. if (theEntry.Name != String.Empty)
  267. {
  268. fileName = Path.Combine(ZipedFolder, theEntry.Name);
  269. if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  270. {
  271. Directory.CreateDirectory(fileName);
  272. continue;
  273. }
  274. streamWriter = File.Create(fileName);
  275. int size = 2048;
  276. byte[] data = new byte[2048];
  277. while (true)
  278. {
  279. size = s.Read(data, 0, data.Length);
  280. if (size > 0)
  281. {
  282. streamWriter.Write(data, 0, size);
  283. }
  284. else
  285. {
  286. break;
  287. }
  288. }
  289. }
  290. }
  291. }
  292. finally
  293. {
  294. if (streamWriter != null)
  295. {
  296. streamWriter.Close();
  297. streamWriter = null;
  298. }
  299. if (theEntry != null)
  300. {
  301. theEntry = null;
  302. }
  303. if (s != null)
  304. {
  305. s.Close();
  306. s = null;
  307. }
  308. GC.Collect();
  309. GC.Collect(1);
  310. }
  311. }
  312. }
  313. public class ZipHelper
  314. {
  315. #region 私有变量
  316. String the_rar;
  317. RegistryKey the_Reg;
  318. Object the_Obj;
  319. String the_Info;
  320. ProcessStartInfo the_StartInfo;
  321. Process the_Process;
  322. #endregion
  323. /// <summary>
  324. /// 压缩
  325. /// </summary>
  326. /// <param name="zipname">要解压的文件名</param>
  327. /// <param name="zippath">要压缩的文件目录</param>
  328. /// <param name="dirpath">初始目录</param>
  329. public void EnZip(string zipname, string zippath, string dirpath)
  330. {
  331. try
  332. {
  333. the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
  334. the_Obj = the_Reg.GetValue("");
  335. the_rar = the_Obj.ToString();
  336. the_Reg.Close();
  337. the_rar = the_rar.Substring(1, the_rar.Length - 7);
  338. the_Info = " a " + zipname + " " + zippath;
  339. the_StartInfo = new ProcessStartInfo();
  340. the_StartInfo.FileName = the_rar;
  341. the_StartInfo.Arguments = the_Info;
  342. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  343. the_StartInfo.WorkingDirectory = dirpath;
  344. the_Process = new Process();
  345. the_Process.StartInfo = the_StartInfo;
  346. the_Process.Start();
  347. }
  348. catch (Exception ex)
  349. {
  350. throw new Exception(ex.Message);
  351. }
  352. }
  353. /// <summary>
  354. /// 解压缩
  355. /// </summary>
  356. /// <param name="zipname">要解压的文件名</param>
  357. /// <param name="zippath">要解压的文件路径</param>
  358. public void DeZip(string zipname, string zippath)
  359. {
  360. try
  361. {
  362. the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
  363. the_Obj = the_Reg.GetValue("");
  364. the_rar = the_Obj.ToString();
  365. the_Reg.Close();
  366. the_rar = the_rar.Substring(1, the_rar.Length - 7);
  367. the_Info = " X " + zipname + " " + zippath;
  368. the_StartInfo = new ProcessStartInfo();
  369. the_StartInfo.FileName = the_rar;
  370. the_StartInfo.Arguments = the_Info;
  371. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  372. the_Process = new Process();
  373. the_Process.StartInfo = the_StartInfo;
  374. the_Process.Start();
  375. }
  376. catch (Exception ex)
  377. {
  378. throw new Exception(ex.Message);
  379. }
  380. }
  381. }
  382. }