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"); |
You may have noticed the 4 | 16 values in the CopyHere method. Those are the copy options which you can change to make the copying behave differently in order to suit your needs. Here is the list of the additional options.
4 – Do not display a progress dialog box.
8 – Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
16 – Respond with “Yes to All” for any dialog box that is displayed.
64 – Preserve undo information, if possible.
128 – Perform the operation on files only if a wildcard file name (*.*) is specified.
256 – Display a progress dialog box but do not show the file names.
512 – Do not confirm the creation of a new directory if the operation requires one to be created.
1024 – Do not display a user interface if an error occurs.
2048 – Version 4.71. Do not copy the security attributes of the file.
4096 – Only operate in the local directory. Do not operate recursively into subdirectories.
8192 – Version 5.0. Do not copy connected files as a group. Only copy the specified files.
For all those with “no such interface supported” error. I fixed it like this:
from jeronevw’s answer on this forum:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/b25e2b8f-141a-4a1c-a73c-1cb92f953b2b/instantiate-shell32shell-object-in-windows-8?forum=clr
Hope it helps!
This method works great (even referenced in stackoverflow: http://stackoverflow.com/questions/16052877/how-to-unzip-all-zip-file-from-folder-using-c-sharp-4-0-and-without-using-any-o ) but I found that it creates temp files that aren’t deleted in the local\temp directory.
No se puede convertir el objeto COM del tipo ‘System.__ComObject’ al tipo de interfaz ‘Shell32.Shell’. Ocurrió un error de operación debido a que la llamada QueryInterface en el componente COM para la interfaz con IID ‘{34936BA1-67AD-4C41-99B8-8C12DFF1E974}’ generó el siguiente error: Interfaz no compatible (Excepción de HRESULT: 0x80004002 (E_NOINTERFACE)).
I have the same issue:
+ $exception {“Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Shell32.Shell’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{34936BA1-67AD-4C41-99B8-8C12DFF1E974}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).”} System.Exception {System.InvalidCastException}
using this code for asp.net application.
########
ERROR
########
ERror after executing Shell32.Folder sourceFile = objShell.NameSpace(zipFile);
System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
did you got a way out? Same error
Thanks.. Works Great for me
Excellent! It works fine and amazing job with such a small piece of code…
Thank you very much
how can i write this statement.
public static void UnZip(string zipFile, string folderPath)
{
i have static void main(string[]args)
Thank you. This really works!