private List <NeedOtaFile> CheckOtaFiles(List <OtaFile> listOtaFiles) { if (listOtaFiles == null) { return(null); } List <NeedOtaFile> needOtaFiles = new List <NeedOtaFile>(); for (var i = 0; i < listOtaFiles.Count; i++) { OtaFile otaFile = listOtaFiles[i]; NeedOtaFile needOtaFile = new NeedOtaFile(); needOtaFile.fileName = otaFile.FileName; needOtaFile.otaUrl = otaFile.DownloadUrl; needOtaFile.localPath = otaFile.RelativePath.TrimStart('\\'); needOtaFile.md5 = otaFile.FileMd5; if (!needOtaFile.CheckMd5()) { //需要更新 needOtaFile.index = needOtaFiles.Count; needOtaFiles.Add(needOtaFile); } } return(needOtaFiles); }
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { NeedOtaFile needOtaFile = e.Argument as NeedOtaFile; if (needOtaFile == null) { return; } DownLoad(needOtaFile); e.Result = needOtaFile.index + 1; }
/// <summary> /// 下载方法 /// </summary> private void DownLoad(NeedOtaFile needOtaFile) { if (needOtaFile == null) { throw new ArgumentNullException(nameof(needOtaFile)); } //比如uri=http://localhost/Rabom/1.rar;iis就需要自己配置了。 string uri = needOtaFile.otaUrl; //截取文件名 string fileName = needOtaFile.fileName; //构造文件完全限定名,准备将网络流下载为本地文件 string fileFullName = needOtaFile.downloadPath; string downloadDir = Path.GetDirectoryName(fileFullName); if (!Directory.Exists(downloadDir)) { Directory.CreateDirectory(downloadDir); } //构造文件的配置文件的完全完全限定名 string fileCfgName = needOtaFile.downloadPath + ".cfg"; //请求地址 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); WebResponse response = request.GetResponse(); long fileLength = response.ContentLength; //获得文件长度 response.Close(); long savePosition = 0; //本地构造文件流 FileStream fs; FileStream fscfg; if (File.Exists(fileFullName)) { string localmd5 = Common.MD5File(fileFullName); if (localmd5 == needOtaFile.md5) { return; } fs = new FileStream(fileFullName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); //如果存在配置文件,则继续下载 if (File.Exists(fileCfgName)) { fscfg = new FileStream(fileCfgName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); byte[] buf = new byte[fscfg.Length]; int len = fscfg.Read(buf, 0, buf.Length); string otaText = Encoding.UTF8.GetString(buf, 0, len); string[] cfgs = otaText.Split('|'); if (cfgs.Length == 4 && cfgs[0] == fileName && cfgs[1] == fileLength.ToString() && cfgs[2] == needOtaFile.md5) { //ota文件校验正确 savePosition = Convert.ToInt64(cfgs[3]); } else { string cfg = fileName + "|" + fileLength + "|" + needOtaFile.md5 + "|" + savePosition; fscfg.Position = 0; buf = Encoding.UTF8.GetBytes(cfg); fscfg.Write(buf, 0, buf.Length); fscfg.Flush(); //创建配置文件 } } else { fscfg = new FileStream(fileCfgName, FileMode.Create); string cfg = fileName + "|" + fileLength + "|" + needOtaFile.md5 + "|" + savePosition; byte[] buf = Encoding.UTF8.GetBytes(cfg); fscfg.Write(buf, 0, buf.Length); fscfg.Flush(); //创建配置文件 } } else { fs = new FileStream(fileFullName, FileMode.Create); if (File.Exists(fileCfgName)) { File.Delete(fileCfgName); } string cfg = fileName + "|" + fileLength + "|" + needOtaFile.md5 + "|" + savePosition; fscfg = new FileStream(fileCfgName, FileMode.Create); byte[] buf = Encoding.UTF8.GetBytes(cfg); fscfg.Write(buf, 0, buf.Length); fscfg.Flush(); //创建配置文件 } down: //开辟内存空间 byte[] buffer = new byte[1024]; request = (HttpWebRequest)WebRequest.Create(uri); //请求开始位置 request.AddRange(savePosition, fileLength); fs.Position = savePosition; //获取网络流 Stream ns = request.GetResponse().GetResponseStream(); do { //获取文件读取到的长度 int length = ns.Read(buffer, 0, buffer.Length); if (length > 0) { //将字节数组写入流 fs.Write(buffer, 0, length); fs.Flush(); //写入文件 savePosition += length; string cfg = fileName + "|" + fileLength + "|" + needOtaFile.md5 + "|" + savePosition; fscfg.Position = 0; byte[] buf = Encoding.UTF8.GetBytes(cfg); fscfg.Write(buf, 0, buf.Length); fscfg.Flush(); //创建配置文件 int percent = (int)(Math.Round((double)savePosition / fileLength, 2) * 100); backgroundWorker1.ReportProgress(percent); } else { break; //下载完成 } } while (true); fs.Close(); string md5 = Common.MD5File(fileFullName); if (md5 != needOtaFile.md5) { //文件下载错误 fs = new FileStream(fileFullName, FileMode.Create); savePosition = 0; string cfg = fileName + "|" + fileLength + "|" + needOtaFile.md5 + "|" + savePosition; fscfg.Position = 0; byte[] buf = Encoding.UTF8.GetBytes(cfg); fscfg.Write(buf, 0, buf.Length); fscfg.Flush(); //创建配置文件 goto down; } else { ns.Close(); fscfg.Close(); File.Delete(fileCfgName); } }