将同步zip操作转换为异步 [英] Convert synchronous zip operation to async

查看:134
本文介绍了将同步zip操作转换为异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个现有的库,其中的某些方法需要转换为异步方法.

We got an existing library where some of the methods needs to be converted to async methods.

但是,我不确定如何使用以下方法进行操作(错误处理已删除).该方法的目的是压缩文件并将其保存到磁盘. (请注意,zip类不会公开任何异步方法.)

However I'm not sure how to do it with the following method (errorhandling has been removed). The purpose of the method is to zip a file and save it to disk. (Note that the zip class doesn't expose any async methods.)

public static bool ZipAndSaveFile(string fileToPack, string archiveName, string outputDirectory)
{
    var archiveNameAndPath = Path.Combine(outputDirectory, archiveName);
    using (var zip = new ZipFile())
    {
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zip.Comment = $"This archive was created at {System.DateTime.UtcNow.ToString("G")} (UTC)";
        zip.AddFile(fileToPack);
        zip.Save(archiveNameAndPath);
    }
    return true;
}

一个实现可能看起来像这样:

An implementation could look like this:

public static async Task<bool> ZipAndSaveFileAsync(string fileToPack, string archiveName, string outputDirectory)
{
    var archiveNameAndPath = Path.Combine(outputDirectory, archiveName);
    await Task.Run(() => 
    {
        using (var zip = new ZipFile())
        {
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zip.Comment = $"This archive was created at {System.DateTime.UtcNow.ToString("G")} (UTC)";
            zip.AddFile(fileToPack);
            zip.Save(archiveNameAndPath);
        }
    });
    return true;
}

这似乎是错误的.客户端可以只使用Task.Run

Which just seems wrong. The client could just call the sync method using Task.Run

请,有人对如何将其转换为异步方法有任何提示吗?

Please, anyone got any hints on how to transform it into a async method ?

推荐答案

作为一种临时解决方案,我将介绍一种扩展方法:

As a temporarily solution, I would introduce an extension method:

public static class ZipFileExtensions
{
    public static Task SaveAsync(this ZipFile zipFile, string filePath)
    {
        zipFile.Save(filePath);
        return Task.FromResult(true);
    }
}

那么用法是:

public static async Task<bool> ZipAndSaveFileAsync(string fileToPack, string  archiveName, string outputDirectory)
{
    var archiveNameAndPath = Path.Combine(outputDirectory, archiveName);
    using (var zip = new ZipFile())
    {
        ...
        await zip.SaveAsync(archiveNameAndPath).ConfugureAwait(false);
    }
    return true;
 }

  1. 实施同步任务不会违反任何规定(谈论Task.FromResult)
  2. https://github.com/jstedfast/Ionic.Zlib 提交请求由于IO操作而在库中提供异步支持
  3. 希望最终完成,然后您可以在应用程序中升级Ionic.Zlib,删除ZipFileExtensions,然后继续使用异步版本的Save方法(这次已内置在库中).
  4. 或者,您可以从GitHub克隆存储库,然后自己添加SaveAsync,然后提交回显请求.
  5. 如果库不支持,将同步方法转换"为异步方法是不可能的.
  1. Implementing synchronous tasks does not violate anything (talking about Task.FromResult)
  2. Submit a request to https://github.com/jstedfast/Ionic.Zlib asking for an async support in the library due to IO operations
  3. Hope that's done eventually, and then you can upgrade the Ionic.Zlib in your app, delete the ZipFileExtensions, and continue using async version of the Save method (this time built into the library).
  4. Alternatively, you can clone the repo from GitHub, and add SaveAsync by yourself, the submit a pull request back.
  5. It's just not possible to 'convert' a sync method to an async if a library does not support it.

从性能的角度来看,这可能不是最佳解决方案,但是从管理的角度来看,您可以将故事将一切转换为异步"和通过使Ionic.Zlib异步提高应用性能"分离开来,这使您的待办事项更多粒状.

From performance standpoint, this might not be the best solution, but from management point of view, you can decouple stories "Convert everything to async" and "Improve app performance by having Ionic.Zlib async", what makes your backlog more granular.

这篇关于将同步zip操作转换为异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆