public TargetLocationsFrm(TargetOutputLocationModel targetOutput = null) { InitializeComponent(); this.DialogResult = DialogResult.None; if (targetOutput != null) { targetOutputLocationModel = (TargetOutputLocationModel)targetOutput.Clone(); } }
private void CopyOutputToOtherTargetFolders(ZipArchivingEventArgs e) { TargetOutputLocationModel targetOutputLoc = zipModel.TargetOutputLocationModel; List <string> targetLocationsArr = targetOutputLoc.GetTargetLocations(); if (targetLocationsArr.Count <= 0) { progressBarStatus.Value = progressBarStatus.Maximum; return; } FileInfo zipOutputFile = new FileInfo(zipArchiving.NewArchiveName); String zipOutputFileName = zipOutputFile.Name; String zipOutputFullFilePathName = zipOutputFile.FullName; long totalFileOutputSize = zipOutputFile.Length * targetLocationsArr.Count; long totalTransferredByte = 0; long totalTransferredByteUpdateProgress = 10485760; //10 * 1024 * 1024, every 10MB long totalTransferredByteUpdateProgressCtr = 0; progressBarStatus.Value = 0; foreach (String targetOutputFolder in targetLocationsArr) { String outputFileFullPath = targetOutputFolder + @"\" + zipOutputFileName; if (isStopProcessing) { break; } try { if (!IsDirectoryValidAndAccessible(targetOutputFolder)) { AddLogItems("Copy file failed", String.Format(@"Failed to place the file [{1}] in [{0}] due to folder is not accessible" + @" or no read and write permission", targetOutputFolder, zipOutputFile.Name)); continue; } int bufferSize = 8 * 1024; using (FileStream sourceStream = new FileStream(zipOutputFullFilePathName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (FileStream fileStream = new FileStream(outputFileFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { fileStream.SetLength(sourceStream.Length); int bytesRead = -1; byte[] bytes = new byte[bufferSize]; if (isStopProcessing) { break; } while ((bytesRead = sourceStream.Read(bytes, 0, bufferSize)) > 0) { fileStream.Write(bytes, 0, bytesRead); totalTransferredByte += bytesRead; //update progress bar not that much, e.g. every 10MB if ((totalTransferredByte - totalTransferredByteUpdateProgressCtr) >= totalTransferredByteUpdateProgress) { totalTransferredByteUpdateProgressCtr = totalTransferredByte; Application.DoEvents(); progressBarStatus.Value = ConverterUtils.GetPercentageFloored(totalTransferredByte, totalFileOutputSize); } } fileStream.Flush(); txtBoxCurrentAction.Text = "Copying output Zip Archive to => " + targetOutputFolder; AddLogItems("Copy file success", outputFileFullPath); } } progressBarStatus.Value = ConverterUtils.GetPercentageFloored(totalTransferredByte, totalFileOutputSize); } catch (Exception ex) { AddLogItems("Copy file failed", String.Format(@"Failed: {0}\{1}. / Error message: {2}", targetOutputFolder, zipOutputFile.Name, ex.Message)); } } //In case some locations are not valid anymore progressBarStatus.Value = progressBarStatus.Maximum; if (isStopProcessing) { txtBoxCurrentAction.Text = "Copying has been halted..."; AddLogItems("Copy Output File", "Copying has been halted..."); progressBarStatus.Value = progressBarStatus.Maximum; } }