简体   繁体   English

目录上的C#DotNetZip进度栏

[英]C# DotNetZip progressbar on directory

I have the following code working for a single file zip without any problems. 我有以下代码可用于单个文件zip,没有任何问题。 But when I zip (create) from a directory the progressbar goes nuts. 但是,当我从目录中压缩(创建)时,进度栏变得很疯狂。

Basically, the progressbar goes back and forward nonstop. 基本上,进度条会不停地前进和后退。 The image illustrates. 该图说明。 在此处输入图片说明

Inside the folder selected I might have like another 10 sub-folders. 在选定的文件夹中,我可能会喜欢另外10个子文件夹。

using (ZipFile zip = new ZipFile())
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zip.SaveProgress += zipProgress;

    zip.AddDirectory(folderPath);
    zip.Save(tempPath);
}

private void zipProgress(object sender, SaveProgressEventArgs e)
{
    if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
        this.progressbar1.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);

    else if (e.EventType == ZipProgressEventType.Saving_Completed)
        this.progressbar1.Value = 100;
}

I do realize that the fact of progress value continuously goes forward and back is due to the fact that I'm zipping one folder that contains 10 sub-folders. 我的确意识到进度值连续不断地前进和后退的事实是由于我正在压缩一个包含10个子文件夹的文件夹。

However i would like to know if there's any way of properly show the progressbar for folder zipping? 但是我想知道是否可以正确显示文件夹压缩的进度条?

It's because "e.BytesTransferred" is the bytes transferred per entry not the total. 这是因为“ e.BytesTransferred”是每个条目传输的字节而不是总数。

Check out the docs 查看文档

"The number of bytes read or written so far for this entry." “到目前为止,该条目已读取或写入的字节数。”

Made the exact same mistake myself :) (although I was using it to extract files) 我自己犯了完全相同的错误:)(尽管我使用它来提取文件)

Building on @Shazi answer , one solution is to depend on the event that has the type Saving_AfterWriteEntry which happens after writing each entry like this: @Shazi答案的基础上,一种解决方案是依赖于类型为Saving_AfterWriteEntry的事件,该事件在编写每个条目之后发生,如下所示:

if(e.EventType == ZipProgressEventType.Saving_AfterWriteEntry)
{
    this.progressbar1.Value = e.EntriesSaved * 100 / e.EntriesTotal;
}

The problem with such solution is that it treats big files as small files, so the progress bar will have different speeds of progressing at different times (depending on the difference between file sizes). 这种解决方案的问题在于,它将大文件视为小文件,因此进度条在不同时间的处理速度将有所不同(取决于文件大小之间的差异)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM