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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值