C# 使用Shell32 进行压缩与解压缩的标准且正确做法

具体参见代码,就不再做其他相关说明了;这是用于上线产品中的源代码。

解压部分:

 1         /// <summary>
 2         /// 解压压缩包
 3         /// </summary>
 4         /// <param name="zipPath">压缩包的绝对路径</param>
 5         /// <param name="dirPath">解压到的文件夹路径</param>
 6         public static void UnZip(string zipPath, string dirPath)
 7         {
 8             // 判断ZIP文件是否存在
 9             if (!System.IO.File.Exists(zipPath))
10                 throw new FileNotFoundException();
11 
12             // 如果目标目录不存在就创建
13             if (!Directory.Exists(dirPath))
14                 Directory.CreateDirectory(dirPath);
15 
16             //Shell的类型
17             Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
18 
19             //创建Shell对象
20             object shell = Activator.CreateInstance(shellAppType);
21 
22             // ZIP文件
23             Shell32.Folder srcFile = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { zipPath });
24 
25             // 解压到的目录
26             Shell32.Folder destFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { dirPath });
27 
28             // 遍历ZIP文件
29             foreach (var file in srcFile.Items())
30             {
31                 destFolder.CopyHere(file, 4 | 16);
32             }
33         }

 

压缩部分:

 1         /// <summary>
 2         /// 压缩文件夹内的文件到zip文件中
 3         /// </summary>
 4         /// <param name="dirPath">需要压缩的目录绝对路径</param>
 5         /// <param name="zipPath">压缩后的绝对路径</param>
 6         public static void ZipDir(string dirPath, string zipPath)
 7         {
 8             // 不存在地址则创建
 9             if (!System.IO.File.Exists(zipPath))
10                 using (System.IO.File.Create(zipPath)) { };
11 
12             //Shell的类型
13             Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
14 
15             //创建Shell对象
16             object shell = Activator.CreateInstance(shellAppType);
17 
18             // ZIP文件
19             Shell32.Folder destFile = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { zipPath });
20 
21             // 源文件夹
22             Shell32.Folder srcFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { dirPath });
23 
24             //已经压缩进去的文件
25             int zipFileCount = 0;
26 
27             // 遍历并压缩源文件夹的文件到Zip中,因为压缩需要消耗时间需要进行等待
28             foreach (var file in srcFolder.Items())
29             {
30                 destFile.CopyHere(file, 4 | 16);
31                 zipFileCount++;
32 
33                 // 判断后进行等待
34                 while (destFile.Items().Count < zipFileCount)
35                 {
36                     // 因为压缩需要消耗时间需要进行等待
37                     System.Threading.Thread.Sleep(10);
38                 }
39             }
40         }

需要引用的DLL

C:\Windows\System32\Shell32.dll 嵌入互操作类型为False

注意:CopyHere中的那个枚举参数建议使用代码中的值不需要做其它处理,其它参数值可参见 http://blog.csdn.net/wzhiu/article/details/18224725

转载于:https://www.cnblogs.com/meifeipeng/p/8340328.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值