Zip unzip with Shell32

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.

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.

Zip/Unzip step by step: http://amberishsingh.blogspot.com/2011/11/easily-zip-unzip-files-using-windows.html

Shell32 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

Zip unzip with Shell32

上一篇:UFLDL学习笔记(3)


下一篇:css-移动端模拟hover效果