增加Progress的IsFinished函数;修改解压ZIP的重试逻辑以及中途取消解压的参数
This commit is contained in:
5 files changed
+35
-29
No files matched your search
@@ -159,16 +159,18 @@ public class ProgressProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsFinished => GetRaw().actual + GetRaw().skiped > 0.9999999;
|
||||||
|
|
||||||
private (double actual, double skiped) observedProgress = (0, 0);
|
private (double actual, double skiped) observedProgress = (0, 0);
|
||||||
private double incrementProgress = 0;
|
private double incrementProgress = 0;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取一个在多次观测之间必定单调递增的进度值,范围为 [0, 1]。
|
/// 获取一个在多次观测之间必定单调递增的进度值,范围为 [0, 1]。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double GetIncrement() {
|
public double GetIncreasing() {
|
||||||
lock (this) {
|
lock (this) {
|
||||||
(double actual, double skiped) current = GetRaw();
|
(double actual, double skiped) current = GetRaw();
|
||||||
if (current == observedProgress) return incrementProgress; // 未改变
|
if (current == observedProgress) return incrementProgress; // 未改变
|
||||||
if (current.actual + current.skiped > 0.9999999) { // 已完成
|
if (IsFinished) { // 已完成
|
||||||
incrementProgress = 1;
|
incrementProgress = 1;
|
||||||
} else if (current.actual >= observedProgress.actual) { // 进度增加
|
} else if (current.actual >= observedProgress.actual) { // 进度增加
|
||||||
incrementProgress += (1 - incrementProgress) *
|
incrementProgress += (1 - incrementProgress) *
|
||||||
@@ -197,7 +199,7 @@ public class ProgressObserver : RateLimitedWorker {
|
|||||||
}
|
}
|
||||||
private void StartObserver((double actual, double skiped) raw) => Start();
|
private void StartObserver((double actual, double skiped) raw) => Start();
|
||||||
private static void Observe(ProgressProvider provider, Action<double> updateAction) {
|
private static void Observe(ProgressProvider provider, Action<double> updateAction) {
|
||||||
double progress = provider.GetIncrement();
|
double progress = provider.GetIncreasing();
|
||||||
// TODO: 添加平缓过渡
|
// TODO: 添加平缓过渡
|
||||||
updateAction(progress);
|
updateAction(progress);
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-17
@@ -325,7 +325,7 @@ public static class FileUtils {
|
|||||||
/// 以只读模式打开压缩文件。
|
/// 以只读模式打开压缩文件。
|
||||||
/// 会先尝试 UTF8 编码,失败后换用 GB18030 编码。
|
/// 会先尝试 UTF8 编码,失败后换用 GB18030 编码。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static ZipArchive OpenZip(string zipFilePath) {
|
public static ZipArchive OpenZip(string zipFilePath, CancellationToken c = default) {
|
||||||
ZipArchive TryOpen(Encoding encoding) {
|
ZipArchive TryOpen(Encoding encoding) {
|
||||||
Logger.Trace($"尝试以 {encoding.EncodingName} 编码打开压缩包:{zipFilePath}");
|
Logger.Trace($"尝试以 {encoding.EncodingName} 编码打开压缩包:{zipFilePath}");
|
||||||
var result = ZipFile.Open(PathUtils.ForApi(zipFilePath), ZipArchiveMode.Read, encoding);
|
var result = ZipFile.Open(PathUtils.ForApi(zipFilePath), ZipArchiveMode.Read, encoding);
|
||||||
@@ -337,23 +337,26 @@ public static class FileUtils {
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
return Retrier.Attempt(delay: _ => TimeSpan.FromMilliseconds(200), maxAttempts:10, isRetryAllowed: ex => ex is IOException, cancellationToken: c, func: _ => {
|
||||||
try { // 尝试两种编码
|
try {
|
||||||
return TryOpen(new UTF8Encoding(false, true));
|
try { // 尝试两种编码
|
||||||
} catch (DecoderFallbackException) {
|
return TryOpen(new UTF8Encoding(false, true));
|
||||||
return TryOpen(Encoding.GetEncoding("GB18030"));
|
} catch (DecoderFallbackException) {
|
||||||
|
return TryOpen(Encoding.GetEncoding("GB18030"));
|
||||||
|
}
|
||||||
|
} catch (InvalidDataException ex) {
|
||||||
|
throw new InvalidDataException($"文件不是压缩包,或者文件已损坏({zipFilePath})", ex);
|
||||||
}
|
}
|
||||||
} catch (InvalidDataException ex) {
|
});
|
||||||
throw new InvalidDataException($"文件不是压缩包,或者文件已损坏({zipFilePath})", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 尝试根据后缀名判断文件种类并解压文件,支持 gz 与 zip,会尝试将 jar 以 zip 方式解压。
|
/// 尝试根据后缀名判断文件种类并解压文件,支持 gz 与 zip,会尝试将 jar 以 zip 方式解压。
|
||||||
/// 会自动创建文件夹。会覆盖已有文件,但不会删除多余文件。
|
/// 会自动创建文件夹。会覆盖已有文件,但不会删除多余文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="progressHandler">参数为已完成的总比例(0~1)。</param>
|
/// <param name="p">参数为已完成的总比例(0~1)。</param>
|
||||||
public static void ExtractToDirectory(string compressionFile, string outputDirectory, Action<double>? progressHandler = null) {
|
public static void ExtractToDirectory(string compressionFile, string outputDirectory, CancellationToken c = default, ProgressProvider? p = null) {
|
||||||
compressionFile = PathUtils.ForApi(compressionFile);
|
compressionFile = PathUtils.ForApi(compressionFile);
|
||||||
DirectoryUtils.Create(outputDirectory);
|
DirectoryUtils.Create(outputDirectory);
|
||||||
// 解压 gz(gz 不需要考虑编码)
|
// 解压 gz(gz 不需要考虑编码)
|
||||||
@@ -363,18 +366,19 @@ public static class FileUtils {
|
|||||||
using var fileStream = FileUtils.ReadAsStream(compressionFile);
|
using var fileStream = FileUtils.ReadAsStream(compressionFile);
|
||||||
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
|
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
|
||||||
FileUtils.Write(outFilePath, gzipStream);
|
FileUtils.Write(outFilePath, gzipStream);
|
||||||
progressHandler?.Invoke(1);
|
p?.Finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 解压 zip
|
// 解压 zip
|
||||||
using var archive = FileUtils.OpenZip(compressionFile);
|
using var archive = FileUtils.OpenZip(compressionFile, c);
|
||||||
int totalCount = archive.Entries.Count;
|
var entries = archive.Entries.Where(e => !string.IsNullOrEmpty(e.Name)).ToList(); // 跳过文件夹条目(ZipArchive 会将文件夹也作为一个 entry,但它们的 Name 为空)
|
||||||
|
int totalCount = entries.Count;
|
||||||
int doneCount = 0;
|
int doneCount = 0;
|
||||||
Logger.Trace($"解压 zip 文件:{compressionFile} → {outputDirectory}(共 {totalCount} 项)");
|
Logger.Trace($"解压 zip 文件:{compressionFile} → {outputDirectory}(共 {totalCount} 项)");
|
||||||
foreach (var entry in archive.Entries) {
|
foreach (var entry in entries) {
|
||||||
|
c.ThrowIfCancellationRequested();
|
||||||
doneCount++;
|
doneCount++;
|
||||||
if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount);
|
if (totalCount > 0) p?.Set((double) doneCount / totalCount);
|
||||||
if (string.IsNullOrEmpty(entry.Name)) continue; // 跳过文件夹条目(ZipArchive 会将文件夹也作为一个 entry,但它们的 Name 为空)
|
|
||||||
// ZipSlip 修复
|
// ZipSlip 修复
|
||||||
string outputFilePath = Path.Combine(outputDirectory, entry.FullName);
|
string outputFilePath = Path.Combine(outputDirectory, entry.FullName);
|
||||||
if (!PathUtils.IsParentOf(outputDirectory, outputFilePath))
|
if (!PathUtils.IsParentOf(outputDirectory, outputFilePath))
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public class ProgressTest : TestBase {
|
|||||||
progress.Set(0.75, skiped: true);
|
progress.Set(0.75, skiped: true);
|
||||||
|
|
||||||
await Assert.That(changedCount).IsEqualTo(2);
|
await Assert.That(changedCount).IsEqualTo(2);
|
||||||
await Assert.That(Math.Abs(progress.GetIncrement() - 2.0 / 3) < 0.000001).IsTrue();
|
await Assert.That(Math.Abs(progress.GetIncreasing() - 2.0 / 3) < 0.000001).IsTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ public class FileUtilsTest : TestWithFolder {
|
|||||||
[Arguments("GB Encoding.zip")]
|
[Arguments("GB Encoding.zip")]
|
||||||
[Arguments("UTF8 Encoding.zip")]
|
[Arguments("UTF8 Encoding.zip")]
|
||||||
public async Task 解压_ReadZip(string testFile) {
|
public async Task 解压_ReadZip(string testFile) {
|
||||||
double progress = 0;
|
|
||||||
string output = Path.Combine(tempFolder, "Extracted");
|
string output = Path.Combine(tempFolder, "Extracted");
|
||||||
FileUtils.ExtractToDirectory(GetTestFile(testFile), output, p => progress = p);
|
var p = new ProgressProvider();
|
||||||
await Assert.That(progress).IsEqualTo(1);
|
FileUtils.ExtractToDirectory(GetTestFile(testFile), output, p: p);
|
||||||
|
await Assert.That(p.IsFinished).IsTrue();
|
||||||
await Assert.That(DirectoryUtils.Exists(Path.Combine(output, "文件夹"))).IsTrue();
|
await Assert.That(DirectoryUtils.Exists(Path.Combine(output, "文件夹"))).IsTrue();
|
||||||
await Assert.That(DirectoryUtils.Exists(Path.Combine(output, "空文件夹"))).IsFalse();
|
await Assert.That(DirectoryUtils.Exists(Path.Combine(output, "空文件夹"))).IsFalse();
|
||||||
await Assert.That(File.ReadAllText(PathUtils.ToExtendedFormat(Path.Combine(output, "fabricloader.log")))).Contains("FabricLoader");
|
await Assert.That(File.ReadAllText(PathUtils.ToExtendedFormat(Path.Combine(output, "fabricloader.log")))).Contains("FabricLoader");
|
||||||
@@ -20,10 +20,10 @@ public class FileUtilsTest : TestWithFolder {
|
|||||||
[Test]
|
[Test]
|
||||||
[Arguments("GZ.gz", "LTCat")]
|
[Arguments("GZ.gz", "LTCat")]
|
||||||
public async Task 解压_ReadGz(string testFile, string containsText) {
|
public async Task 解压_ReadGz(string testFile, string containsText) {
|
||||||
double progress = 0;
|
|
||||||
string output = Path.Combine(tempFolder, "Extracted");
|
string output = Path.Combine(tempFolder, "Extracted");
|
||||||
FileUtils.ExtractToDirectory(GetTestFile(testFile), output, p => progress = p);
|
var p = new ProgressProvider();
|
||||||
await Assert.That(progress).IsEqualTo(1);
|
FileUtils.ExtractToDirectory(GetTestFile(testFile), output, p: p);
|
||||||
|
await Assert.That(p.IsFinished).IsTrue();
|
||||||
await Assert.That(File.ReadAllText(PathUtils.ToExtendedFormat(Path.Combine(output, PathUtils.GetFileNameWithoutExtension(testFile))))).Contains(containsText);
|
await Assert.That(File.ReadAllText(PathUtils.ToExtendedFormat(Path.Combine(output, PathUtils.GetFileNameWithoutExtension(testFile))))).Contains(containsText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ public class TaskUtilsTest : TestBase {
|
|||||||
], progress);
|
], progress);
|
||||||
|
|
||||||
await Assert.That(ReferenceEquals(firstProgress, secondProgress)).IsFalse();
|
await Assert.That(ReferenceEquals(firstProgress, secondProgress)).IsFalse();
|
||||||
await Assert.That(progress.GetIncrement()).IsEqualTo(0.5);
|
await Assert.That(progress.GetIncreasing()).IsEqualTo(0.5);
|
||||||
continueSecond.SetResult(null);
|
continueSecond.SetResult(null);
|
||||||
var results = await whenAll;
|
var results = await whenAll;
|
||||||
await Assert.That(string.Join(",", results)).IsEqualTo("1,2");
|
await Assert.That(string.Join(",", results)).IsEqualTo("1,2");
|
||||||
await Assert.That(progress.GetIncrement()).IsEqualTo(1);
|
await Assert.That(progress.GetIncreasing()).IsEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user