Background:
we can use Shell32.shell to zip/unzip file. the following segment can work well at win 7 before. BUT it does not work in win 8.
public void Unzip(string zipFile, string destination) { System.Type shellType = System.Type.GetTypeFromProgID("Shell.Application"); Shell shell = System.Activator.CreateInstance(shellType) as Shell; Folder zip = shell.NameSpace(zipFile); Folder dest = shell.NameSpace(destination); dest.CopyHere(zip.Items(), 0x14); }
Issue:
Unable to cast COM object of type ‘Shell32.ShellClass‘ to interface type ‘Shell32.IShellDispatch6‘. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{286E6F1B-7113-4355-9562-96B7E9D64C54}‘ failed due
to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
public void Unzip(string zipFile, string folderPath) { Type shellAppType = Type.GetTypeFromProgID("Shell.Application"); Object shell = Activator.CreateInstance(shellAppType); Folder zip = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { zipFile }); Folder dest = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folderPath }); dest.CopyHere(zip.Items(), 0x14); }
Note:
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.
Zip/Unzip step by step: http://amberishsingh.blogspot.com/2011/11/easily-zip-unzip-files-using-windows.htmlShell32 code compiled on Windows 7/ Vista does not run on XP/2003: http://techitongue.blogspot.com/2012/06/shell32-code-compiled-on-windows-7.html