c#解压zip进度_zip压缩、解压、进度条【实例简介】
【实例截图】
【核⼼代码】
BackgroundWorker worker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
worker.WorkerSupportsCancellation = true;//是否⽀持异步取消
worker.WorkerReportsProgress = true;//能否报告进度更新
worker.DoWork = Worker_DoWork;
}
int value;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
//saveFileDialog1.ShowDialog();
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Add(folderBrowser.SelectedPath);
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "zip files(*.zip)|*.zip";
if (saveFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = saveFile.FileName;
}
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Add(openFile.FileName);
}
}
private void compression_Click(object sender, EventArgs e)
{
value = 0;
worker.RunWorkerAsync("compression");//会触发worker的DoWork事件ProgressFrom progress = new ProgressFrom(worker);
progress.ShowDialog(this);
}
private void uncompression_Click(object sender, EventArgs e)
{
value = 0;
worker.RunWorkerAsync("uncompression");
ProgressFrom progress = new ProgressFrom(worker);//将worker给progress窗体progress.ShowDialog(this);
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
if (e.Argument.ToString()== "compression")
{
try
{
//Create Zip File
ZipOutputStream zipStream = null;
using (zipStream = new ZipOutputStream(File.Create(textBox1.Text)))
{
zipStream.Password = "123";//压缩密码
zipStream.SetComment("版本1.0");//压缩⽂件描述
zipStream.SetLevel(6); //设置CompressionLevel,压缩⽐
foreach (string f in listBox1.Items)
ZipMultiFiles(f, zipStream);
}
}
}getsavefilename
catch (Exception ex)
{
MessageBox.Show("压缩失败" "\r\n" ex.Message);
}
finally
{
worker.CancelAsync();
}
}
else
{
try
{
ZipInputStream zipStream = null;
foreach (string f in listBox1.Items)
{
if (File.Exists(f))
{
using (zipStream = new ZipInputStream(File.OpenRead(f))) {
zipStream.Password = "123";
UnZipMultiFiles(f, zipStream,textBox2.Text);
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("解压失败" "\r\n" ex.Message);
finally
{
worker.CancelAsync();
}
}
}
private void ZipMultiFiles(string file, ZipOutputStream zipStream, string lastName = "")
{
FileStream streamReader = null;
if (File.Exists(file))      //是⽂件,压缩
{
using (streamReader = File.OpenRead(file))
{
//处理file,如果不处理,直接new ZipEntry(file)的话,压缩出来是全路径
/
/如C:\Users\RD\Desktop\l,压缩包⾥就是C:\Users\RD\Desktop\l //如果处理为l压缩出来就是l(这样才跟⽤⼯具压缩的是⼀样的效果)
string path = Path.GetFileName(file);
if (lastName != "")
{
path = lastName "\\" path;
}
ZipEntry zipEntry = new ZipEntry(path);
zipEntry.DateTime = DateTime.Now;
zipEntry.Size = streamReader.Length;
zipStream.PutNextEntry(zipEntry);//压⼊
int sourceCount = 0;
byte[] buffer = new byte[4096 * 1024];
while ((sourceCount = streamReader.Read(buffer, 0, buffer.Length)) > 0)
{
zipStream.Write(buffer, 0, sourceCount);
if (worker.WorkerReportsProgress)
{
if (value == 100)//因为不知道要压缩的总量有多⼤,所以这⾥简陋的让进度条从0-100循环
value = 0;
}
value = 1;
worker.ReportProgress(value);//报告进度,在progress窗体⾥响应
}
if (worker != null && worker.IsBusy)
{
if (worker.CancellationPending)
{
// update the entry size to avoid the ZipStream exception
zipEntry.Size = streamReader.Position;
break;
}
}
}
if (worker.WorkerReportsProgress)
{
worker.ReportProgress(value, file);//报告进度,和刚压缩完成的file
}
if (worker != null && worker.CancellationPending)
{
return;
}
}
}
else//是⽂件夹,递归
{
string[] filesArray = Directory.GetFileSystemEntries(file);
string folderName = Regex.Match(file, @"[^\/:*\?\”“\<>|,\\]*$").ToString();//获取最⾥⼀层⽂件夹if (lastName != "")
{
folderName = lastName "\\" folderName;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。