SharpZipLib - 提取过程中文件的进度

发布于 2024-08-15 21:36:26 字数 385 浏览 3 评论 0原文

这必须非常简单,而且这似乎是一个非常常见的问题,但我一生都无法找到“直接”的答案。

我想创建一个进度条来显示 SharpZipLib 提取的 Zip 文件。

FastZip 和 FastZipEvents 类提供单个文件的进度,但不提供整个 Zip 中的位置。也就是说,如果Zip包含200个文件,当前正在提取什么文件。我不关心单个文件的进度(例如 Foo.txt 中的 20KB 到 43KB)。

我想我可以通过首先创建一个 ZipFile 并访问 Count 属性来捏造一种方法。然后...使用 ZipInputStream 或 FastZip 来提取并保持进度计数,但我认为这意味着 Zip 实际上被解压缩了两次(一次完全进入内存),我不喜欢这样。

有什么干净的方法可以做到这一点吗?

this has to be really easy, and it certainly seems to be a very frequently asked question, but I can't for the life of me find a 'straightforward' answer.

I want to create a ProgressBar that shows a Zip file being extracted by SharpZipLib.

The FastZip and FastZipEvents classes give progress on individual files but not on position within the overall Zip. That is, if there Zip contains 200 files, what file is currently being extracted. I don't care about the progress through individual files (e.g. 20KB through 43KB in Foo.txt).

I think I could fudge a way of doing this by first creating a ZipFile and to access the Count property. And then... using ZipInputStream or FastZip to extract and keep progress count myself but I think that means the Zip is effectively unzipped twice (once entirely into memory) and I don't like that.

Any clean way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-08-22 21:36:26

关于你的最后一句话:“我认为这意味着 Zip 实际上被解压了两次”。

读取 zip 文件的内容表根本不需要花费太多(并且不会访问所包含的文件。您可能注意到,当您查看带有“密码”的 zip 文件时,只需要输入密码您尝试提取文件。您可以查看条目/内容表)。

因此,我认为首先检查索引/内容表、存储条目计数(甚至可能压缩/未压缩大小?)并稍后使用基于流的 api 的方法没有任何问题。

Regarding your last sentence: "I think that means the Zip is effectively unzipped twice".

Reading the content table of a zip file doesn't cost a lot at all (and doesn't access the contained files. You probably noticed that when you looked at a zip file with a "password" and only needed to enter the password when you tried to extract a file. You can look at the entries/content table just fine).

So I see nothing wrong with the approach of first checking the index/content table, storing the entry count (maybe even with compressed/uncompressed size?) and using the stream based api later.

红ご颜醉 2024-08-22 21:36:26

仅供参考: DotNetZip 有针对此类事件的 ExtractProgress 事件。代码:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  zip.ExtractProgress = MyExtractProgress; 
  zip.ExtractAll(TargetDirectory);    
}

extractprogress 处理程序如下所示:

    private void MyExtractProgress(object sender, ExtractProgressEventArgs e)
    {
        switch (e.EventType)
        {
            case ZipProgressEventType.Extracting_BeforeExtractEntry:
            ....
            case ZipProgressEventType.Extracting_EntryBytesWritten:
            ...
            case ZipProgressEventType.Extracting_AfterExtractEntry:
            ....
        }
    }

您可以使用它来驱动熟悉的 2 进度条 UI,其中一个条显示存档的进度,另一个条显示存档中单个文件的进度。

FYI: DotNetZip has ExtractProgress event for this sort of thing. Code:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  zip.ExtractProgress = MyExtractProgress; 
  zip.ExtractAll(TargetDirectory);    
}

The extractprogress handler looks like this:

    private void MyExtractProgress(object sender, ExtractProgressEventArgs e)
    {
        switch (e.EventType)
        {
            case ZipProgressEventType.Extracting_BeforeExtractEntry:
            ....
            case ZipProgressEventType.Extracting_EntryBytesWritten:
            ...
            case ZipProgressEventType.Extracting_AfterExtractEntry:
            ....
        }
    }

You could use it to drive the familiar 2-progressbar UI, with one bar showing progress for the archive, and another bar showing progress for the individual file within the archive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文