Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / Windows Forms

UnZipper: a File Extraction Tool

Rate me:
Please Sign up or sign in to vote.
3.60/5 (2 votes)
1 Aug 2007CPOL4 min read 44.3K   470   20  
UnZipper is a file extraction tool for decompressing files of a selected type/file extension out of a Zip or a compressed folder.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace UnZipper
{
    public partial class frmMain : Form
    {
        #region Members
        private Thread zipThread;
        private Library.UnZipper zip;
        private Library.serialUsedFileTyes fileExts;
        private int fileCount;
        #endregion

        #region FormEvents
        public frmMain()
        {
            InitializeComponent();
            MyInitializer();
        }

        private void saveFolder_Click(object sender, EventArgs e)
        {
            DestinationPath.Text = Browse();
        }

        private void openFile_Click(object sender, EventArgs e)
        {
            ContainingPath.Text = Browse();
        }

        private void DestBrowse_Click(object sender, EventArgs e)
        {
            DestinationPath.Text = Browse();
        }

        private void ContBrowse_Click(object sender, EventArgs e)
        {
            ContainingPath.Text = Browse();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            VerifyStart();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to stop the process?\r\n" +
                               "You will have to start over if you chose 'Yes'.",
                               "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                                == DialogResult.Yes)
            {
                zip.Stop();
                zipThread.Abort();
            }
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (zip != null)
            {
                zip.UpdateForm = false;
                zip.IsStarted = false;
            }
            UnZipper.Properties.Settings.Default.Save();
        }
        #endregion 
        
        #region MyMethods
        private void VerifyStart()
        {
            if (this.ContainingPath.Text != "" &&
                this.DestinationPath.Text != "" &&
                Directory.Exists(this.ContainingPath.Text) &&
                Directory.Exists(this.DestinationPath.Text))
            {
                if (this.FileType.Text != "")
                {
                    Go();
                }
                else
                {
                    if (MessageBox.Show("You did not enter a file extension.\r\n\n" +
                                       "Click 'Yes' to decompress all files in the Zip\r\n" +
                                       "Click 'No'  to cancel operation.", "Confirm",
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Go();
                    }
                }
            }
            else
            {
                MessageBox.Show("You must enter a valid source and destination path.",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void Go()
        {
            fileCount = 0;
            this.NumFiles.Text = fileCount.ToString();

            Library.UnZipOptions opt = new UnZipper.Library.UnZipOptions();
            opt.ContainingPath = this.ContainingPath.Text;
            opt.DestinationPath = this.DestinationPath.Text;
            opt.Extension = this.FileType.Text;
            StoreFileExt();
            zip = new UnZipper.Library.UnZipper();
            zip.Options = opt;
            /*  These are altenate ways of accomplishing the same thing.
                zip.ContaningPath = this.ContainingPath.Text;       <<Setting the values
                zip.DestanationPath = this.DestinationPath.Text;    <<directly to 
                zip.Extensions = this.FileType.Text;                <<properties
                zipThread = new Thread(new ParameterizedThreadStart(zip.Star)); <<Using a Parameterized Thread
                zipThread.Start(opt);
             * Also you do not have to have a separate thread. I used one so control would be 
             * passed backed to the form.
            */

            zipThread = new Thread(new ThreadStart(zip.Start));
            zipThread.Start();
        }

        private void StoreFileExt()
        {            
            if (!fileExts.FileExtension.Contains(this.FileType.Text.Trim()))
            {
                fileExts.FileExtension.Add(this.FileType.Text.Trim());
                this.FileType.Items.Add(this.FileType.Text.Trim());
                this.FileType.Refresh();
            }
        }

        private void MyInitializer()
        {
            fileExts = (Library.serialUsedFileTyes)UnZipper.Properties.Settings.Default.FileExt;
            foreach (string s in fileExts.FileExtension)
                this.FileType.Items.Add(s);
            UnZipper.Library.UnZipper.SetFileEvent += 
                new UnZipper.Library.SetNumFiles(UnZipper_SetFileEvent);
            UnZipper.Library.UnZipper.NumFilesEvent += 
                new UnZipper.Library.UpdateNumFiles(UnZipper_NumFilesEvent);
            UnZipper.Library.UnZipper.FileProcessEvent += 
                new UnZipper.Library.UpdateFileProcess(UnZipper_FileProcessEvent);
            UnZipper.Library.UnZipper.UpdateFileNameEvent += 
                new UnZipper.Library.UpdateFileName(UnZipper_UpdateFileNameEvent);
        }

        private string Browse()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            return fbd.SelectedPath;
        }
        #endregion

        #region UpdateFormSection
        private void UnZipper_UpdateFileNameEvent(string Name)
        {
            this.BeginInvoke(new UpdateFileName(FileNameUpdate), 
                             new object[] { Name });
        }
        private delegate void UpdateFileName(string Name);
        private void FileNameUpdate(string Name)
        {
            this.FileName.Text = "Zip File Name: " + Name;
        }

        private void UnZipper_SetFileEvent(int NumFiles)
        {
            this.BeginInvoke(new UpdateFiles(FileUpdate), 
                             new object[] { NumFiles });
        }
        private delegate void UpdateFiles(int num);
        private void FileUpdate(int num)
        {
            fileCount += num;
            this.NumFiles.Text = fileCount.ToString();
        }

        private void UnZipper_NumFilesEvent(int NumFiles)
        {
            this.BeginInvoke(new SetBar(BarSet), 
                             new object[] { NumFiles });
        }
        private delegate void SetBar(int num);
        private void BarSet(int num)
        {
            this.ProgressBar.Maximum = num;
        }

        private void UnZipper_FileProcessEvent(int Process)
        {
            this.BeginInvoke(new UpdateBar(BarUpdate));
        }
        private delegate void UpdateBar();
        private void BarUpdate()
        {
            this.ProgressBar.PerformStep();
        }
        #endregion
    }
}

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
Web Developer
United States United States
I am a software, database, and gis developer. I love the challenge of learning new ways to code.

Comments and Discussions