public static ArchiveExtractResult Extract7z(SharpCompress.Archives.SevenZip.SevenZipArchive extractor, string outputFolder, System.EventHandler <ExtractProgress> progress_callback) { Dictionary <bool, List <SharpCompress.Common.IEntry> > myList = new Dictionary <bool, List <SharpCompress.Common.IEntry> >(); myList.Add(true, new List <SharpCompress.Common.IEntry>()); myList.Add(false, new List <SharpCompress.Common.IEntry>()); int total = extractor.Entries.Count; int extractedindex = 0; using (var entries = extractor.ExtractAllEntries()) while (entries.MoveToNextEntry()) { try { FileInfo fi = new FileInfo(Path.Combine(outputFolder, entries.Entry.Key)); FileSystem.CreateDirectory(fi.DirectoryName); using (FileStream fs = fi.Create()) { entries.WriteEntryTo(fs); fs.Flush(); } myList[true].Add(entries.Entry); } catch (System.Exception) { myList[false].Add(entries.Entry); } extractedindex++; if (progress_callback != null) { syncContext.Post(new System.Threading.SendOrPostCallback(delegate { progress_callback?.Invoke(extractor, new ExtractProgress(total, extractedindex)); }), null); } } return(new ArchiveExtractResult(myList)); /*Dictionary<bool, List<ArchiveFileInfo>> myList = new Dictionary<bool, List<ArchiveFileInfo>>(); * myList.Add(true, new List<ArchiveFileInfo>()); * myList.Add(false, new List<ArchiveFileInfo>()); * extractor.ExtractArchive(outputFolder); * if (progress_callback != null) * extractor.Extracting += progress_callback; * foreach (var node in extractor.ArchiveFileData) * { * if (!node.IsDirectory) * { * if (File.Exists(Path.Combine(outputFolder, node.FileName))) * myList[true].Add(node); * else * myList[false].Add(node); * } * } * return (new SevenZipExtractResult(myList));//*/ }
protected virtual void OnDownloadUpdate(Version ver) { this._CurrentProgress = -1; string thePath = Path.ChangeExtension(Leayal.AppInfo.ApplicationFilename, ".update-" + ver.ToString()); this.RaiseEventStepChanged(LanguageManager.GetMessageText("SelfUpdate_ExtractingUpdates", "Extracting Updates")); using (FileStream fssss = new FileStream(thePath + ".7z", FileMode.Open, FileAccess.Read, FileShare.Read)) if (SharpCompress.Archives.SevenZip.SevenZipArchive.IsSevenZipFile(fssss)) { fssss.Position = 0; using (SharpCompress.Archives.SevenZip.SevenZipArchive archive = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(fssss)) using (SharpCompress.Readers.IReader reader = archive.ExtractAllEntries()) while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { if (reader.Entry.Key.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { using (FileStream fs = File.Create(thePath)) reader.WriteEntryTo(fs); break; } } } } else { this.OnHandledException(new HandledExceptionEventArgs(new FileNotFoundException("Update content not found", thePath))); } try { File.Delete(thePath + ".7z"); } catch { } this.OnProgressBarStateChanged(new Events.ProgressBarStateChangedEventArgs(Forms.MyMainMenu.ProgressBarVisibleState.Infinite)); this.RaiseEventStepChanged(LanguageManager.GetMessageText("SelfUpdate_RestartToUpdate", "Restarting application to perform update.")); using (Process theProcess = new Process()) { theProcess.StartInfo.FileName = this.UpdaterPath; theProcess.StartInfo.Arguments = Leayal.ProcessHelper.TableStringToArgs( new string[] { "-leayal", $"-patch:{thePath}", $"-destination:{Leayal.AppInfo.ApplicationFilename}" } ); if (Leayal.OSVersionInfo.IsVistaAndUp) { theProcess.StartInfo.Verb = "runas"; } theProcess.Start(); } Environment.Exit(0); }
private void myWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if ((e.Error != null)) { this.OnHandledException(new HandledExceptionEventArgs(e.Error)); } else { if (e.UserState is Version) { this.OnDownloadUpdate((Version)e.UserState); } else if (e.UserState is DownloadFileMeta) { DownloadFileMeta state = (DownloadFileMeta)e.UserState; if ((state.State == "downloadupdater")) { this.RaiseEventStepChanged("Downloading new version"); using (FileStream fsss = new FileStream(this.UpdaterPath + ".7z", FileMode.Open, FileAccess.Read, FileShare.Read)) if (SharpCompress.Archives.SevenZip.SevenZipArchive.IsSevenZipFile(fsss)) { fsss.Position = 0; using (SharpCompress.Archives.SevenZip.SevenZipArchive archive = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(fsss)) using (SharpCompress.Readers.IReader reader = archive.ExtractAllEntries()) while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { if (reader.Entry.Key.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { using (FileStream fs = File.Create(this.UpdaterPath)) reader.WriteEntryTo(fs); break; } } } } else { this.OnHandledException(new HandledExceptionEventArgs(new FileNotFoundException("Updater not found", this.UpdaterPath))); } try { File.Delete(this.UpdaterPath + ".7z"); } catch { } this.OnProgressBarStateChanged(new Events.ProgressBarStateChangedEventArgs(Forms.MyMainMenu.ProgressBarVisibleState.Percent)); this.myWebClient.DownloadFileAsync(this.UpdateUri, Path.ChangeExtension(Leayal.AppInfo.ApplicationFilename, ".update-" + state.Ver.ToString()) + ".7z", state.Ver); } } } }