Tag Archives: Shell32
Unzip files using Shell32 in C#
The .NET Framework didn’t have an easy way for developers to unzip files resulting to a lot of people having to use third party libraries to achieve that functionality. This was until the .NET Framework 4.5 was released which had a new class called ZipFile that simplified the process of unzipping files..
But since targeting the .NET Framework 4.5 might not be ideal in many cases, mainly because its adaptation rate is still fairly low I will demostrate another way to unzip .zip files with the use of Shell32.
Firstly you will need to reference in your project the COM library called Microsoft Shell Controls And Automation (Interop.Shell32.dll) in order to be able to access the Shell32 namespace.
The method below is a simple example that takes two parameters. The .zip file location and the folder destination where the files will be extracted to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void UnZip(string zipFile, string folderPath) { if (!File.Exists(zipFile)) throw new FileNotFoundException(); if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); Shell32.Shell objShell = new Shell32.Shell(); Shell32.Folder destinationFolder = objShell.NameSpace(folderPath); Shell32.Folder sourceFile = objShell.NameSpace(zipFile); foreach (var file in sourceFile.Items()) { destinationFolder.CopyHere(file, 4 | 16); } } |
The usage is fairly simple:
1 | UnZip(@"C:\myzipfile.zip", @"C:\my destination folder"); |