基本信息
源码名称:ICSharpCode.SharpZipLib 压缩文件实例(源码)
源码大小:0.62M
文件格式:.zip
开发语言:C#
更新时间:2019-07-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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        

        
    }
}