( 实 例 ) Z I P 解 压 缩 (实例)ZIP解压缩 (实例)ZIP解压缩
1.(实例)ZIP解压缩
实例:实现一个解压缩程序,可以把zip文件解压缩到指定目录
相关技术:
- ZIP解压缩的API
- 工作线程
- 进度条控件的使用
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dstField.Text = @"C:\example\";
}
private void srcBtn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".zip";
dlg.Filter = "ZIP文件 |*.zip";
dlg.InitialDirectory = Path.GetFullPath(".");
if (dlg.ShowDialog() == DialogResult.OK)
{
srcField.Text = dlg.FileName;
}
}
private void dstBtn_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = Path.GetFullPath(".");
if (dlg.ShowDialog() == DialogResult.OK)
{
dstField.Text = dlg.SelectedPath;
}
}
private void extractBtn_Click(object sender, EventArgs e)
{
string zipPath = srcField.Text.Trim();
if (zipPath.Length == 0) return;
string extractPath = dstField.Text.Trim();
if (extractPath.Length == 0) return;
ProgressDlg dlg = new ProgressDlg(zipPath, extractPath);
dlg.ShowDialog(this);
}
}
2.ZIP相关API
在.NET FrameworkAPI
已经支持ZipArchive ,ZipArchiveEntry
的ZIP相关API
新建一个项目,对ZIP相关API作单独的测试
ZipArchive表示一个Zip压缩文件
ZipArchiveEntry表示Zip文件中的一项(目录或文件)
- FullName属性:表示该项的路径
- Length属性:表示该项的原始大小
3.界面流程
4.解压缩及进度
实例:创建工作线程,实现解压缩的功能,并在进度条上显示进度
进度:总大小,已处理大小
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dstField.Text = @"C:\example\";
}
private void srcBtn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".zip";
dlg.Filter = "ZIP文件 |*.zip";
dlg.InitialDirectory = Path.GetFullPath(".");
if (dlg.ShowDialog() == DialogResult.OK)
{
srcField.Text = dlg.FileName;
}
}
private void dstBtn_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = Path.GetFullPath(".");
if (dlg.ShowDialog() == DialogResult.OK)
{
dstField.Text = dlg.SelectedPath;
}
}
private void extractBtn_Click(object sender, EventArgs e)
{
string zipPath = srcField.Text.Trim();
if (zipPath.Length == 0) return;
string extractPath = dstField.Text.Trim();
if (extractPath.Length == 0) return;
ProgressDlg dlg = new ProgressDlg(zipPath, extractPath);
dlg.ShowDialog(this);
}
}
public partial class ProgressDlg : Form
{
public string zipPath;
public string extractPath;
public ProgressDlg(string zipPath,string extractPath)
{
this.zipPath = zipPath;
this.extractPath = extractPath;
InitializeComponent();
this.progressBar.Maximum = 100;
this.progressBar.Minimum = 0;
}
// 对话框显示时,启动线程
private void ProgressDlg_Load(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(this.ExtractTask));
th.Start();
}
// 线程的主方法
private void ExtractTask()
{
if (!extractPath.EndsWith("\\")) extractPath += "\\";
Directory.CreateDirectory(extractPath);
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
// 计算总大小
long total = 0;
foreach (ZipArchiveEntry entry in archive.Entries)
{
total += entry.Length;
}
// 开始解压缩
long bytes = 0;
foreach (ZipArchiveEntry entry in archive.Entries)
{
string fullPath = extractPath + entry.FullName;
if (fullPath.EndsWith("/"))
{
Directory.CreateDirectory(fullPath);
}
else
{
entry.ExtractToFile(fullPath, true);
// 更新进度
bytes += entry.Length;
OnTaskProgress(total, bytes, entry.FullName);
// 为了显示效果,加上一个sleep,实际应用中不需要sleep
Thread.Sleep(100);
}
}
}
OnTaskFinished();
}
// 在工作线程中更新UI: 显示进度
public void OnTaskProgress(long total, long bytes, string name)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<long, long, string>(this.OnTaskProgress),
total, bytes, name);
return;
}
this.progressBar.Value = (int)(bytes * 100 / total);
this.progressLabel.Text = name;
}
// 在工作线程中更新UI: 任务完成
public void OnTaskFinished()
{
if (this.InvokeRequired)
{
this.Invoke(new Action(this.OnTaskFinished));
return;
}
this.DialogResult = DialogResult.OK;
}
}