static XGameUpdate.UpdateFileInfo GenFiles(string inputPath, bool isCry) { if (inputPath == null || inputPath.Equals("")) { return(null); } var fi = new XGameUpdate.UpdateFileInfo(); var dir = inputPath.Split('/'); //压缩出来的文件加个随机数 fi.name = dir[dir.Length - 1]; int randomNum = UnityEngine.Random.Range(0, 999999999); fi.packageName = fi.name + "_" + randomNum; ForeachFile(inputPath, (string f) => { Debug.LogWarning("genfile " + f); if (Path.GetExtension(f).Equals("meta")) { File.Delete(f); } else { if (isCry) { CryFile(f); } } }); var outPath = inputPath + "_" + randomNum + ".zip"; if (File.Exists(outPath)) { if (File.Exists(outPath + "_bk")) { File.Delete(outPath + "_bk"); } File.Move(outPath, outPath + "_bk"); } Debug.LogError("zipfile " + inputPath); DWTools.FastZipCompress(inputPath, outPath); fi.md5 = DWTools.GenMD5(outPath); fi.length = GetFileLength(outPath); return(fi); }
//开始下载 public void BeginDownLoad() { m_stop = false; m_hasDownLoadBits = 0; //用于显示当前的进度 long lStartPos = 0; //打开上次下载的文件或新建文件 FileStream fs; if (File.Exists(m_fileName)) { var md5 = DWTools.GenMD5(m_fileName); Debug.Log("get md5:" + md5); Debug.Log("need md5:" + m_md5); if (m_md5.ToLower() == md5.ToLower()) { m_downLoadStatus = EnumDownLoadStatus.EDLS_SUCCESS; return; } fs = File.OpenWrite(m_fileName); //打开流 lStartPos = fs.Length; //通过字节流的长度确定当前的下载位置 fs.Seek(lStartPos, SeekOrigin.Current); //移动文件流中的当前指针 } else { fs = new FileStream(m_fileName, FileMode.Create); lStartPos = 0; } try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_url); request.Timeout = 10000; if (lStartPos > 0) { request.AddRange((int)lStartPos); //设置Range值 m_hasDownLoadBits += lStartPos; } //向服务器请求,获得服务器回应数据流 Stream ns = request.GetResponse().GetResponseStream(); ns.ReadTimeout = 10000; byte[] nbytes = new byte[1024 * 1024]; int nReadSize = 0; nReadSize = ns.Read(nbytes, 0, 1024 * 1024); while (nReadSize > 0 || m_stop) { fs.Write(nbytes, 0, nReadSize); m_hasDownLoadBits = m_hasDownLoadBits + nReadSize; nReadSize = ns.Read(nbytes, 0, 1024 * 1024); } ns.Close(); fs.Flush(); if (m_stop) { Debug.LogError("Stop Success"); m_downLoadStatus = EnumDownLoadStatus.EDLS_STOP; fs.Close(); } else { //检测MD5 fs.Close(); var md5 = DWTools.GenMD5(m_fileName); Debug.Log("get md5:" + md5); Debug.Log("need md5:" + m_md5); if (m_md5.ToLower() == md5.ToLower()) { m_downLoadStatus = EnumDownLoadStatus.EDLS_SUCCESS; } else { File.Delete(m_fileName); m_downLoadStatus = EnumDownLoadStatus.EDLS_MD5WRONG; } } } catch (Exception ex) { fs.Close(); m_downLoadStatus = EnumDownLoadStatus.EDLS_NETERROR; Debug.LogError(ex.ToString()); } }