Call 7-zip compressed files on Windows using C#

Use C# to call 7-zip compressed files on Windows

You can set the path of the output file or leave it blank. If you leave it blank, a .compressed package with the same name will be created in the compressed file.
You can set the password for the compressed package
You can set the encryption method of the compressed package (ASE-256). You can use LZMA but an error will be reported when encrypting the password.
You can set the format of the compressed package (zip). You can use 7z but an error will be reported when adding the password.
Added a limit on the maximum password length (98 characters, 7zip limit)
In the graphical interface of 7-ZIP, you can select 7z format compression and enter a Chinese password.

 using System;
using System.Diagnostics;

Compression of namespace files
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello, let’s start compressing files");

            ZipsHelper.CompressedInformation compressedInformation = new ZipsHelper.CompressedInformation(
               @"E:\Compressed File Test\Compressed File_Orgion\V_1696602827.txt",
               "",
                "",
               ZipsHelper.CompressedFileType.Zip,
               ZipsHelper.CompressedPackageEncryptionMode.AES256);
            //Compression E:\Compressed file test\Compressed file_Orgion\V1696602827.txt
            //Go to E:\Compressed File Test\Compressed File_Orgion\V1696602827.zip

            ZipsHelper.DoCompressedFile(compressedInformation);

            Console.ReadKey();

        }
    }

    /// <summary>
    /// zip file compression
    /// </summary>
    public class ZipsHelper
    {

        /// <summary>
        /// Compressed file
        /// </summary>
        public static void DoCompressedFile(CompressedInformation compressedInformation)
        {
            //Set the path to the 7-Zip executable file and modify it according to your installation path
            string sevenZipExePath = @"C:\Program Files\7-Zip\7z.exe";
            if (!System.IO.File.Exists(sevenZipExePath))
            {
                Console.WriteLine($"Unable to find 7z.exe, please check the path, the current path is: {sevenZipExePath}");
                return;
            }
           if (compressedInformation.Password.Length > 98)
            {
                Console.WriteLine($"Compression canceled, password length is too long, the maximum length is 98, the current length is: {compressedInformation.Password.Length}.");
                return;
            }
            string encryptionMethod;//Encryption method of compressed package
            if (compressedInformation.CompressedPackageEncryptionMode == CompressedPackageEncryptionMode.AES256)
            {
                encryptionMethod = "-mem=AES256";
            }
            //else if (compressedInformation.CompressedPackageEncryptionMode == CompressedPackageEncryptionMode.LZMA)
            //{
            //encryptionMethod = "-mhe=on -m0=BCJ2 -m1=LZMA2 -m2=LZMA2 -m3=LZMA2 -mb0:1 -mb0s1:2 -mb0s2:3";
            //}
            else
            {
                encryptionMethod = "-mem=AES256";
            }

            string format;//Set the format of the compressed package
            if (compressedInformation.CompressedFileType == CompressedFileType.Zip)
            {
                compressedInformation.CompressedFilePath + = ".zip";//Add the file suffix of the compressed package
                format = "zip";
            }
            else
            {
                format = "7z";
            }

            string arguments;//Compressed parameters
            //Build 7-Zip command line parameters
            if (compressedInformation.Password == "")//When the compression encryption method is selected but the password is empty, compression cannot be performed
            {
                arguments = $"a -t{format} "{compressedInformation.CompressedFilePath}" "{compressedInformation.FilePathToCompress}"";
            }
            else
            {
                arguments = $"a -t{format} "{compressedInformation.CompressedFilePath}" "{compressedInformation.FilePathToCompress}" {encryptionMethod} -p{compressedInformation.Password}";
            }


            Console.WriteLine(arguments);

            //Create a new process to run 7-Zip
            Process process = new Process();
            process.StartInfo.FileName = sevenZipExePath;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;

            // Start the 7-Zip process and wait for it to complete
            process.Start();
            process.WaitForExit();

            // Process the output results
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            if (string.IsNullOrEmpty(error))
            {
                Console.WriteLine("File compression successful!");
            }
            else
            {
                Console.WriteLine("File compression failed, error message:" + error);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(output);


        }


        /// <summary>
        /// Compressed package type
        /// </summary>
        public enum CompressedFileType
        {
            zip = 1,
            // _7Z = 2

        }

        /// <summary>
        /// Compressed package encryption format
        /// </summary>
        public enum CompressedPackageEncryptionMode
        {
            AES256,
            // LZMA,
        }


        public class CompressedInformation
        {
            /// <summary>
            /// Compressed file path
            /// </summary>
            private string filePathToCompress;

            /// <summary>
            /// Output file path
            /// </summary>
            private string compressedFilePath;

            /// <summary>
            /// password
            /// </summary>
            private string password;

            /// <summary>
            /// Compressed package type
            /// </summary>
            private CompressedFileType compressedFileType;


            /// <summary>
            /// Compressed package encryption format
            /// </summary>
            private CompressedPackageEncryptionMode compressedPackageEncryptionMode;

            public string FilePathToCompress { get => filePathToCompress; set => filePathToCompress = value; }
            public string CompressedFilePath { get => compressedFilePath; set => compressedFilePath = value; }
            public string Password { get => password; set => password = value; }
            public CompressedFileType CompressedFileType { get => compressedFileType; set => compressedFileType = value; }
            public CompressedPackageEncryptionMode CompressedPackageEncryptionMode { get => compressedPackageEncryptionMode; set => compressedPackageEncryptionMode = value; }

            /// <summary>
            /// Compression command parameters
            /// </summary>
            /// <param name="filePathToCompress">Compressed file path</param>
            /// <param name="compressedFilePath">Compressed package output path</param>
            /// <param name="password">Password</param>
            /// <param name="compressedFileType">Compressed package format</param>
            /// <param name="compressedPackageEncryptionMode">Compressed package encryption method</param>
            public CompressedInformation(string filePathToCompress,
                string compressedFilePath = "",
                string password = "",
                CompressedFileType compressedFileType = CompressedFileType.Zip,
                CompressedPackageEncryptionMode compressedPackageEncryptionMode = CompressedPackageEncryptionMode.AES256)
            {
                this.FilePathToCompress = filePathToCompress;
                this.CompressedFilePath = compressedFilePath;
                this.Password = password;
                this.CompressedFileType = compressedFileType;
                this.CompressedPackageEncryptionMode = compressedPackageEncryptionMode;

                if (compressedFilePath == "")
                {
                    GetFileNameAndExtension(filePathToCompress, out compressedFilePath);
                    this.CompressedFilePath = compressedFilePath;
                }
            }

            public static void GetFileNameAndExtension(string filePath, out string pathWithoutExtension)
            {
                pathWithoutExtension = System.IO.Path.ChangeExtension(filePath, null); // Remove file suffix
            }


        }

    }

}