在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → ICSharpCode.SharpZipLib 压缩文件实例(源码)

ICSharpCode.SharpZipLib 压缩文件实例(源码)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.62M
  • 下载次数:24
  • 浏览次数:163
  • 发布时间:2019-07-10
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Csharp 解压缩 ZIP 文件 解压

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 压缩程序
{
    public partial class frmMain : Form
    {
        //选择的文件名(全路径)
        private string path;
        //解压的文件名(全路径)
        private string destPath;

        private string[] filespath;
        //声明一个解压对象
        ZipClass zp = new ZipClass();
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DisplayStatusMessage("打开使用压缩程序!");
            EnableButtons(false);           
        }

        #region 自定义方法   
        /// <summary>   
        /// 自定义方法   
        /// </summary>
        //显示状态列信息
        public void DisplayStatusMessage(string statusMessage)
        {
            this.StatusLabel.Text = statusMessage;
        }

        //启动属于“文件”一些操作
        private void EnableMenu(bool value)
        {
            addfilesBt.Enabled = value;
            extractBt.Enabled = value;
            addfileStrip.Enabled = value;
            addDocumentsStrip.Enabled = value;
            extractStrip.Enabled = value;
        }

        //关闭属于“操作”的一些功能
        private void EnableButtons(bool value)
        {
            openwayBt.Enabled = value;
            UncompressWayBt.Enabled = value;
            ClearBt.Enabled = value;
            compressBt.Enabled = value;
            UncompressBt.Enabled = value;  
            openwayStrip.Enabled = value;
            compressStrip.Enabled = value;
            UncompressWayStrip.Enabled = value;
            UncompressStrip.Enabled = value;
            ClearStrip.Enabled = value;
        }


        //重置所有控件
        private void ClearProperties()
        {
            path = null;
            destPath = null;
            filespath = null;
            lblParent.Text = String.Empty;
            lblRoot.Text = String.Empty;
            lblLength.Text = String.Empty;
            lblAttributes.Text = String.Empty;
            lblCreationTime.Text = String.Empty;
            lblLastAccessTime.Text = String.Empty;
            lblLastWriteTime.Text = String.Empty;
            lblExtension.Text = String.Empty;
            lblFullName.Text = String.Empty;
            lblName.Text = String.Empty;
            textBox3.Text = String.Empty;
            checkBox1.Checked = false;
            EnableMenu(true);
            EnableButtons(false);
            DisplayStatusMessage("重置所有信息");
        }

        // 显示 FileInfo 与 DirectoryInfo 对象两者所通用的属性。
        private void DisplayFSIProperties(FileSystemInfo fsi)
        {
            lblAttributes.Text = fsi.Attributes.ToString();
            lblCreationTime.Text = fsi.CreationTime.ToString();
            lblLastAccessTime.Text = fsi.LastAccessTime.ToString();
            lblLastWriteTime.Text = fsi.LastWriteTime.ToString();
            lblExtension.Text = fsi.Extension;
            lblFullName.Text = fsi.FullName;
            lblName.Text = fsi.Name;
        }

        #endregion

        #region 压缩功能
        /// <summary>   
        /// 压缩操作  
        /// </summary>   
        /// <param name="sender"></param>   
        /// <param name="e"></param>  
        //添加文件操作
        private void addfilesBt_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "所有文件 (*.*)|*.*";
            file.InitialDirectory = "c:\\";
            file.FilterIndex = 2;
            file.Multiselect = true;
            file.RestoreDirectory = true;
            file.CheckFileExists = true;

            if (file.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = file.OpenFile()) != null)
                {
                    
                    foreach (string s in file.FileNames)
                    {
                        if (listBox1.Items.Contains(s))
                            continue;
                        listBox1.Items.Add(s);
                    }
                    myStream.Close();
                    //限制解压操作
                    extractBt.Enabled = false;
                    addDocumentsStrip.Enabled = false;
                    extractStrip.Enabled = false;
                    openwayBt.Enabled = true;
                    compressBt.Enabled = true;
                    ClearBt.Enabled = true;
                    openwayStrip.Enabled = true;
                    compressStrip.Enabled = true;
                    ClearStrip.Enabled = true;
                    DisplayStatusMessage("成功添加文件");
                }
            }
        }

        private void addfileStrip_Click(object sender, EventArgs e)
        {
            addfilesBt_Click(sender, e);
        }

        //添加文件夹处理


        //选择压缩路径
        private void openwayBt_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfg = new SaveFileDialog();
            sfg.InitialDirectory = "c:\\";
            sfg.Filter = "zip压缩文件(*.zip)|*.zip";
            sfg.FilterIndex = 2;
            sfg.RestoreDirectory = true;

            if (sfg.ShowDialog() == DialogResult.OK)
            {
                // 设置压缩包保存路径
                string Newpath = sfg.FileName;
                destPath = Path.GetDirectoryName(Newpath);
                //压缩后的目标文件   
                destPath = destPath   "\\"   Path.GetFileNameWithoutExtension(Newpath)   ".zip";
                DisplayStatusMessage("文件保存路径 "   destPath);
            }
        }

        private void openwayStrip_Click(object sender, EventArgs e)
        {
            openwayBt_Click(sender, e);
        }

       



        //文件或文件夹压缩处理
        private void compressBt_Click(object sender, EventArgs e)
        {
            if (destPath == null)
            {
                MessageBox.Show("请选择解压路径", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (listBox1.Items.Count == 1)
            {
                path = listBox1.Items[0].ToString();
                filespath = null;
            }
            else
            {
                filespath = new string[listBox1.Items.Count];
                for (int i = 0; i < filespath.Length; i  )
                {
                    filespath[i] = listBox1.Items[i].ToString();
                }
                path = null;
            }

            DisplayStatusMessage("成功进行压缩处理");
            //选择是否加密压缩 
            if (checkBox1.Checked == true & textBox3.Text != "")
            {
                //多文件加密压缩
                if (filespath != null)
                {
                    zp.Zip(filespath, destPath, textBox3.Text);
                    return;
                }
                //单文件活文件夹加密压缩
                if (path != null)
                {
                    zp.Zip(path, destPath, textBox3.Text);
                    return;
                }
            }
            else
            {
                //多文件压缩
                if (filespath != null)
                {
                    zp.Zip(filespath, destPath,"");
                    return;
                }
                //单文件或文件夹压缩
                if (path != null)
                {
                    zp.Zip(path, destPath,"");
                    return;
                }
            }

        }

        private void compressStrip_Click(object sender, EventArgs e)
        {
            compressBt_Click(sender, e);
        }
        #endregion

        #region 解压功能
        /// <summary>   
        /// 解压功能   
        /// </summary>   
        /// <param name="sender"></param>   
        /// <param name="e"></param> 

        //添加压缩包操作
        private void extractBt_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "zip压缩文件(*.zip)|*.zip";
            file.InitialDirectory = "c:\\";
            file.FilterIndex = 2;
            file.RestoreDirectory = true;
            file.CheckFileExists = true;

            if (file.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = file.OpenFile()) != null)
                {
                    listBox1.Items.Clear();
                    listBox1.Items.Add(file.FileName);
                    // 读取压缩包位置,并设置默认加压路径
                    destPath = Path.GetDirectoryName(file.FileName);
                    addfilesBt.Enabled = false;
                    addDocumentsStrip.Enabled = false;
                    addfileStrip.Enabled = false;
                    UncompressWayBt.Enabled = true;
                    UncompressBt.Enabled = true;
                    ClearBt.Enabled = true;
                    UncompressWayStrip.Enabled = true;
                    UncompressStrip.Enabled = true;
                    ClearStrip.Enabled = true;

                    DisplayStatusMessage("读取压缩包 "   file.FileName );
                    myStream.Close();
                }
            }
        }

        private void extractStrip_Click(object sender, EventArgs e)
        {
            extractBt_Click(sender, e);
        }

        //选择解压路径操作
        private void UncompressWayBt_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fold = new FolderBrowserDialog();
            if (fold.ShowDialog() == DialogResult.OK)
            {
                //解压释放路径
                destPath = fold.SelectedPath;
                DisplayStatusMessage("解压路径 "   destPath);
            }
        }

        private void UncompressWayStrip_Click(object sender, EventArgs e)
        {
            UncompressWayBt_Click(sender, e);
        }


        //解压压缩包操作
        private void UncompressBt_Click(object sender, EventArgs e)
        {                     
            path = listBox1.Items[0].ToString();           

            //判断是否输入口令解压
            if (checkBox1.Checked == true & textBox3.Text != "")
                UnZipClass.UnZip(path, destPath,textBox3.Text);
            else                
                UnZipClass.UnZip(path, destPath, "");
            DisplayStatusMessage("成功进行解压处理");
        }

        private void UncompressStrip_Click(object sender, EventArgs e)
        {
            UncompressBt_Click(sender, e);
        }
        #endregion

        #region 文件列表触发事件处理
        /// <summary>   
        /// 文件列表触发事件处理 
        /// </summary> 

        //选择FileInfo对象或Dire对象属相
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {               
                return;
            }

            try
            {
                string listPath = listBox1.SelectedItem.ToString();
                // 一个 FileInfo 对象,以便显示其相关属性。
                if (File.Exists(listPath))
                {
                    FileInfo fi = new FileInfo(listPath);
                    // 显示文件大小。
                    lblLength.Text = fi.Length.ToString()   "K";
                    DisplayFSIProperties(fi);
                    DisplayStatusMessage("文件:"   listPath);
                    return;
                }

                // 一个 DirectoryInfo 对象,以便显示其相关属性。
                if (Directory.Exists(listPath))
                {
                    DirectoryInfo di = new DirectoryInfo(listPath);
                    lblParent.Text = di.Parent.Name;
                    lblRoot.Text = di.Root.Name;
                    DisplayFSIProperties(di);
                    DisplayStatusMessage("文件夹:"   listPath);
                    return;
                }
            }
            catch
            {
                MessageBox.Show("读取有错", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //文件列表为空时候,自动重置
        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                ClearProperties();
                EnableButtons(false);
            }
        }

        //文件列表控件单击右键显示快捷菜单
        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right || listBox1.SelectedIndices.Count == 0)
            {
                return;
            }
            int index = listBox1.IndexFromPoint(e.X, e.Y);

            if (index != -1)
            {
                listBox1.SelectedIndex = index;
                contextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DisplayStatusMessage("移除文件列表中的:"   listBox1.SelectedItem.ToString());
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);            
        }
        #endregion

        #region 重置功能
        /// <summary>   
        /// 重置功能   
        /// </summary>  
        //重置所有信息
        private void ClearBt_Click(object sender, EventArgs e)
        {
            ClearProperties();
            EnableButtons(false);
            listBox1.Items.Clear();
            DisplayStatusMessage("清空所有信息");
        }

        private void ClearStrip_Click(object sender, EventArgs e)
        {
            ClearBt_Click(sender, e);
        }

        #endregion

        #region 口令功能
        /// <summary>   
        /// 口令功能   
        /// </summary>
        //选择是否打开口令功能
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox3.Enabled = true;
                DisplayStatusMessage("打开口令功能");
            }
            else
            {
                textBox3.Enabled = false;
                DisplayStatusMessage("关闭口令功能");
            }
        }
        #endregion


        #region 帮助和退出功能
        /// <summary>   
        /// 帮助功能   
        /// </summary>
        private void 于我们联系ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ContactUs contact = new ContactUs();
            contact.ShowDialog();
        }

        private void 关于压缩程序AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutUs about = new AboutUs();
            about.ShowDialog();
        }

        //退出功能
        private void closeMenuStripButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion        

        
    }
}

实例下载地址

ICSharpCode.SharpZipLib 压缩文件实例(源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警