UnZipThread.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading;
  6. using ICSharpCode.SharpZipLib.Zip;
  7. using UnityEngine;
  8. public class UnZipThread
  9. {
  10. public Thread thread;
  11. private UnZipThreadPool unZipThreadPool;
  12. private string unPath;
  13. private byte[] zipData = new byte[2048];
  14. public bool isUnEnd;
  15. private bool isUpdateEnd;
  16. public void InitUnZip(UnZipThreadPool unZipThreadPool)
  17. {
  18. this.unZipThreadPool = unZipThreadPool;
  19. unPath = unZipThreadPool.unPath;
  20. isUpdateEnd = true;
  21. thread = new Thread(ThreadUpdate);
  22. thread.Start();
  23. }
  24. private void ThreadUpdate()
  25. {
  26. while (isUpdateEnd)
  27. {
  28. try
  29. {
  30. ZipEntry ze = null;
  31. Stream stream = null;
  32. lock (unZipThreadPool.allZipFile)
  33. {
  34. if (unZipThreadPool.allZipFile.Count > 0)
  35. {
  36. ze = unZipThreadPool.allZipFile[0];
  37. unZipThreadPool.allZipFile.Remove(ze);
  38. stream = unZipThreadPool.zipFile.GetInputStream(ze);
  39. }
  40. }
  41. if (ze == null)
  42. {
  43. Thread.Sleep(1000);
  44. return;
  45. }
  46. isUnEnd = false;
  47. UnzipFile(ze, stream, unPath);
  48. }
  49. catch (Exception e)
  50. {
  51. Debug.LogError("解压失败");
  52. }
  53. isUnEnd = true;
  54. Thread.Sleep(10);
  55. }
  56. }
  57. public void Cloas()
  58. {
  59. isUpdateEnd = false;
  60. thread = null;
  61. }
  62. private void UnzipFile(ZipEntry zip, Stream stream, string dirPath)
  63. {
  64. FileStream fileStream = null;
  65. try
  66. {
  67. //文件名不为空
  68. if (!string.IsNullOrEmpty(zip.Name))
  69. {
  70. string filePath = dirPath;
  71. string[] files = zip.Name.Split('/');
  72. // if (files.Length < 2)
  73. // {
  74. // return;
  75. // }
  76. filePath += "/" + files[files.Length - 1];
  77. //如果是一个新的文件路径 这里需要创建这个文件路径
  78. if (IsDirectory(filePath))
  79. {
  80. Debug.Log("Create file paht " + filePath);
  81. if (!Directory.Exists(filePath))
  82. {
  83. Directory.CreateDirectory(filePath);
  84. }
  85. }
  86. else
  87. {
  88. if (File.Exists(filePath))
  89. {
  90. File.Delete(filePath);
  91. }
  92. fileStream = File.Create(filePath);
  93. // Debug.Log(filePath);
  94. // MemoryStream ms = new MemoryStream();
  95. int size = 0;
  96. //每次读取2MB 直到把这个内容读完
  97. while (true)
  98. {
  99. size = stream.Read(zipData, 0, zipData.Length);
  100. //小于0, 也就读完了当前的流
  101. if (size > 0)
  102. {
  103. unZipThreadPool.currSize += size;
  104. fileStream.Write(zipData, 0, size);
  105. }
  106. else
  107. {
  108. break;
  109. }
  110. }
  111. fileStream.Flush();
  112. fileStream.Close();
  113. fileStream.Dispose();
  114. fileStream = null;
  115. }
  116. }
  117. }
  118. catch (Exception e)
  119. {
  120. if (fileStream != null)
  121. {
  122. fileStream.Close();
  123. fileStream.Dispose();
  124. }
  125. fileStream = null;
  126. Debug.LogError("解压错误" + zip.Name);
  127. Debug.Log(e);
  128. }
  129. }
  130. /// <summary>
  131. /// 判断是否是目录文件
  132. /// </summary>
  133. /// <param name="path"></param>
  134. /// <returns></returns>
  135. private bool IsDirectory(string path)
  136. {
  137. if (path[path.Length - 1] == '/')
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. }
  144. public class UnZipThreadPool
  145. {
  146. public string unPath;
  147. public float jd
  148. {
  149. get { return currSize / maxSize; }
  150. }
  151. public float currSize;
  152. public bool isEnd
  153. {
  154. get
  155. {
  156. bool isCount = false;
  157. lock (allZipFile)
  158. {
  159. if (allZipFile.Count <= 0)
  160. {
  161. isCount = true;
  162. }
  163. }
  164. if (isCount)
  165. {
  166. for (int i = 0; i < _zipThreads.Count; i++)
  167. {
  168. if (!_zipThreads[i].isUnEnd)
  169. {
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. return false;
  176. }
  177. }
  178. public float maxSize;
  179. public ZipFile zipFile;
  180. public List<ZipEntry> allZipFile = new List<ZipEntry>();
  181. private List<UnZipThread> _zipThreads = new List<UnZipThread>();
  182. public int CurrCount()
  183. {
  184. lock (allZipFile)
  185. {
  186. return allZipFile.Count;
  187. }
  188. }
  189. private FileStream fileStream;
  190. public void InitUnZip(byte[] zipData, string unPath)
  191. {
  192. this.unPath = unPath;
  193. maxSize = zipData.Length;
  194. MemoryStream ms = new MemoryStream(zipData);
  195. zipFile = new ZipFile(ms);
  196. IEnumerator ie = zipFile.GetEnumerator();
  197. while (ie.MoveNext())
  198. {
  199. allZipFile.Add((ZipEntry) ie.Current);
  200. }
  201. for (int i = 0; i < 8; i++)
  202. {
  203. UnZipThread u = new UnZipThread();
  204. u.InitUnZip(this);
  205. _zipThreads.Add(u);
  206. }
  207. }
  208. public void InitUnZip(string filePath, string unPath)
  209. {
  210. if (!Directory.Exists(unPath))
  211. {
  212. Directory.CreateDirectory(unPath);
  213. }
  214. this.unPath = unPath;
  215. fileStream = new FileStream(filePath, FileMode.Open);
  216. zipFile = new ZipFile(fileStream);
  217. IEnumerator ie = zipFile.GetEnumerator();
  218. while (ie.MoveNext())
  219. {
  220. allZipFile.Add((ZipEntry) ie.Current);
  221. }
  222. maxSize = allZipFile.Count;
  223. for (int i = 0; i < 4; i++)
  224. {
  225. UnZipThread u = new UnZipThread();
  226. u.InitUnZip(this);
  227. _zipThreads.Add(u);
  228. }
  229. }
  230. public void Cloas()
  231. {
  232. for (int i = 0; i < _zipThreads.Count; i++)
  233. {
  234. _zipThreads[i].Cloas();
  235. }
  236. if (fileStream != null)
  237. {
  238. fileStream.Close();
  239. fileStream.Dispose();
  240. }
  241. allZipFile.Clear();
  242. fileStream = null;
  243. }
  244. // public void UnzipWithPath()
  245. // {
  246. // Debug.Log("dirpath is:" + unPath);
  247. // //ZipEntry:文件条目 就是该目录下所有的文件列表(也就是所有文件的路径)
  248. // ZipEntry zip = null;
  249. // //输入的所有的文件流都是存储在这里面的
  250. // ZipInputStream zipInStream = null;
  251. //
  252. // MemoryStream ms = new MemoryStream(zipData);
  253. //
  254. // //读取文件流到zipInputStream
  255. // zipInStream = new ZipInputStream(ms);
  256. //
  257. // int topSize = zipData.Length;
  258. //
  259. // int countCount = 0;
  260. //
  261. // //循环读取Zip目录下的所有文件
  262. // while ((zip = zipInStream.GetNextEntry()) != null)
  263. // {
  264. // // Debug.Log("name is:" zip.Name " zipStream " zipInStream);
  265. // UnzipFile(zip, zipInStream, unPath);
  266. // countCount++;
  267. // jd = currSize * 1.0f / topSize;
  268. // if (countCount > 20)
  269. // {
  270. // countCount = 0;
  271. // Thread.Sleep(10);
  272. // }
  273. // }
  274. //
  275. // zipInStream.Close();
  276. // isEnd = true;
  277. // }
  278. //
  279. // private void UnzipFile(ZipEntry zip, ZipInputStream zipInStream, string dirPath)
  280. // {
  281. // try
  282. // {
  283. // //文件名不为空
  284. // if (!string.IsNullOrEmpty(zip.Name))
  285. // {
  286. // string filePath = dirPath;
  287. // string[] files = zip.Name.Split('/');
  288. // if (files.Length < 2)
  289. // {
  290. // return;
  291. // }
  292. //
  293. // filePath += "/" + files[files.Length - 1];
  294. //
  295. //
  296. // //如果是一个新的文件路径 这里需要创建这个文件路径
  297. // if (IsDirectory(filePath))
  298. // {
  299. // Debug.Log("Create file paht " + filePath);
  300. // if (!Directory.Exists(filePath))
  301. // {
  302. // Directory.CreateDirectory(filePath);
  303. // }
  304. // }
  305. // else
  306. // {
  307. // MemoryStream ms = new MemoryStream();
  308. //
  309. // int size = 0;
  310. //
  311. // //每次读取2MB 直到把这个内容读完
  312. // while (true)
  313. // {
  314. // size = zipInStream.Read(zipData, 0, zipData.Length);
  315. // //小于0, 也就读完了当前的流
  316. // if (size > 0)
  317. // {
  318. // currSize += size;
  319. // ms.Write(zipData, 0, size);
  320. // }
  321. // else
  322. // {
  323. // break;
  324. // }
  325. // }
  326. //
  327. // ms.Flush();
  328. // byte[] fileData = ms.ToArray();
  329. // File.WriteAllBytes(filePath, fileData);
  330. // if (zip.Name.Contains("md5File"))
  331. // {
  332. // File.WriteAllBytes(md5Path, fileData);
  333. // }
  334. //
  335. // ms.Close();
  336. // ms.Dispose();
  337. // }
  338. // }
  339. // }
  340. // catch (Exception e)
  341. // {
  342. // Debug.LogError("解压错误"+zip.Name);
  343. // Debug.Log(e);
  344. // }
  345. // }
  346. //
  347. // /// <summary>
  348. // /// 判断是否是目录文件
  349. // /// </summary>
  350. // /// <param name="path"></param>
  351. // /// <returns></returns>
  352. // private bool IsDirectory(string path)
  353. // {
  354. // if (path[path.Length - 1] == '/')
  355. // {
  356. // return true;
  357. // }
  358. //
  359. // return false;
  360. // }
  361. }