Skip to content

Commit

Permalink
简化代码结构并改进文件处理逻辑
Browse files Browse the repository at this point in the history
删除了对 `overwrite` 变量的检查和相关的条件分支,使得在文件存在时总是删除文件。
将 `Stopwatch` 计时器的启动和停止以及下载数据的代码块移到了外层,去掉了不必要的嵌套。
将下载数据为空的检查和日志记录移到了外层,去掉了不必要的嵌套。
将下载完成的日志记录移到了外层,去掉了不必要的嵌套。
在文件写入后,调用 `Extract` 方法时增加了 `overwrite` 参数。
  • Loading branch information
猿人易 committed Dec 6, 2024
1 parent 1d33f2f commit 3099479
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions Pek.AspNetCore/Helpers/DHWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,27 +830,24 @@ public static async Task DownloadLinkAndExtract(String url, String name, String
var saveFile = savePath.AsFile();
if (saveFile.Exists)
{
if (!overwrite)
{
saveFile.Delete();
saveFile.Delete();
}

var sw = Stopwatch.StartNew();
var responseData = await Client().Get(UrlHelper.Combine(url, name)).DownloadDataAsync().ConfigureAwait(false);
sw.Stop();
var sw = Stopwatch.StartNew();
var responseData = await Client().Get(UrlHelper.Combine(url, name)).DownloadDataAsync().ConfigureAwait(false);
sw.Stop();

if (responseData == null || responseData.Length == 0)
{
XTrace.WriteLine($"下载{name}失败");
return;
}
if (responseData == null || responseData.Length == 0)
{
XTrace.WriteLine($"下载{name}失败");
return;
}

XTrace.Log.Info("下载完成,共{0:n0}字节,耗时{1:n0}毫秒", saveFile.Length, sw.ElapsedMilliseconds);
XTrace.Log.Info("下载完成,共{0:n0}字节,耗时{1:n0}毫秒", saveFile.Length, sw.ElapsedMilliseconds);

FileUtil.Write(savePath, responseData!); // 保存文件
}
}

savePath.AsFile().Extract(destdir);
FileUtil.Write(savePath, responseData!); // 保存文件

savePath.AsFile().Extract(destdir, overwrite);

XTrace.Log.Info("解压缩到 {0}", destdir);
}
Expand Down

0 comments on commit 3099479

Please sign in to comment.