(实例)ZIP解压缩

( 实 例 ) 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;
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值