C#WPF,Async文件夹解压缩以及取消支持和进度

时间:2018-11-20 10:14:06

标签: c# wpf async-await zip

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

到目前为止,它很好用,但是我想增加在UI中显示进度并使其可随时取消的可能性。

我已经设法通过下载部分完成了操作,但没有通过减压完成操作。

我真的不在乎我的文件是zip格式还是其他格式。我只想将Azure上的文件夹发送为单个文件并尽可能小。我也想避免现在支付nuget库进行压缩。

这是我到目前为止所做的:

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;
        }
    }

1 个答案:

答案 0 :(得分:0)

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

调用方法后,您将无法取消该操作,并且不确定是否要完成该操作。

您最好的办法是在共压缩文件期间显示一个中间文件(将IsIntermediate设置为trueProgressBar