Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
17 Nov 2015 1  
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.

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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here