C#WPF,异步文件夹解压缩与取消支持和进展



我正试图在WPF客户端中创建一个方法,该方法从azure下载zip文件并将其解压缩到文件夹中。

到目前为止,它工作得很好,但我想添加一种可能性,在UI中显示进度,并使其随时可取消。

我已经设法完成了下载部分,但没有进行解压缩。

我真的不在乎我的文件是zip格式还是其他格式。我只想在azure上发送尽可能小的单个文件的文件夹。我现在还想避免为压缩支付nuget-libs的费用。

以下是我迄今为止所做的:

public async Task Download()
{
if (!IsDownloading)
try
{
IsDownloading = true;
//Step 1: Check local presence
//TODO
State = CIState.Downloading;
StateTooltip = "Downloading";
//Step 2: Download from azure blob
var sasUri = await cloudTransferService.GetDownloadSasUri(GetCIType());
if (await cloudTransferService.BlobExists(CI.ID, sasUri))
{
cloudTransferService.OnProgressMade += CloudTransferService_OnProgressMade;
Cancelable = true;
await cloudTransferService.DownloadCIAsync(CI.ID, sasUri, Settings.Default.ProjectLocalPath, cancellationTokenSourceource);
Cancelable = false; //Disable cancel button as for now decompression cannot be cancelled
if(!canceled)
{
StateTooltip = $"Uncompressing content";
var path = await fileCompressionService.UncompressCI(Settings.Default.ProjectLocalPath + @"" + CI.ID.ToString() + ".zip");
localStorageManagementService.DeleteFile(Settings.Default.ProjectLocalPath + @"" + CI.ID.ToString() + ".zip");
StateTooltip = $"Done";
}
}
else
{
Cancelable = false;
State = CIState.Error;
StateTooltip = "No content found";
}
//step 3: Add to local library
//TODO

IsDownloading = false;
Cancelable = false;
}
catch (System.Threading.Tasks.TaskCanceledException)
{
IsDownloading = false;
State = CIState.AvailableToDownload;
Cancelable = false;
}
catch (System.Exception e)
{
Cancelable = false;
StateTooltip = "Error";
State = CIState.AvailableToDownload;
IsDownloading = false;
}
}

在文件压缩服务中:

public async Task<string> UncompressCI(string zipFile)
{
try
{
var ProjectPath = Path.Combine(Path.GetDirectoryName(zipFile),Path.GetFileNameWithoutExtension(zipFile));
if (Directory.Exists(ProjectPath)) Directory.Delete(ProjectPath,true);
await Task.Run(() =>
{
try
{
ZipFile.ExtractToDirectory(zipFile,ProjectPath );
}
catch (Exception e)
{
//throw e;
}
}).ConfigureAwait(false);
if (!Directory.Exists(ProjectPath))
throw new Exception( "Cannot find the uncompressed folder");
return zipFile;
}
catch (Exception e)
{
throw e;
}
}

API的实现必须向调用方报告其进度并支持取消,以便您能够跟踪其进度并取消它。ZipFile.ExtractToDirectory方法两者都不支持。

一旦调用方法,就无法取消操作,也不确定它需要多长时间才能完成。

您可以做的最好的事情是在共压缩文件的时间内显示一个中间值(将IsIntermediate设置为true(ProgressBar

相关内容

最新更新