之前发过一个,解压大文件实在太慢了,改进了一下
调用方式
[C#] 纯文本查看 复制代码 private async void Button_Click(object sender, RoutedEventArgs e)
{
var zip = new Ezip();
zip.unzipProgressChange += Zip_unzipProgressChange;
if (! await zip.unzip(@"C:\Users\pc\Desktop\ZIP\Microsoft.WebView2.FixedVersionRuntime.98.0.1108.62.x86.zip", @"C:\Users\pc\Desktop\ZIP\x\")) {
MessageBox.Show("解压失败");
}
}
private void Zip_unzipProgressChange(int totalProgress, int fileProgress, string filename)
{
Filename.Content = filename;
Zong.Value = totalProgress;
File.Value = fileProgress;
}
解压类
[C#] 纯文本查看 复制代码 internal class Ezip
{
/// <summary>
/// 总进度
/// </summary>
int totalProgress = 0;
/// <summary>
/// 单文件进度
/// </summary>
int fileProgress = 0;
/// <summary>
/// 单文件名
/// </summary>
string fileName = "";
bool end = false;
/// <summary>
/// 解压进度事件
/// </summary>
/// <param name="totalProgress">总进度</param>
/// <param name="fileProgress">单文件进度</param>
/// <param name="filename">正在提取中的文件名</param>
public delegate void UnzipProgress(int totalProgress,int fileProgress,string filename);
public event UnzipProgress unzipProgressChange;
public Task<bool> unzip(string filepath, string outdir) {
var task = Task.Run(() => {
this.end = false;
this.updateProgress();
var re = Unzip_(filepath, outdir);
this.end = true;
return re;
});
return task;
}
private bool Unzip_(string filepath, string outdir)
{
long totalSize = 0;
long totalCountProgress = 0;
outdir += @"\";
outdir.Replace(@"/", @"\");
outdir.Replace(@"\\", @"\");
outdir.Replace(@"\\", @"\");
FileStream zipFileToOpen;
ZipArchive archive;
try
{
zipFileToOpen = new FileStream(filepath, FileMode.Open);
archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read);
}
catch (Exception ex) {
return false;
}
List<DatazipType> Datalist = new List<DatazipType>();
//吧所有文件路径归类存储到list
foreach (var zipArchiveEntry in archive.Entries)
{
Datalist.Add(new DatazipType() { Type = zipArchiveEntry.Name == "" ? ZipType.dir: ZipType.file, FullName = zipArchiveEntry.FullName });
}
totalSize = Datalist.Count;
//创建所有目录
foreach (var item in Datalist)
{
if (item.Type == ZipType.dir && !Directory.Exists(outdir + item.FullName))
{
try { Directory.CreateDirectory(outdir + item.FullName); } catch { }
}
if (item.Type == ZipType.dir) {
this.totalProgress = (int)this.取百分比(totalSize, ++totalCountProgress);
}
// Debug.WriteLine((++totalCountProgress / totalSize) * 100);
}
//解压某个文件
foreach (DatazipType item in Datalist)
{
if (item.Type == ZipType.file)
{
ZipArchiveEntry entry = archive.GetEntry(item.FullName);
System.IO.Stream stream = entry.Open();
long fileSize = entry.Length;
long fileReadLen = 0;
try
{
this.fileName = item.FullName;
System.IO.Stream output = new FileStream(outdir + item.FullName, FileMode.Create);
int readLenth = 0;
do {
byte[] buffer = new byte[1024*10];
readLenth = stream.Read(buffer, 0, 1024*10);
output.Write(buffer, 0, readLenth);
fileReadLen += readLenth;
this.fileProgress = (int)this.取百分比(fileSize, fileReadLen);
} while (readLenth > 0);
stream.Close();
output.Close();
}
catch (Exception ex)
{
continue;
}
this.totalProgress = (int)this.取百分比(totalSize, ++totalCountProgress);
}
}
setChange();
Datalist.Clear();
archive.Dispose();
zipFileToOpen.Close();
return true;
}
private void updateProgress() {
Task.Factory.StartNew(() =>
{
do {
setChange();
Thread.Sleep(50);
} while (!end);
});
}
void setChange() {
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
if (unzipProgressChange != null)
unzipProgressChange(this.totalProgress, fileProgress, fileName);
}));
}
double 取百分比(double max, double value)
{
return (value / max) * 100.0;
}
double 取百分比(int max, int value)
{
return (Convert.ToDouble(value) / Convert.ToDouble(max)) * 100;
}
class DatazipType
{
/// <summary>
/// 1 目录 2 文件
/// </summary>
public ZipType Type;
public string FullName;
}
enum ZipType
{
dir = 1,
file = 2,
}
}
|