如何使用 7zip C# 将多个文件夹存档到单个 Zip 文件中

本文关键字:单个 Zip 文件 文件夹 7zip 何使用 | 更新日期: 2023-09-27 18:36:50

我有多个文件夹路径:

SourceFilePath="C:'Users'Anuj'Desktop'PSI"
SourceFilePath1="C:'Users'Anuj'Desktop'Google"
SourceFilePath2="C:'Users'Anuj'Desktop'Isp"

我想使用 7zip 命令行代码和 Zip 密码压缩这些路径。

编辑:

     //Declare and instantiate a new process component.
                    System.Diagnostics.Process proc;
                    proc = new System.Diagnostics.Process();
                    //Do not receive an event when the process exits.
                    proc.EnableRaisingEvents = false;

                    //The "/C" Tells Windows to Run The Command then Terminate 
                    string strCmdLine;
                    strCmdLine = "/C cd c:''Program Files''7-Zip'' ";
                    strCmdLine += " & 7z a  "// here i need help 
                    System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
                    proc.Close();

我实际做了什么:

 var MultiplePathFolders=SourceFilePath+SourceFilePath1+SourceFilePath2
        System.Diagnostics.Process proc;
  proc = new System.Diagnostics.Process();
  proc.EnableRaisingEvents = false;
   string strCmdLine;
     strCmdLine = "/C cd c:''Program Files''7-Zip'' ";
      strCmdLine += " & 7z a " + SyncPath + "''" + ZipName + "-FileName.7z " + MultiplePathFolders + " -p" + DecryptedPassword + "";
  System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
   proc.Close();

如何使用 7zip C# 将多个文件夹存档到单个 Zip 文件中

你可以这样做:

string yourPassWord = "Hello";
string yourZipPath="D:''my7z.7z";                      // Your 7z file path
//string yourZipPath="D:''myZip.zip";                  // Or your zip file path
System.Diagnostics.Process proc;
proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
List<string> foldersAndFiles = new List<string>();
foldersAndFiles.Add("D:''F1");                        // Add folder
foldersAndFiles.Add("D:''F2");                        // Add folder
foldersAndFiles.Add("D:''file.txt");                  // Add file
string cmd = "a -tzip -p'"" + yourPassWord + "'" '"" + your7zPath + "'"";
foreach(var item in foldersAndFiles)
{
    cmd += " '"" + item + "'"";
}
proc.StartInfo.FileName
    = "c:''Program Files''7-Zip''7z.exe";            // Be sure this file exist.
proc.StartInfo.Arguments = cmd;
proc.Start();
proc.Close();