我正在准备一个为给定目录创建 zip 文件的应用程序。
我希望在创建 zip 时显示以下内容。
- 完成该 zip 的预计时间(已用时间和剩余时间)
- 压缩完成百分比
这是我的书面代码:
private void CreateZip(string FilePath)
{
using (ZipFile zip = new ZipFile())
{
zip.AddProgress += Zip_AddProgress;
zip.SaveProgress += Zip_SaveProgress;
zip.CompressionMethod = Ionic.Zip.CompressionMethod.Deflate;
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
if (!string.IsNullOrEmpty(FilePath))
{
zip.AddDirectory(FilePath, new DirectoryInfo(FilePath).Name);
}
var d= zip;
if (File.Exists(txtDest.Text))
{
File.Delete(txtDest.Text);
}
zip.Save(txtDest.Text);
}
}
private void Zip_SaveProgress(object sender, SaveProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Saving_Started)
lblFileName.Text = "Proccess Started Successfully";
if (e.EventType == ZipProgressEventType.Saving_AfterSaveTempArchive)
lblFileName.Text = "Proccess Completed Successfully";
if (e.BytesTransferred > 0 && e.TotalBytesToTransfer > 0)
{
int progress = (int)Math.Floor((decimal)((e.BytesTransferred * 100) / e.TotalBytesToTransfer));
pbPerFile.Value = progress;
lblPercentagePerFile.Text = Convert.ToString(progress) + "%";
Application.DoEvents();
}
if (e.EntriesSaved > 0 && e.EntriesTotal > 0)
{
int progress = (int)Math.Floor((decimal)((e.EntriesSaved * 100) / e.EntriesTotal));
pbTotalFile.Value = progress;
Application.DoEvents();
lblTotal.Text = Convert.ToString(progress) + "%";
}
}
第一个进度条适用于文件的大小,因为e.BytesTransferred和e.TotalBytesToTransfer是以Bytes为单位的返回大小。
但是e.EntriesSaved和e.EntriesTotal将返回已保存条目的长度和我们添加的条目总数。
所以我希望第二个进度条根据整个选定文件和压缩文件的大小工作。
您的努力将不胜感激。
谢谢...