使用进度条提取存档? [英] Extract an archive with progress bar?

查看:15
本文介绍了使用进度条提取存档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下我如何使用进度条?

How i can use an progress bar in this case?

void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    //System.Windows.MessageBox.Show("Update Complete!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
    Uri uri = new Uri(url);
    string filename = System.IO.Path.GetFileName(uri.AbsolutePath);
    ZipFile.ExtractToDirectory(filePathDir + "/" + filename, filePathDir);
}

@Alessandro D'Andria ,但在这种情况下?:

@Alessandro D'Andria , But in this case?:

WebClient wc = new WebClient();
Stream zipReadingStream = wc.OpenRead(url);
ZipArchive zip = new ZipArchive(zipReadingStream);
ZipFileExtensions.ExtractToDirectory(zip, filePathDir);

推荐答案

可以查看ExtractToDirectory on GitHub,你唯一需要做的就是传入一个 Progress 并在 foreach 中调用它循环.

You can see the source of ExtractToDirectory on GitHub, the only thing you need to do is pass in a Progress<ZipProgress> and call it inside the foreach loop.

//This is a new class that represents a progress object.
public class ZipProgress
{
    public ZipProgress(int total, int processed, string currentItem)
    {
        Total = total;
        Processed = processed;
        CurrentItem = currentItem;
    }
    public int Total { get; }
    public int Processed { get; }
    public string CurrentItem { get; }
}

public static class MyZipFileExtensions
{
    public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress<ZipProgress> progress)
    {
        ExtractToDirectory(source, destinationDirectoryName, progress, overwrite: false);
    }

    public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress<ZipProgress> progress, bool overwrite)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        if (destinationDirectoryName == null)
            throw new ArgumentNullException(nameof(destinationDirectoryName));


        // Rely on Directory.CreateDirectory for validation of destinationDirectoryName.

        // Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists:
        DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
        string destinationDirectoryFullPath = di.FullName;

        int count = 0;
        foreach (ZipArchiveEntry entry in source.Entries)
        {
            count++;
            string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, entry.FullName));

            if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
                throw new IOException("File is extracting to outside of the folder specified.");

            var zipProgress = new ZipProgress(source.Entries.Count, count, entry.FullName);
            progress.Report(zipProgress);

            if (Path.GetFileName(fileDestinationPath).Length == 0)
            {
                // If it is a directory:

                if (entry.Length != 0)
                    throw new IOException("Directory entry with data.");

                Directory.CreateDirectory(fileDestinationPath);
            }
            else
            {
                // If it is a file:
                // Create containing directory:
                Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath));
                entry.ExtractToFile(fileDestinationPath, overwrite: overwrite);
            }
        }
    }
}

这就像使用

public class YourClass
{
    public Progress<ZipProgress> _progress;

    public YourClass()
    {
        // Create the progress object in the constructor, it will call it's ReportProgress using the sync context it was constructed on.
        // If your program is a UI program that means you want to new it up on the UI thread.
        _progress = new Progress<ZipProgress>();
        _progress.ProgressChanged += Report
    }

    private void Report(object sender, ZipProgress zipProgress)
    {
        //Use zipProgress here to update the UI on the progress.
    }

    //I assume you have a `Task.Run(() => Download(url, filePathDir);` calling this so it is on a background thread.
    public void Download(string url, string filePathDir)
    {
        WebClient wc = new WebClient();
        Stream zipReadingStream = wc.OpenRead(url);
        ZipArchive zip = new ZipArchive(zipReadingStream);
        zip.ExtractToDirectory(filePathDir, _progress);
    }

    //...

这篇关于使用进度条提取存档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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