• 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

  • 相关阅读:
    五、MongoDB的索引
    四、MongoDB的查询
    各模块启动
    HBase1.2.6 javaapi查看rowkey 所在分区等信息
    HBase1.2.6 预分区后,数据不进入预定分区的一个 bug
    SparkStreaming程序设计
    SparkSQL程序设计
    Spark SQL概述
    常用RDD
    spark程序设计
  • 原文地址:https://www.cnblogs.com/mq0036/p/15931344.html
Copyright © 2020-2023  润新知