Click here to Skip to main content
15,884,099 members
Articles / DevOps
Tip/Trick

Unzip Multiple Zip Files on File Name's Alpha-numeric Sequence

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Nov 2015CPOL1 min read 12.6K   323   7  
A utility application that uses .NET 4.5 ZipFile class to unzip multiple files on file name sequence and save unzipped files to a same root directory.

Background

We have Deltek Time & Expense running on our server. Deltek releases a lot of hotfixes in a short time period. I don't apply the hotfixes one by one if it seems to not affect our users. One day after our users reported some errors of the system, I wanted to apply all hotfixes. There are 106 hotfixes as zip files that I have to download and apply to the system. Applying a Deltek T&E hotfix is to unzip the downloaded zip file and copy its code files to corresponding directories. I implemented this Windows Form application as a utility that can unzip all zip files on their names' alpha-numeric sequence in a directory and save all unzipped files to a common root directory. The new unzipped files will overwrite the files with same file name and path from older zip files.

Image 1

Unzip Method

Firstly, you need to specify the zip files full folder path and the unzipped files' root folder path. After you click the unzip button, the following code will be executed to unzip all zipped files to a specified root folder.

C#
private void buttonUnzip_Click(object sender, EventArgs e)
{
    if (textBoxZipFolder.Text == "")
    {
        MessageBox.Show("Zip files folder path is not specified.");
        textBoxZipFolder.Focus();
        return;
    }
    if (textBoxUnzipRootFolder.Text == "")
    {
        MessageBox.Show("Save root folder path is not specified.");
        textBoxUnzipRootFolder.Focus();
        return;
    }
 
    // Check if the unzip folder exists.
    if (!Directory.Exists(textBoxUnzipRootFolder.Text))
    {
        Directory.CreateDirectory(textBoxUnzipRootFolder.Text);
    }
 
    DirectoryInfo directorySelected = new DirectoryInfo(textBoxZipFolder.Text);
    List<string> fileNames = new List<string>();
 
    // Get all file names
    foreach (FileInfo fileInfo in directorySelected.GetFiles("*.zip"))
    {
        fileNames.Add(fileInfo.Name);
    }
 
    // Sort all file names in alpha-numeric order
    fileNames.Sort(new AlphanumComparator());
    StreamWriter sw = new StreamWriter
    	(textBoxUnzipRootFolder.Text + "\\unzipped-files.txt");
             
    // Unzip all zip files
    foreach(string name in fileNames)
    {
        sw.WriteLine(name);
        string zipfilePath = textBoxZipFolder.Text + "\\" + name;
        //ZipFile.ExtractToDirectory(zipfilePath, textBoxUnzipRootFolder.Text);
        using (ZipArchive archive = ZipFile.OpenRead(zipfilePath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string unzipFileName = Path.Combine(textBoxUnzipRootFolder.Text, 
                	entry.FullName).Replace("/", "\\");
                string directoryPath = Path.GetDirectoryName(unzipFileName);
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);
                if (entry.Name == "")
                    continue;
 
                entry.ExtractToFile(unzipFileName, true);
            }
        } 
    }
    sw.Close();
 
    MessageBox.Show("Total " + fileNames.Count.ToString() + 
    	" zip files have been unzipped.");
    Close();
}

For users who are not interested in code development or don't have a development environment, the application executable files are attached. This application requires .NET 4.5 installed on your Windows computer.

Points of Interest

The method ZipFile.ExtractToDirectory (zipfilePath, textBoxUnzipRootFolder.Text) does not have an option to overwrite an old unzipped file by a new unzipped file in the same path. That will cause the unzip process interrupted. I use ZipArchive class to extract each file and overwrite old files.

License

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


Written By
CEO Qi Tech LLC
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --