diff --git a/CheckDownload.csproj b/CheckDownload.csproj
index 2bd9ea8..710a0d0 100644
--- a/CheckDownload.csproj
+++ b/CheckDownload.csproj
@@ -184,8 +184,8 @@
-
+
-
+
\ No newline at end of file
diff --git a/Form1.Designer.cs b/Form1.Designer.cs
index 86a5beb..493cfc2 100644
--- a/Form1.Designer.cs
+++ b/Form1.Designer.cs
@@ -32,6 +32,7 @@
this.Update_Pro = new System.Windows.Forms.ProgressBar();
this.Status_Box = new System.Windows.Forms.Label();
this.Count_Box = new System.Windows.Forms.Label();
+ this.Size_Box = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// Update_Text
@@ -55,24 +56,33 @@
this.Status_Box.AutoSize = false;
this.Status_Box.Location = new System.Drawing.Point(72, 12);
this.Status_Box.Name = "Status_Box";
- this.Status_Box.Size = new System.Drawing.Size(213, 12);
+ this.Status_Box.Size = new System.Drawing.Size(94, 12);
this.Status_Box.TabIndex = 2;
this.Status_Box.Text = "...";
//
// Count_Box
//
- this.Count_Box.Location = new System.Drawing.Point(291, 12);
+ this.Count_Box.Location = new System.Drawing.Point(296, 12);
this.Count_Box.Name = "Count_Box";
- this.Count_Box.Size = new System.Drawing.Size(60, 13);
+ this.Count_Box.Size = new System.Drawing.Size(55, 13);
this.Count_Box.TabIndex = 3;
this.Count_Box.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
+ // Size_Box
+ //
+ this.Size_Box.Location = new System.Drawing.Point(166, 12);
+ this.Size_Box.Name = "Size_Box";
+ this.Size_Box.Size = new System.Drawing.Size(130, 13);
+ this.Size_Box.TabIndex = 4;
+ this.Size_Box.TextAlign = System.Drawing.ContentAlignment.TopRight;
+ //
// Update
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(363, 73);
this.ControlBox = false;
+ this.Controls.Add(this.Size_Box);
this.Controls.Add(this.Count_Box);
this.Controls.Add(this.Status_Box);
this.Controls.Add(this.Update_Pro);
@@ -96,6 +106,7 @@
private System.Windows.Forms.ProgressBar Update_Pro;
private System.Windows.Forms.Label Status_Box;
private System.Windows.Forms.Label Count_Box;
+ private System.Windows.Forms.Label Size_Box;
}
}
diff --git a/Form1.cs b/Form1.cs
index 1d96a85..397a7be 100644
--- a/Form1.cs
+++ b/Form1.cs
@@ -149,6 +149,22 @@ namespace CheckDownload
}
}
+ ///
+ /// 更新大小显示文本(线程安全)
+ ///
+ /// 大小值
+ private void UpdateSize(string sizeMessage)
+ {
+ if (this.InvokeRequired)
+ {
+ this.Invoke((MethodInvoker)delegate { Size_Box.Text = sizeMessage; });
+ }
+ else
+ {
+ Size_Box.Text = sizeMessage;
+ }
+ }
+
///
/// 窗体加载事件处理,启动更新流程
///
@@ -183,6 +199,7 @@ namespace CheckDownload
CleanupNewFiles();
UpdateStatus("下载在线MD5文件并读取...");
UpdateCount("");
+ UpdateSize("");
string tempFilePath = Path.Combine(_tempDirectory, Md5File);
// 使用新的带123盘的下载方法
if (!await DownloadMd5FileWithFallback(Md5File, tempFilePath))
@@ -210,6 +227,7 @@ namespace CheckDownload
{
UpdateStatus("所有文件都是最新的,无需更新");
UpdateCount("");
+ UpdateSize("");
UpdateProgressValue(100);
// 无需更新时清理临时文件夹
@@ -238,11 +256,13 @@ namespace CheckDownload
{
UpdateStatus($"有 {failedFiles.Count} 个文件下载失败,开始重试...");
UpdateCount("");
+ UpdateSize("");
var stillFailing = await RetryFailedFilesAsync(new Dictionary(failedFiles));
if (stillFailing.Any())
{
UpdateStatus($"重试后仍有 {stillFailing.Count} 个文件下载失败。");
UpdateCount("");
+ UpdateSize("");
}
}
@@ -507,7 +527,7 @@ namespace CheckDownload
using (var fileStream = File.Create(tempFilePath))
{
- await obj.Content.CopyToAsync(fileStream);
+ await DownloadWithProgressAsync(obj.Content, fileStream, obj.Metadata.ContentLength);
}
return true;
}
@@ -515,6 +535,7 @@ namespace CheckDownload
{
UpdateStatus($"{fileName}");
UpdateCount($"{_completedCount + 1}/{_totalCount}");
+ UpdateSize("");
string ossKey = $"File/{expectedMd5}";
return await DownloadFileWithFallback(ossKey, tempFilePath);
}
@@ -587,11 +608,13 @@ namespace CheckDownload
{
UpdateStatus($"正在从123盘下载: {fileName}");
UpdateCount("");
+ UpdateSize("");
string authUrl = GenerateAuthUrl($"http://{OneDriveMainDomain}{OneDrivePath}", fileName);
UpdateStatus($"使用主域名下载文件...");
UpdateCount("");
+ UpdateSize("");
var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 };
request.Headers.Add("User-Agent", "CheckDownload/1.0");
@@ -608,11 +631,12 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
- await remote.CopyToAsync(localFile);
+ await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
UpdateStatus($"123盘主域名下载成功: {fileName}");
UpdateCount("");
+ UpdateSize("");
return true;
}
}
@@ -620,17 +644,20 @@ namespace CheckDownload
{
UpdateStatus($"123盘主域名下载失败,尝试备用域名: {fileName}");
UpdateCount("");
+ UpdateSize("");
}
try
{
UpdateStatus($"正在从123盘备用域名下载: {fileName}");
UpdateCount("");
+ UpdateSize("");
string authUrl = GenerateAuthUrl($"http://{OneDriveBackupDomain}{OneDrivePath}", fileName);
UpdateStatus($"使用备用域名下载文件...");
UpdateCount("");
+ UpdateSize("");
var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 };
request.Headers.Add("User-Agent", "CheckDownload/1.0");
@@ -647,11 +674,12 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
- await remote.CopyToAsync(localFile);
+ await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
UpdateStatus($"123盘备用域名下载成功: {fileName}");
UpdateCount("");
+ UpdateSize("");
return true;
}
}
@@ -692,7 +720,7 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
- await remote.CopyToAsync(localFile);
+ await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
return true;
@@ -721,7 +749,7 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
- await remote.CopyToAsync(localFile);
+ await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
return true;
@@ -746,6 +774,7 @@ namespace CheckDownload
{
UpdateStatus($"第 {i + 1} 次重试,剩余 {filesToRetry.Count} 个文件...");
UpdateCount("");
+ UpdateSize("");
var failedThisRound = new ConcurrentDictionary();
await PerformDownloads(filesToRetry, failedThisRound);
@@ -756,6 +785,7 @@ namespace CheckDownload
{
UpdateStatus($"等待 3 秒后进行下一次重试...");
UpdateCount("");
+ UpdateSize("");
await Task.Delay(3000);
}
}
@@ -808,6 +838,7 @@ namespace CheckDownload
{
UpdateStatus("正在校验文件...");
UpdateCount("");
+ UpdateSize("");
var failedFiles = new ConcurrentBag();
var filesForScripting = new ConcurrentBag<(string original, string newFile)>();
@@ -875,6 +906,7 @@ namespace CheckDownload
{
UpdateStatus("所有文件校验和保存成功");
UpdateCount("");
+ UpdateSize("");
UpdateProgressValue(100);
}
}
@@ -1039,7 +1071,7 @@ namespace CheckDownload
}
using (var fileStream = File.Create(localPath))
{
- obj.Content.CopyTo(fileStream);
+ await DownloadWithProgressAsync(obj.Content, fileStream, obj.Metadata.ContentLength);
}
return true;
}
@@ -1083,7 +1115,7 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
- await remote.CopyToAsync(localFile);
+ await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
}
return true;
@@ -1398,5 +1430,49 @@ namespace CheckDownload
return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLowerInvariant();
}
}
+
+ ///
+ /// 异步将远程数据流写入本地文件流,并实时更新下载进度
+ ///
+ /// 远程数据源的流
+ /// 本地文件的流
+ /// 文件的总字节数,用于计算进度
+ private async Task DownloadWithProgressAsync(Stream remoteStream, Stream localStream, long? totalBytes)
+ {
+ long totalRead = 0;
+ byte[] buffer = new byte[8192]; // 8KB 缓冲区
+ int bytesRead;
+
+ while ((bytesRead = await remoteStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
+ {
+ await localStream.WriteAsync(buffer, 0, bytesRead);
+ totalRead += bytesRead;
+ if (totalBytes.HasValue)
+ {
+ string progressText = $"{FormatBytes(totalRead)}/{FormatBytes(totalBytes.Value)}";
+ UpdateSize(progressText);
+ }
+ }
+ }
+
+ ///
+ /// 将字节大小格式化为更易读的单位(B, KB, MB, GB, TB)
+ ///
+ /// 要格式化的字节数
+ /// 格式化后的字符串
+ private static string FormatBytes(long bytes)
+ {
+ string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
+ int i = 0;
+ double dblSByte = bytes;
+ if (bytes > 1024)
+ {
+ for (i = 0; (bytes / 1024) > 0 && i < suffixes.Length - 1; i++, bytes /= 1024)
+ {
+ dblSByte = bytes / 1024.0;
+ }
+ }
+ return $"{dblSByte:0.##}{suffixes[i]}";
+ }
}
}
\ No newline at end of file
diff --git a/packages.config b/packages.config
index e332f38..50677d8 100644
--- a/packages.config
+++ b/packages.config
@@ -3,7 +3,7 @@
-
+