编写异步压缩进度

时间:2019-03-18 15:45:04

标签: c# asynchronous compression task

我有此代码:

using (FileStream sourceStream = new FileStream(sourceFile, FileMode.OpenOrCreate))
{
    using (FileStream targetStream = File.Create(compressedFile))
    {
        using (GZipStream compressionStream = new GZipStream(targetStream, CompressionMode.Compress))
        {
            sourceStream.CopyTo(compressionStream);
            Console.WriteLine(
                "Compress of file{0} is end. Inintial size: {1}  compressed size: {2}.",
                sourceFile,
                sourceStream.Length.ToString(),
                targetStream.Length.ToString());
        }
    }
}

现在,我需要异步编写compressiong文件的进度。我该怎么办?

1 个答案:

答案 0 :(得分:0)

首先找到源文件的大小。然后,进度是到目前为止消耗的源文件中的输入字节数除以源文件的大小。