From b70c300df0edf0acd985e677a12c98d5858bc216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E7=87=8F?= <103927505+ZiYuKing@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:49:41 +0800 Subject: [PATCH] Increase Download Progress Prompt --- SwitchGiftDataManager.WinForm/MgdbForm.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/SwitchGiftDataManager.WinForm/MgdbForm.cs b/SwitchGiftDataManager.WinForm/MgdbForm.cs index 1a311a2..83434ef 100644 --- a/SwitchGiftDataManager.WinForm/MgdbForm.cs +++ b/SwitchGiftDataManager.WinForm/MgdbForm.cs @@ -38,17 +38,31 @@ private async Task DownloadRepoMGDB() Directory.CreateDirectory(path); var zipPath = Path.Combine(path, "tmp.zip"); - using (var response = await client.GetAsync(url)) + using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) { + var totalBytes = response.Content.Headers.ContentLength ?? 0; using (var content = await response.Content.ReadAsStreamAsync()) + using (var stream = new FileStream(zipPath, FileMode.Create, FileAccess.Write, FileShare.None)) { - using (var stream = new FileStream(zipPath, FileMode.Create, FileAccess.Write, FileShare.None)) + var buffer = new byte[8192]; + int bytesRead; + long totalRead = 0; + while ((bytesRead = await content.ReadAsync(buffer, 0, buffer.Length)) > 0) { - await content.CopyToAsync(stream); + await stream.WriteAsync(buffer, 0, bytesRead); + totalRead += bytesRead; + // Calculate and display progress + if (totalBytes > 0) + { + double progressPercentage = (double)totalRead / totalBytes * 100; + // Update UI or log progress here + lblMessage.Text = $"Download progress: {progressPercentage:F2}%"; + } } } } + ZipFile.ExtractToDirectory(zipPath, path); File.Delete(zipPath);