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;
}
if (!Directory.Exists(textBoxUnzipRootFolder.Text))
{
Directory.CreateDirectory(textBoxUnzipRootFolder.Text);
}
DirectoryInfo directorySelected = new DirectoryInfo(textBoxZipFolder.Text);
List<string> fileNames = new List<string>();
foreach (FileInfo fileInfo in directorySelected.GetFiles("*.zip"))
{
fileNames.Add(fileInfo.Name);
}
fileNames.Sort(new AlphanumComparator());
StreamWriter sw = new StreamWriter
(textBoxUnzipRootFolder.Text + "\\unzipped-files.txt");
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.