• [C#]使用SHELL32压缩文件


     1         enum SHFILEOPSTRUCT : int
     2         {
     3             /// <summary>
     4             /// Default. No options specified.
     5             /// </summary>
     6             FOF_DEFAULT = 0,
     7             /// <summary>
     8             /// Do not display a progress dialog box.
     9             /// </summary>
    10             FOF_SILENT = 4,
    11             /// <summary>
    12             /// Rename the target file if a file exists at the target location with the same name.
    13             /// </summary>
    14             FOF_RENAMEONCOLLISION = 8,
    15             /// <summary>
    16             /// Click "Yes to All" in any dialog box displayed.
    17             /// </summary>
    18             FOF_NOCONFIRMATION = 16,
    19             /// <summary>
    20             /// Preserve undo information, if possible.
    21             /// </summary>
    22             FOF_ALLOWUNDO = 64,
    23             /// <summary>
    24             /// Perform the operation only if a wildcard file name (*.*) is specified.
    25             /// </summary>
    26             FOF_FILESONLY = 128,
    27             /// <summary>
    28             /// Display a progress dialog box but do not show the file names.
    29             /// </summary>
    30             FOF_SIMPLEPROGRESS = 256,
    31             /// <summary>
    32             /// Do not confirm the creation of a new directory if the operation requires one to be created.
    33             /// </summary>
    34             FOF_NOCONFIRMMKDIR = 512,
    35             /// <summary>
    36             /// Do not display a user interface if an error occurs.
    37             /// </summary>
    38             FOF_NOERRORUI = 1024,
    39             /// <summary>
    40             /// Do not copy the security attributes of the file
    41             /// </summary>
    42             FOF_NOCOPYSECURITYATTRIBS = 2048,
    43             /// <summary>
    44             /// Disable recursion.
    45             /// </summary>
    46             FOF_NORECURSION = 4096,
    47             /// <summary>
    48             /// Do not copy connected files as a group. Only copy the specified files.
    49             /// </summary>
    50             FOF_NO_CONNECTED_ELEMENTS = 9182
    51         }
    52 
    53         /// <summary>
    54         /// Zip function calls Shell32, Interop.Shell32.dll is needed
    55         /// </summary>
    56         /// <param name="filesInFolder">Specify a folder containing the zip source files</param>
    57         /// <param name="zipFile">Specify the final zip file name, with ".zip" extension</param>
    58         private void ZipMe(string filesInFolder, string zipFile)
    59         {
    60             if (filesInFolder == null || filesInFolder.Trim() == ""return;
    61             if (zipFile == null || zipFile.Trim() == ""return;
    62             if (!Directory.Exists(filesInFolder)) return;
    63 
    64             Shell32.ShellClass sh = new Shell32.ShellClass();
    65             try
    66             {
    67                 if (File.Exists(zipFile)) File.Delete(zipFile);
    68                 //Create an empty zip file
    69                 byte[] emptyzip = new byte[] { 80755600000000000000000000 };
    70 
    71                 FileStream fs = File.Create(zipFile);
    72                 fs.Write(emptyzip, 0, emptyzip.Length);
    73                 fs.Flush();
    74                 fs.Close();
    75 
    76                 Shell32.Folder srcFolder = sh.NameSpace(filesInFolder);
    77                 Shell32.Folder destFolder = sh.NameSpace(zipFile);
    78                 Shell32.FolderItems items = srcFolder.Items();
    79                 destFolder.CopyHere(items, SHFILEOPSTRUCT.FOF_SILENT | SHFILEOPSTRUCT.FOF_NOCONFIRMATION);
    80             }
    81             finally
    82             {
    83                 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(sh);
    84             }
    85         }


  • 相关阅读:
    「两千年中公历转换」数据库介绍
    [转]Web中使用Word控件。(DSOFramer )
    解决DRIVE_IRQL_NOT_LESS_OR_EQUAL的方法
    Html Img的几个属性_存在个问题
    不错的开源C#博客_BlogEngine.net
    [转]引用指定的namespace 解决命名空间冲突的错误
    [原]不太完善的图像合并程序VS2005CSharp_有目录监控_TIF_JPG输出
    [转]JS小游戏_9格的棋
    JS小游戏_能坚持几秒
    [转]前台JS限制上传图片质量大小和尺寸!
  • 原文地址:https://www.cnblogs.com/LeoWong/p/1913306.html
Copyright © 2020-2023  润新知