public void Decompress(string sourcePath, string destination) { _logger.Info("Extracting package to " + destination); var archive = ArchiveFactory.Open(sourcePath).Entries.ToList(); var progress = new ProgressUpdatedEvent { MaxValue = archive.Count }; foreach (var entry in archive) { if (!entry.IsDirectory) { entry.WriteToDirectory(destination, new ExtractionOptions { ExtractFullPath = true, Overwrite = true, PreserveFileTime = true }); } progress.Value++; } }
/// <summary> /// /// </summary> /// <param name="origin"></param> /// <param name="destiny"></param> /// <returns></returns> public bool CopyFiles(List <string> origin, List <string> destiny) { if (!(origin.Count == destiny.Count)) { throw new ArgumentOutOfRangeException($"{nameof(origin)} - {nameof(destiny)} ", "The destiny files are less or more that the total of the origin files"); } long progress = 0; long total = origin.Size(); for (int i = 0; i < origin.Count; i++) { using (FileStream fsorigin = new FileStream(origin[i], FileMode.Open), fsdestiny = new FileStream(destiny[i], FileMode.Create)) { byte[] buffer = new byte[1048756]; int readBytes; while ((readBytes = fsorigin.Read(buffer, 0, buffer.Length)) > 0) { fsdestiny.Write(buffer, 0, readBytes); progress += readBytes; ProgressUpdatedEvent?.Invoke(this, new FileProgressUpdatedArgs(origin[i], destiny[i], total, progress)); } } } return(true); }
public void should_return_correct_percent() { var state = new ProgressUpdatedEvent { Value = 10, MaxValue = 20 }; state.GetPercentCompleted().Should().Be(50); }
/// <summary> /// /// </summary> /// <param name="origin"></param> /// <param name="destiny"></param> /// <returns></returns> public bool CopyFile(string origin, string destiny) { using (FileStream fsorigin = new FileStream(origin, FileMode.Open), fsdestiny = new FileStream(destiny, FileMode.Create)) { byte[] buffer = new byte[1048756]; int readBytes; while ((readBytes = fsorigin.Read(buffer, 0, buffer.Length)) > 0) { fsdestiny.Write(buffer, 0, readBytes); ProgressUpdatedEvent?.Invoke(this, new FileProgressUpdatedArgs(origin, destiny, fsorigin.Length, fsorigin.Position)); } } return(true); }
private void OnProgressUpdated(ProgressUpdatedEvent e) { var newPercent = e.GetPercentCompleted(); if (newPercent < 99 && _currentProgressState + 1 > newPercent) { return; } ProgressStatus = $"Downloading {AppSession.CurrentManifest.Name} {AppSession.CurrentManifest.Version}"; _currentProgressState = newPercent; Progress = (long)newPercent; DetailedStatus = e.Value.ToFileSize() + " / " + e.MaxValue.ToFileSize(); }
public async Task TransferFile(string source, string destinationFile, Action <ProgressUpdatedEvent> progressCallback) { using (var resp = await _httpClient.GetAsync(new Uri(source), Timeout.InfiniteTimeSpan, HttpCompletionOption.ResponseHeadersRead)) { var contentType = resp.Content.Headers.ContentType; if (contentType != null && contentType.MediaType.Contains("text")) { throw new InvalidDownloadUrlException(source, $"[ContentType={contentType.MediaType}]"); } if (_fileSystem.FileExists(destinationFile)) { _fileSystem.DeleteFile(destinationFile); } var maxValue = resp.Content.Headers.ContentLength ?? 0; var progress = new ProgressUpdatedEvent { MaxValue = maxValue }; using (var httpStream = await resp.Content.ReadAsStreamAsync()) { var tempFile = $"{destinationFile}.APPGET_DOWNLOAD"; using (var tempFileStream = _fileSystem.Open(tempFile, FileMode.Create, FileAccess.ReadWrite)) { var buffer = new byte[BUFFER_LENGTH]; int len; while ((len = httpStream.Read(buffer, 0, BUFFER_LENGTH)) > 0) { tempFileStream.Write(buffer, 0, len); progress.Value += len; progressCallback(progress); } } progress.IsCompleted = true; progressCallback(progress); _fileSystem.Move(tempFile, destinationFile); } } }