123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using ICSharpCode.SharpZipLib.Zip;
- using UnityEngine;
- public class UnZipThread
- {
- public Thread thread;
- private UnZipThreadPool unZipThreadPool;
- private string unPath;
-
- private byte[] zipData = new byte[2048];
- public bool isUnEnd;
- private bool isUpdateEnd;
- public void InitUnZip(UnZipThreadPool unZipThreadPool)
- {
- this.unZipThreadPool = unZipThreadPool;
- unPath = unZipThreadPool.unPath;
-
- isUpdateEnd = true;
- thread = new Thread(ThreadUpdate);
- thread.Start();
- }
- private void ThreadUpdate()
- {
- while (isUpdateEnd)
- {
- try
- {
- ZipEntry ze = null;
- Stream stream = null;
- lock (unZipThreadPool.allZipFile)
- {
- if (unZipThreadPool.allZipFile.Count > 0)
- {
- ze = unZipThreadPool.allZipFile[0];
- unZipThreadPool.allZipFile.Remove(ze);
- stream = unZipThreadPool.zipFile.GetInputStream(ze);
- }
- }
- if (ze == null)
- {
- Thread.Sleep(1000);
- return;
- }
- isUnEnd = false;
- UnzipFile(ze, stream, unPath);
-
- }
- catch (Exception e)
- {
- Debug.LogError("解压失败");
- }
- isUnEnd = true;
- Thread.Sleep(10);
- }
- }
- public void Cloas()
- {
- isUpdateEnd = false;
- thread = null;
- }
- private void UnzipFile(ZipEntry zip, Stream stream, string dirPath)
- {
- FileStream fileStream = null;
- try
- {
- //文件名不为空
- if (!string.IsNullOrEmpty(zip.Name))
- {
- string filePath = dirPath;
- string[] files = zip.Name.Split('/');
- // if (files.Length < 2)
- // {
- // return;
- // }
- filePath += "/" + files[files.Length - 1];
- //如果是一个新的文件路径 这里需要创建这个文件路径
- if (IsDirectory(filePath))
- {
- Debug.Log("Create file paht " + filePath);
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- }
- else
- {
- if (File.Exists(filePath))
- {
- File.Delete(filePath);
- }
- fileStream = File.Create(filePath);
- // Debug.Log(filePath);
- // MemoryStream ms = new MemoryStream();
- int size = 0;
- //每次读取2MB 直到把这个内容读完
- while (true)
- {
- size = stream.Read(zipData, 0, zipData.Length);
- //小于0, 也就读完了当前的流
- if (size > 0)
- {
- unZipThreadPool.currSize += size;
- fileStream.Write(zipData, 0, size);
- }
- else
- {
- break;
- }
- }
- fileStream.Flush();
- fileStream.Close();
- fileStream.Dispose();
- fileStream = null;
-
- }
- }
- }
- catch (Exception e)
- {
- if (fileStream != null)
- {
- fileStream.Close();
- fileStream.Dispose();
- }
- fileStream = null;
- Debug.LogError("解压错误" + zip.Name);
- Debug.Log(e);
- }
- }
- /// <summary>
- /// 判断是否是目录文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- private bool IsDirectory(string path)
- {
- if (path[path.Length - 1] == '/')
- {
- return true;
- }
- return false;
- }
- }
- public class UnZipThreadPool
- {
-
- public string unPath;
- public float jd
- {
- get { return currSize / maxSize; }
- }
- public float currSize;
- public bool isEnd
- {
- get
- {
- bool isCount = false;
- lock (allZipFile)
- {
- if (allZipFile.Count <= 0)
- {
- isCount = true;
- }
- }
- if (isCount)
- {
- for (int i = 0; i < _zipThreads.Count; i++)
- {
- if (!_zipThreads[i].isUnEnd)
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- }
- public float maxSize;
-
- public ZipFile zipFile;
- public List<ZipEntry> allZipFile = new List<ZipEntry>();
- private List<UnZipThread> _zipThreads = new List<UnZipThread>();
- public int CurrCount()
- {
- lock (allZipFile)
- {
- return allZipFile.Count;
- }
- }
- private FileStream fileStream;
- public void InitUnZip(byte[] zipData, string unPath)
- {
-
- this.unPath = unPath;
- maxSize = zipData.Length;
- MemoryStream ms = new MemoryStream(zipData);
- zipFile = new ZipFile(ms);
- IEnumerator ie = zipFile.GetEnumerator();
- while (ie.MoveNext())
- {
- allZipFile.Add((ZipEntry) ie.Current);
- }
- for (int i = 0; i < 8; i++)
- {
- UnZipThread u = new UnZipThread();
- u.InitUnZip(this);
- _zipThreads.Add(u);
- }
- }
- public void InitUnZip(string filePath, string unPath)
- {
-
- if (!Directory.Exists(unPath))
- {
- Directory.CreateDirectory(unPath);
- }
- this.unPath = unPath;
- fileStream = new FileStream(filePath, FileMode.Open);
- zipFile = new ZipFile(fileStream);
- IEnumerator ie = zipFile.GetEnumerator();
- while (ie.MoveNext())
- {
- allZipFile.Add((ZipEntry) ie.Current);
- }
- maxSize = allZipFile.Count;
- for (int i = 0; i < 4; i++)
- {
- UnZipThread u = new UnZipThread();
- u.InitUnZip(this);
- _zipThreads.Add(u);
- }
- }
-
- public void Cloas()
- {
- for (int i = 0; i < _zipThreads.Count; i++)
- {
- _zipThreads[i].Cloas();
- }
- if (fileStream != null)
- {
- fileStream.Close();
- fileStream.Dispose();
- }
- allZipFile.Clear();
- fileStream = null;
- }
- // public void UnzipWithPath()
- // {
- // Debug.Log("dirpath is:" + unPath);
- // //ZipEntry:文件条目 就是该目录下所有的文件列表(也就是所有文件的路径)
- // ZipEntry zip = null;
- // //输入的所有的文件流都是存储在这里面的
- // ZipInputStream zipInStream = null;
- //
- // MemoryStream ms = new MemoryStream(zipData);
- //
- // //读取文件流到zipInputStream
- // zipInStream = new ZipInputStream(ms);
- //
- // int topSize = zipData.Length;
- //
- // int countCount = 0;
- //
- // //循环读取Zip目录下的所有文件
- // while ((zip = zipInStream.GetNextEntry()) != null)
- // {
- // // Debug.Log("name is:" zip.Name " zipStream " zipInStream);
- // UnzipFile(zip, zipInStream, unPath);
- // countCount++;
- // jd = currSize * 1.0f / topSize;
- // if (countCount > 20)
- // {
- // countCount = 0;
- // Thread.Sleep(10);
- // }
- // }
- //
- // zipInStream.Close();
- // isEnd = true;
- // }
- //
- // private void UnzipFile(ZipEntry zip, ZipInputStream zipInStream, string dirPath)
- // {
- // try
- // {
- // //文件名不为空
- // if (!string.IsNullOrEmpty(zip.Name))
- // {
- // string filePath = dirPath;
- // string[] files = zip.Name.Split('/');
- // if (files.Length < 2)
- // {
- // return;
- // }
- //
- // filePath += "/" + files[files.Length - 1];
- //
- //
- // //如果是一个新的文件路径 这里需要创建这个文件路径
- // if (IsDirectory(filePath))
- // {
- // Debug.Log("Create file paht " + filePath);
- // if (!Directory.Exists(filePath))
- // {
- // Directory.CreateDirectory(filePath);
- // }
- // }
- // else
- // {
- // MemoryStream ms = new MemoryStream();
- //
- // int size = 0;
- //
- // //每次读取2MB 直到把这个内容读完
- // while (true)
- // {
- // size = zipInStream.Read(zipData, 0, zipData.Length);
- // //小于0, 也就读完了当前的流
- // if (size > 0)
- // {
- // currSize += size;
- // ms.Write(zipData, 0, size);
- // }
- // else
- // {
- // break;
- // }
- // }
- //
- // ms.Flush();
- // byte[] fileData = ms.ToArray();
- // File.WriteAllBytes(filePath, fileData);
- // if (zip.Name.Contains("md5File"))
- // {
- // File.WriteAllBytes(md5Path, fileData);
- // }
- //
- // ms.Close();
- // ms.Dispose();
- // }
- // }
- // }
- // catch (Exception e)
- // {
- // Debug.LogError("解压错误"+zip.Name);
- // Debug.Log(e);
- // }
- // }
- //
- // /// <summary>
- // /// 判断是否是目录文件
- // /// </summary>
- // /// <param name="path"></param>
- // /// <returns></returns>
- // private bool IsDirectory(string path)
- // {
- // if (path[path.Length - 1] == '/')
- // {
- // return true;
- // }
- //
- // return false;
- // }
- }
|