/// <summary> /// 保存并解压更新文件 /// </summary> string SaveAndExtractZip(byte[] bytes, string zipFilePath, string updateFile, double progress) { if (!Directory.Exists(zipFilePath)) Directory.CreateDirectory(zipFilePath); var zipFile = Path.Combine(zipFilePath, updateFile); using (BinaryWriter writer = new BinaryWriter(new FileStream(zipFile, FileMode.OpenOrCreate))) { writer.Write(bytes); writer.Flush(); writer.Close(); } var extractPath = Path.Combine(zipFilePath, "extract"); FastZipEvents events = new FastZipEvents(); //events.CompletedFile = (sender, e) => //{ // MainWindow.AppendInfo(Properties.Resources.ExtractedFile + ":" + e.Name); //}; double value = 0; events.TotalProgress = (sender, e) => { var i = e.PercentComplete * progress / 100 - value; value = e.PercentComplete * progress / 100; MainWindow.UpdateProgress(i); }; FastZip zip = new FastZip(events); zip.ExtractZip(zipFile, extractPath, ""); return extractPath; }
void DownloadAndExtractFile(IList<Manifest.ModuleInfo> newer) { double x = 80 / Math.Max(1, newer.Count()); int progress = 10; foreach (var module in newer) { OnDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(progress)); List<string> extractFiles = new List<string>(); double y = x / Math.Max(1, module.Files.Count()); foreach (var file in module.Files) { var data = DownloadFile(file, progress, (int)y); var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, m_UpdateFileFolder, Guid.NewGuid().ToString("N")); if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath); var savedFile = Path.Combine(filePath, file); using (BinaryWriter writer = new BinaryWriter(new FileStream(savedFile, FileMode.OpenOrCreate))) { writer.Write(data); writer.Flush(); writer.Close(); } var extractPath = Path.Combine(filePath, "extract"); FastZipEvents events = new FastZipEvents(); FastZip zip = new FastZip(events); zip.ExtractZip(savedFile, extractPath, ""); extractFiles.AddRange(CopyDirectory(extractPath, AppDomain.CurrentDomain.BaseDirectory)); progress = (int)(progress + y); OnDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(progress)); } module.Files.Clear(); module.Files.AddRange(extractFiles); } }