Search code examples
c#7zip

How can I archive multiple Folders into a single Zip File using 7zip C#?


I have Multiple folder path:

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

I want to Compress These path Using 7zip Command line code along with a Zip Password.

Edit:

     //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();

What I actually Did:

 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();

Solution

  • You can do like this:

    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();