ZipManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using ICSharpCode.SharpZipLib.Zip;
  7. using ICSharpCode.SharpZipLib.Checksums;
  8. namespace HXX.Scanner.Common
  9. {
  10. /// <summary>
  11. /// 压缩解压zip类
  12. /// </summary>
  13. public class ZipManager
  14. {
  15. /// 递归压缩文件夹方法
  16. public static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
  17. {
  18. if (!Directory.Exists(FolderToZip))
  19. {
  20. return false;
  21. }
  22. bool res = true;
  23. string[] folders, filenames;
  24. ZipEntry entry = null;
  25. FileStream fs = null;
  26. Crc32 crc = new Crc32();
  27. try
  28. {
  29. //创建当前文件夹
  30. entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
  31. s.PutNextEntry(entry);
  32. s.Flush();
  33. //先压缩文件,再递归压缩文件夹
  34. filenames = Directory.GetFiles(FolderToZip);
  35. foreach (string file in filenames)
  36. {
  37. //打开压缩文件
  38. fs = File.OpenRead(file);
  39. byte[] buffer = new byte[fs.Length];
  40. fs.Read(buffer, 0, buffer.Length);
  41. string targetFileName = Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file);
  42. if (targetFileName.StartsWith("/"))
  43. {
  44. targetFileName = targetFileName.Substring(1);
  45. }
  46. entry = new ZipEntry(Path.Combine(ParentFolderName, targetFileName));
  47. entry.DateTime = DateTime.Now;
  48. entry.Size = fs.Length;
  49. fs.Close();
  50. crc.Reset();
  51. crc.Update(buffer);
  52. entry.Crc = crc.Value;
  53. s.PutNextEntry(entry);
  54. s.Write(buffer, 0, buffer.Length);
  55. }
  56. }
  57. catch (Exception ee)
  58. {
  59. res = false;
  60. }
  61. finally
  62. {
  63. if (fs != null)
  64. {
  65. fs.Close();
  66. fs = null;
  67. }
  68. if (entry != null)
  69. {
  70. entry = null;
  71. }
  72. }
  73. folders = Directory.GetDirectories(FolderToZip);
  74. foreach (string folder in folders)
  75. {
  76. if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
  77. {
  78. return false;
  79. }
  80. }
  81. return res;
  82. }
  83. /// <summary>
  84. /// 压缩目录
  85. /// </summary>
  86. /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
  87. /// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
  88. /// <param name="Password"></param>
  89. /// <returns></returns>
  90. public static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
  91. {
  92. bool res;
  93. if (!Directory.Exists(FolderToZip))
  94. {
  95. return false;
  96. }
  97. ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
  98. s.SetLevel(6);
  99. s.Password = Password;
  100. res = ZipFileDictory(FolderToZip, s, "");
  101. s.Finish();
  102. s.Close();
  103. return res;
  104. }
  105. /// <summary>
  106. /// 压缩文件
  107. /// </summary>
  108. /// <param name="FileToZip">要进行压缩的文件名</param>
  109. /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
  110. /// <param name="Password"></param>
  111. /// <returns></returns>
  112. public static bool ZipFile(string FileToZip, string ZipedFile, String Password)
  113. {
  114. //如果文件没有找到,则报错
  115. if (!File.Exists(FileToZip))
  116. {
  117. throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
  118. }
  119. //FileStream fs = null;
  120. FileStream ZipFile = null;
  121. ZipOutputStream ZipStream = null;
  122. ZipEntry ZipEntry = null;
  123. bool res = true;
  124. try
  125. {
  126. ZipFile = File.OpenRead(FileToZip);
  127. byte[] buffer = new byte[ZipFile.Length];
  128. ZipFile.Read(buffer, 0, buffer.Length);
  129. ZipFile.Close();
  130. ZipFile = File.Create(ZipedFile);
  131. ZipStream = new ZipOutputStream(ZipFile);
  132. ZipStream.Password = Password;
  133. ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
  134. ZipStream.PutNextEntry(ZipEntry);
  135. ZipStream.SetLevel(6);
  136. ZipStream.Write(buffer, 0, buffer.Length);
  137. }
  138. catch
  139. {
  140. res = false;
  141. }
  142. finally
  143. {
  144. if (ZipEntry != null)
  145. {
  146. ZipEntry = null;
  147. }
  148. if (ZipStream != null)
  149. {
  150. ZipStream.Finish();
  151. ZipStream.Close();
  152. }
  153. if (ZipFile != null)
  154. {
  155. ZipFile.Close();
  156. ZipFile = null;
  157. }
  158. GC.Collect();
  159. GC.Collect(1);
  160. }
  161. return res;
  162. }
  163. /// <summary>
  164. /// 压缩文件 和 文件夹
  165. /// </summary>
  166. /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
  167. /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
  168. /// <param name="Password"></param>
  169. /// <returns></returns>
  170. public static bool Zip(String FileToZip, String ZipedFile, String Password)
  171. {
  172. if (Directory.Exists(FileToZip))
  173. {
  174. return ZipFileDictory(FileToZip, ZipedFile, Password);
  175. }
  176. else if (File.Exists(FileToZip))
  177. {
  178. return ZipFile(FileToZip, ZipedFile, Password);
  179. }
  180. else
  181. {
  182. return false;
  183. }
  184. }
  185. /// <summary>
  186. /// 解压功能(解压压缩文件到指定目录)
  187. /// </summary>
  188. /// <param name="FileToUpZip">待解压的文件</param>
  189. /// <param name="ZipedFolder">指定解压目标目录</param>
  190. /// <param name="Password"></param>
  191. public static void UnZip(string FileToUpZip, string ZipedFolder, string Password)
  192. {
  193. if (!File.Exists(FileToUpZip))
  194. {
  195. return;
  196. }
  197. if (!Directory.Exists(ZipedFolder))
  198. {
  199. Directory.CreateDirectory(ZipedFolder);
  200. }
  201. ZipInputStream s = null;
  202. ZipEntry theEntry = null;
  203. string fileName;
  204. FileStream streamWriter = null;
  205. try
  206. {
  207. s = new ZipInputStream(File.OpenRead(FileToUpZip));
  208. s.Password = Password;
  209. while ((theEntry = s.GetNextEntry()) != null)
  210. {
  211. if (theEntry.Name != String.Empty)
  212. {
  213. fileName = Path.Combine(ZipedFolder, theEntry.Name);
  214. /**/
  215. ///判断文件路径是否是文件夹
  216. if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  217. {
  218. Directory.CreateDirectory(fileName);
  219. continue;
  220. }
  221. streamWriter = File.Create(fileName);
  222. int size = 2048;
  223. byte[] data = new byte[2048];
  224. while (true)
  225. {
  226. size = s.Read(data, 0, data.Length);
  227. if (size > 0)
  228. {
  229. streamWriter.Write(data, 0, size);
  230. }
  231. else
  232. {
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. finally
  240. {
  241. if (streamWriter != null)
  242. {
  243. streamWriter.Close();
  244. streamWriter = null;
  245. }
  246. if (theEntry != null)
  247. {
  248. theEntry = null;
  249. }
  250. if (s != null)
  251. {
  252. s.Close();
  253. s = null;
  254. }
  255. GC.Collect();
  256. GC.Collect(1);
  257. }
  258. }
  259. ///// <summary>
  260. ///// 解压功能(解压压缩文件到指定目录)
  261. ///// </summary>
  262. ///// <param name="FileToUpZip">待解压的文件</param>
  263. ///// <param name="ZipedFolder">指定解压目标目录</param>
  264. ///// <param name="Password"></param>
  265. //public static void UnZip(Stream stream, string ZipedFolder, string Password)
  266. //{
  267. // if (!Directory.Exists(ZipedFolder))
  268. // {
  269. // Directory.CreateDirectory(ZipedFolder);
  270. // }
  271. // ZipInputStream s = null;
  272. // ZipEntry theEntry = null;
  273. // string fileName;
  274. // FileStream streamWriter = null;
  275. // int count = 0;
  276. // //byte[] data = new byte[131070];
  277. // byte[] data = new byte[65535];
  278. // try
  279. // {
  280. // s = new ZipInputStream(stream);
  281. // s.Password = Password;
  282. // while ((theEntry = s.GetNextEntry()) != null)
  283. // {
  284. // if (theEntry.Name != String.Empty && theEntry.Name != "last")
  285. // {
  286. // fileName = Path.Combine(ZipedFolder, theEntry.Name);
  287. // /**/
  288. // ///判断文件路径是否是文件夹
  289. // if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  290. // {
  291. // Directory.CreateDirectory(fileName);
  292. // continue;
  293. // }
  294. // streamWriter = File.Create(fileName);
  295. // while (true)
  296. // {
  297. // count = s.Read(data, 0, data.Length);
  298. // if (count > 0)
  299. // {
  300. // streamWriter.Write(data, 0, count);
  301. // }
  302. // else
  303. // {
  304. // break;
  305. // }
  306. // }
  307. // }
  308. // }
  309. // }
  310. // catch (Exception ee)
  311. // {
  312. // if (ee.Message.IndexOf("EOF") == -1)
  313. // {
  314. // throw ee;
  315. // }
  316. // }
  317. // finally
  318. // {
  319. // if (streamWriter != null)
  320. // {
  321. // streamWriter.Close();
  322. // streamWriter = null;
  323. // }
  324. // if (theEntry != null)
  325. // {
  326. // theEntry = null;
  327. // }
  328. // if (s != null)
  329. // {
  330. // s.Close();
  331. // s = null;
  332. // }
  333. // GC.Collect();
  334. // }
  335. //}
  336. /// <summary>
  337. /// 解压功能(解压压缩文件到指定目录)
  338. /// </summary>
  339. /// <param name="FileToUpZip">待解压的文件</param>
  340. /// <param name="ZipedFolder">指定解压目标目录</param>
  341. /// <param name="Password"></param>
  342. public static void UnZip(Stream stream, string ZipedFolder, string Password)
  343. {
  344. if (!Directory.Exists(ZipedFolder))
  345. {
  346. Directory.CreateDirectory(ZipedFolder);
  347. }
  348. ZipInputStream s = null;
  349. ZipEntry theEntry = null;
  350. string fileName;
  351. //FileStream streamWriter = null;
  352. int count = 0;
  353. //byte[] data = new byte[131070];
  354. byte[] data = new byte[65535];
  355. try
  356. {
  357. s = new ZipInputStream(stream);
  358. s.Password = Password;
  359. while ((theEntry = s.GetNextEntry()) != null)
  360. {
  361. if (theEntry.Name != String.Empty && theEntry.Name != "last")
  362. {
  363. fileName = Path.Combine(ZipedFolder, theEntry.Name);
  364. /**/
  365. ///判断文件路径是否是文件夹
  366. if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  367. {
  368. Directory.CreateDirectory(fileName);
  369. continue;
  370. }
  371. using (FileStream streamWriter = File.Create(fileName))
  372. {
  373. while (true)
  374. {
  375. count = s.Read(data, 0, data.Length);
  376. if (count > 0)
  377. {
  378. streamWriter.Write(data, 0, count);
  379. }
  380. else
  381. {
  382. break;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. catch (Exception ee)
  390. {
  391. if (ee.Message.IndexOf("EOF") == -1)
  392. {
  393. throw ee;
  394. }
  395. }
  396. finally
  397. {
  398. //if (streamWriter != null)
  399. //{
  400. // streamWriter.Close();
  401. // streamWriter = null;
  402. //}
  403. if (theEntry != null)
  404. {
  405. theEntry = null;
  406. }
  407. if (s != null)
  408. {
  409. s.Close();
  410. s = null;
  411. }
  412. GC.Collect();
  413. }
  414. }
  415. //public static void UnZip(Stream stream, string ZipedFolder, string Password)
  416. //{
  417. // if (!Directory.Exists(ZipedFolder))
  418. // {
  419. // Directory.CreateDirectory(ZipedFolder);
  420. // }
  421. // ZipInputStream s = null;
  422. // ZipEntry theEntry = null;
  423. // string fileName;
  424. // FileStream streamWriter = null;
  425. // int count = 0;
  426. // byte[] data = new byte[65535];
  427. // try
  428. // {
  429. // s = new ZipInputStream(stream);
  430. // s.Password = Password;
  431. // while ((theEntry = s.GetNextEntry()) != null)
  432. // {
  433. // if (theEntry.Name != String.Empty && theEntry.Name != "last")
  434. // {
  435. // fileName = Path.Combine(ZipedFolder, theEntry.Name);
  436. // /**/
  437. // ///判断文件路径是否是文件夹
  438. // if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  439. // {
  440. // Directory.CreateDirectory(fileName);
  441. // continue;
  442. // }
  443. // streamWriter = File.Create(fileName);
  444. // byte[] buffer = new byte[655350];
  445. // while (true)
  446. // {
  447. // count = s.Read(buffer, 0, buffer.Length);
  448. // if (count > 0)
  449. // {
  450. // streamWriter.Write(buffer, 0, count);
  451. // }
  452. // else
  453. // {
  454. // break;
  455. // }
  456. // }
  457. // }
  458. // }
  459. // }
  460. // catch (Exception ee)
  461. // {
  462. // if (ee.Message.IndexOf("EOF") == -1)
  463. // {
  464. // throw ee;
  465. // }
  466. // }
  467. // finally
  468. // {
  469. // if (streamWriter != null)
  470. // {
  471. // streamWriter.Close();
  472. // streamWriter = null;
  473. // }
  474. // if (theEntry != null)
  475. // {
  476. // theEntry = null;
  477. // }
  478. // if (s != null)
  479. // {
  480. // s.Close();
  481. // s = null;
  482. // }
  483. // GC.Collect();
  484. // }
  485. //}
  486. public static Dictionary<string, List<byte>> UnZip(Stream stream)
  487. {
  488. ZipInputStream s = null;
  489. ZipEntry theEntry = null;
  490. string fileName;
  491. int count = 0;
  492. byte[] data = new byte[65535];
  493. Dictionary<string, List<byte>> result = new Dictionary<string, List<byte>>();
  494. List<byte> bPhoto;
  495. byte[] temp;
  496. try
  497. {
  498. s = new ZipInputStream(stream);
  499. s.Password = string.Empty;
  500. while ((theEntry = s.GetNextEntry()) != null)
  501. {
  502. if (theEntry.Name != String.Empty && theEntry.Name != "last")
  503. {
  504. //fileName = Path.Combine(ZipedFolder, theEntry.Name);
  505. fileName = theEntry.Name;
  506. /**/
  507. ///判断文件路径是否是文件夹
  508. if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
  509. {
  510. //Directory.CreateDirectory(fileName);
  511. continue;
  512. }
  513. bPhoto = new List<byte>();
  514. while (true)
  515. {
  516. count = s.Read(data, 0, data.Length);
  517. if (count > 0)
  518. {
  519. if (count == data.Length)
  520. {
  521. bPhoto.AddRange(data);
  522. }
  523. else
  524. {
  525. temp = new byte[count];
  526. Array.Copy(data, temp, count);
  527. bPhoto.AddRange(temp);
  528. }
  529. }
  530. else
  531. {
  532. break;
  533. }
  534. }
  535. if (bPhoto.Count > 0)
  536. {
  537. result.Add(fileName, bPhoto);
  538. }
  539. }
  540. }
  541. }
  542. catch (Exception ee)
  543. {
  544. if (ee.Message.IndexOf("EOF") == -1)
  545. {
  546. throw ee;
  547. }
  548. }
  549. finally
  550. {
  551. //if (streamWriter != null)
  552. //{
  553. // streamWriter.Close();
  554. // streamWriter = null;
  555. //}
  556. if (theEntry != null)
  557. {
  558. theEntry = null;
  559. }
  560. if (s != null)
  561. {
  562. s.Close();
  563. s = null;
  564. }
  565. GC.Collect();
  566. }
  567. return result;
  568. }
  569. }
  570. }