protected override void OnAbort() { SiphonStream abortMe = this.abortMeStream; if (abortMe != null) { abortMe.Abort(new Exception()); } base.OnAbort(); }
public override void OnEnteredState() { this.zipTempName = Path.GetTempFileName() + ".zip"; try { bool flag; if (OS.VerifyFrameworkVersion(this.downloadMe.NetFxMajorVersion, this.downloadMe.NetFxMinorVersion, this.downloadMe.NetFxServicePack, true)) { flag = false; } else { flag = true; } base.OnProgress(0.0); FileStream underlyingStream = new FileStream(this.zipTempName, FileMode.Create, FileAccess.Write, FileShare.Read); try { SiphonStream output = new SiphonStream(underlyingStream); this.abortMeStream = output; ProgressEventHandler progressCallback = (sender, e) => base.OnProgress(e.Percent); WebHelpers.DownloadFile(new Uri(this.downloadMe.ChooseDownloadUrl(flag)), output, progressCallback); output.Flush(); this.abortMeStream = null; output = null; } finally { if (underlyingStream != null) { underlyingStream.Close(); underlyingStream = null; } } base.StateMachine.QueueInput(PrivateInput.GoToExtracting); } catch (Exception exception) { this.exception = exception; if (base.AbortRequested) { base.StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = exception; base.StateMachine.QueueInput(PrivateInput.GoToError); } } }
public static Document LoadDocument(Control owner, string fileName, out FileType fileTypeResult, ProgressEventHandler progressCallback) { fileTypeResult = null; try { var fileTypes = FileTypes.GetFileTypes(); var extName = Path.GetExtension(fileName); if (extName == null) { //Todo: Get file real type. throw new Exception("Get file real type."); } extName = extName.Replace(".", ""); fileTypeResult = fileTypes.First(x => x.Extensions.Any(ext => ext == extName)); } catch (ArgumentException) { string format = PdnResources.GetString("LoadImage.Error.InvalidFileName.Format"); string error = string.Format(format, fileName); Utility.ErrorBox(owner, error); return(null); } Document document = null; using (new WaitCursorChanger(owner)) { Utility.GCFullCollect(); Stream stream = null; try { try { stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); long totalBytes = 0; var siphonStream = new SiphonStream(stream); IOEventHandler ioEventHandler = null; ioEventHandler = delegate(object sender, IOEventArgs e) { if (progressCallback != null) { totalBytes += (long)e.Count; double percent = Utility.Clamp(100.0 * ((double)totalBytes / (double)siphonStream.Length), 0, 100); progressCallback(null, new ProgressEventArgs(percent)); } }; siphonStream.IOFinished += ioEventHandler; using (new WaitCursorChanger(owner)) { document = fileTypeResult.Load(siphonStream); if (progressCallback != null) { progressCallback(null, new ProgressEventArgs(100.0)); } } siphonStream.IOFinished -= ioEventHandler; siphonStream.Close(); } catch (WorkerThreadException ex) { Type innerExType = ex.InnerException.GetType(); ConstructorInfo ci = innerExType.GetConstructor(new Type[] { typeof(string), typeof(Exception) }); if (ci == null) { throw; } else { var ex2 = (Exception)ci.Invoke(new object[] { "Worker thread threw an exception of this type", ex.InnerException }); throw ex2; } } } catch (ArgumentException) { if (fileName.Length == 0) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.BlankFileName")); } else { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.ArgumentException")); } } catch (UnauthorizedAccessException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.UnauthorizedAccessException")); } catch (SecurityException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.SecurityException")); } catch (FileNotFoundException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.FileNotFoundException")); } catch (DirectoryNotFoundException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.DirectoryNotFoundException")); } catch (PathTooLongException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.PathTooLongException")); } catch (IOException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.IOException")); } catch (SerializationException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.SerializationException")); } catch (OutOfMemoryException) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.OutOfMemoryException")); } catch (Exception) { Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.Exception")); } finally { if (stream != null) { stream.Close(); stream = null; } } } return(document); }
public void OnEnteredStateImpl() { FileStream zipFileRead = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream exeFileWrite = null; try { ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFileRead); // Search for the first .msi file in the exe, and extract it ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; bool foundExe = false; while (true) { zipEntry = zipStream.GetNextEntry(); if (zipEntry == null) { break; } if (!zipEntry.IsDirectory && string.Compare(".exe", Path.GetExtension(zipEntry.Name), true, CultureInfo.InvariantCulture) == 0) { foundExe = true; break; } } if (!foundExe) { this.exception = new FileNotFoundException(); StateMachine.QueueInput(PrivateInput.GoToError); } else { int maxBytes = (int)zipEntry.Size; int bytesSoFar = 0; this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), zipEntry.Name); exeFileWrite = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read); SiphonStream siphonStream2 = new SiphonStream(exeFileWrite, 4096); this.abortMeStream = siphonStream2; IOEventHandler ioFinishedDelegate = delegate(object sender, IOEventArgs e) { bytesSoFar += e.Count; double percent = 100.0 * ((double)bytesSoFar / (double)maxBytes); OnProgress(percent); }; OnProgress(0.0); if (maxBytes > 0) { siphonStream2.IOFinished += ioFinishedDelegate; } Utility.CopyStream(zipStream, siphonStream2); if (maxBytes > 0) { siphonStream2.IOFinished -= ioFinishedDelegate; } this.abortMeStream = null; siphonStream2 = null; exeFileWrite.Close(); exeFileWrite = null; zipStream.Close(); zipStream = null; StateMachine.QueueInput(PrivateInput.GoToReadyToInstall); } } catch (Exception ex) { if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } finally { if (exeFileWrite != null) { exeFileWrite.Close(); exeFileWrite = null; } if (zipFileRead != null) { zipFileRead.Close(); zipFileRead = null; } if (this.exception != null || this.AbortRequested) { if (this.installerPath != null) { try { File.Delete(this.installerPath); } catch { } } } if (this.extractMe != null) { try { File.Delete(this.extractMe); } catch { } } } }
public void OnEnteredStateImpl() { FileStream zipFileRead = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream exeFileWrite = null; try { ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFileRead); // Search for the first .msi file in the exe, and extract it ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; bool foundExe = false; while (true) { zipEntry = zipStream.GetNextEntry(); if (zipEntry == null) { break; } if (!zipEntry.IsDirectory && string.Compare(".exe", Path.GetExtension(zipEntry.Name), true, CultureInfo.InvariantCulture) == 0) { foundExe = true; break; } } if (!foundExe) { this.exception = new FileNotFoundException(); StateMachine.QueueInput(PrivateInput.GoToError); } else { int maxBytes = (int)zipEntry.Size; int bytesSoFar = 0; this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), zipEntry.Name); exeFileWrite = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read); SiphonStream siphonStream2 = new SiphonStream(exeFileWrite, 4096); this.abortMeStream = siphonStream2; IOEventHandler ioFinishedDelegate = delegate(object sender, IOEventArgs e) { bytesSoFar += e.Count; double percent = 100.0 * ((double)bytesSoFar / (double)maxBytes); OnProgress(percent); }; OnProgress(0.0); if (maxBytes > 0) { siphonStream2.IOFinished += ioFinishedDelegate; } Utility.CopyStream(zipStream, siphonStream2); if (maxBytes > 0) { siphonStream2.IOFinished -= ioFinishedDelegate; } this.abortMeStream = null; siphonStream2 = null; exeFileWrite.Close(); exeFileWrite = null; zipStream.Close(); zipStream = null; StateMachine.QueueInput(PrivateInput.GoToReadyToInstall); } } catch (Exception ex) { if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } finally { if (exeFileWrite != null) { exeFileWrite.Close(); exeFileWrite = null; } if (zipFileRead != null) { zipFileRead.Close(); zipFileRead = null; } if (this.exception != null || this.AbortRequested) { if (this.installerPath != null) { bool result = FileSystem.TryDeleteFile(this.installerPath); } } if (this.extractMe != null) { bool result = FileSystem.TryDeleteFile(this.extractMe); } } }
public override void OnEnteredState() { this.zipTempName = Path.GetTempFileName() + ".zip"; try { bool getFull; if (SystemLayer.OS.IsDotNetVersionInstalled( downloadMe.NetFxVersion.Major, downloadMe.NetFxVersion.Minor, downloadMe.NetFxVersion.Build)) { getFull = false; } else { getFull = true; } OnProgress(0.0); FileStream zipFileWrite = new FileStream(zipTempName, FileMode.Create, FileAccess.Write, FileShare.Read); try { // we need to wrap the zipFileWrite in a SiphonStream so that we can // Abort() it externally SiphonStream monitorStream = new SiphonStream(zipFileWrite); this.abortMeStream = monitorStream; ProgressEventHandler progressCallback = delegate(object sender, ProgressEventArgs e) { OnProgress(e.Percent); }; string url; url = downloadMe.ChooseDownloadUrl(getFull); SystemLayer.Tracing.Ping("Chosen mirror url: " + url); Utility.DownloadFile(new Uri(url), monitorStream, progressCallback); monitorStream.Flush(); this.abortMeStream = null; monitorStream = null; } finally { if (zipFileWrite != null) { zipFileWrite.Close(); zipFileWrite = null; } } StateMachine.QueueInput(PrivateInput.GoToExtracting); } catch (Exception ex) { this.exception = ex; if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } }
public override void OnEnteredState() { this.zipTempName = Path.GetTempFileName() + ".zip"; try { bool getFull; if (SystemLayer.OS.IsDotNetVersionInstalled( downloadMe.NetFxMajorVersion, downloadMe.NetFxMinorVersion, downloadMe.NetFxServicePack, true)) { getFull = false; } else { getFull = true; } OnProgress(0.0); FileStream zipFileWrite = new FileStream(zipTempName, FileMode.Create, FileAccess.Write, FileShare.Read); try { // we need to wrap the zipFileWrite in a SiphonStream so that we can // Abort() it externally SiphonStream monitorStream = new SiphonStream(zipFileWrite); this.abortMeStream = monitorStream; ProgressEventHandler progressCallback = delegate(object sender, ProgressEventArgs e) { OnProgress(e.Percent); }; string url; url = downloadMe.ChooseDownloadUrl(getFull); SystemLayer.Tracing.Ping("Chosen mirror url: " + url); Utility.DownloadFile(new Uri(url), monitorStream, progressCallback); monitorStream.Flush(); this.abortMeStream = null; monitorStream = null; } finally { if (zipFileWrite != null) { zipFileWrite.Close(); zipFileWrite = null; } } StateMachine.QueueInput(PrivateInput.GoToExtracting); } catch (Exception ex) { this.exception = ex; if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } }
public void OnEnteredStateImpl() { FileStream stream = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream underlyingStream = null; try { using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, true)) { ZipArchiveEntry entry = archive.Entries.FirstOrDefault <ZipArchiveEntry>(ze => string.Compare(".exe", Path.GetExtension(ze.Name), true, CultureInfo.InvariantCulture) == 0); if (entry == null) { this.exception = new FileNotFoundException(); base.StateMachine.QueueInput(PrivateInput.GoToError); } else { int maxBytes = (int)entry.Length; int bytesSoFar = 0; this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), entry.Name); underlyingStream = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read); SiphonStream output = new SiphonStream(underlyingStream, 0x1000); this.abortMeStream = output; IOEventHandler handler = delegate(object sender, IOEventArgs e) { bytesSoFar += e.Count; double percent = 100.0 * (((double)bytesSoFar) / ((double)maxBytes)); this.OnProgress(percent); }; base.OnProgress(0.0); if (maxBytes > 0) { output.IOFinished += handler; } using (Stream stream4 = entry.Open()) { StreamUtil.CopyStream(stream4, output); } if (maxBytes > 0) { output.IOFinished -= handler; } this.abortMeStream = null; output = null; underlyingStream.Close(); underlyingStream = null; base.StateMachine.QueueInput(PrivateInput.GoToReadyToInstall); } } } catch (Exception exception) { if (base.AbortRequested) { base.StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = exception; base.StateMachine.QueueInput(PrivateInput.GoToError); } } finally { if (underlyingStream != null) { underlyingStream.Close(); underlyingStream = null; } if (stream != null) { stream.Close(); stream = null; } if (((this.exception != null) || base.AbortRequested) && (this.installerPath != null)) { bool flag = FileSystem.TryDeleteFile(this.installerPath); } if (this.extractMe != null) { bool flag2 = FileSystem.TryDeleteFile(this.extractMe); } } }