Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Make a Zip/UnZip Software using SharpZipLib

Rate me:
Please Sign up or sign in to vote.
4.33/5 (17 votes)
14 Sep 2007CPOL 593.3K   9K   45  
This article shows how to use SharpZipLib to make a small Zip/UnZip software easily
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Threading;

namespace ZipTest
{
    public partial class FormZip : Form
    {
        public FormZip()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            radioButtonZip.Checked = true;
        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            //show a openfiledialog to select a file
            OpenFileDialog f = new OpenFileDialog();
            f.Multiselect = false;
            if (f.ShowDialog() == DialogResult.OK)
            {
                textBoxFileName.Text = f.FileName;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textBoxFileName.Text != "")
            {
                if (radioButtonZip.Checked == true)
                {
                    //start a new thread to zip it
                    Thread th = new Thread(new ThreadStart(Zip));
                    th.Start();
                }
                else
                {
                    //start a new thread to unzip it
                    Thread th = new Thread(new ThreadStart(UnZip));
                    th.Start();
                }
            }
        }

        private void Zip()
        {
            toolStripStatusLabel1.Text = "Zipping...";
            SetButtonOK(false);
            ZipHelp.Zip(textBoxFileName.Text, textBoxFileName.Text + ".zip", 4096);
            toolStripStatusLabel1.Text = "Done";
            SetButtonOK(true);
        }

        private void UnZip()
        {
            toolStripStatusLabel1.Text = "UnZipping...";
            SetButtonOK(false);
            ZipHelp.UnZip(textBoxFileName.Text, Path.GetDirectoryName(textBoxFileName.Text), 4096);
            toolStripStatusLabel1.Text = "Done";
            SetButtonOK(true);
        }

        //the method to set button's state
        private void SetButtonOK(bool Enable)
        {
            if (buttonOK.InvokeRequired)
                buttonOK.Invoke(new SetEnableCallBack(SetButtonOK), new object[] { Enable });
            else
                buttonOK.Enabled = Enable;
        }

        //delegate to call back
        delegate void SetEnableCallBack(bool Enable);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
I live in Xi'an(西安). It has 3000+ years history and is one of the birthplaces of the ancient civilization of China.
accelerate your life.

Comments and Discussions