[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         }


原文地址:https://www.cnblogs.com/LeoWong/p/1913306.html