使用C#在Windows上调用7-zip压缩文件

使用C#在Windows上调用7-zip压缩文件

可以设置输出文件的路径也可以留空,留空则会在压缩文件创建一个同名的.压缩包
可以设置压缩包的密码
可以设置压缩包的加密方式(ASE-256),可以使用LZMA但是加密码会报错
可以设置压缩包的格式(zip),可以使用7z但是加密码会报错
添加了密码最大长度的限制(98个字符,7zip限制的)
在7-ZIP的图形界面可以选择7z格式压缩可以输入中文的密码

 using System;
using System.Diagnostics;

namespace 文件的压缩
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("你好,接下来开始压缩文件");

            ZipsHelper.CompressedInformation compressedInformation = new ZipsHelper.CompressedInformation(
               @"E:\压缩文件测试\压缩文件_Orgion\V_1696602827.txt",
               "",
                "",
               ZipsHelper.CompressedFileType.Zip,
               ZipsHelper.CompressedPackageEncryptionMode.AES256);
            //压缩  E:\压缩文件测试\压缩文件_Orgion\V1696602827.txt
            //到     E:\压缩文件测试\压缩文件_Orgion\V1696602827.zip

            ZipsHelper.DoCompressedFile(compressedInformation);

            Console.ReadKey();

        }
    }

    /// 
    /// zip文件压缩
    /// 
    public class ZipsHelper
    {

        /// 
        /// 压缩文件
        /// 
        public static void DoCompressedFile(CompressedInformation compressedInformation)
        {
            // 设置7-Zip可执行文件的路径,根据你的安装路径进行修改
            string sevenZipExePath = @"C:\Program Files\7-Zip\7z.exe";
            if (!System.IO.File.Exists(sevenZipExePath))
            {
                Console.WriteLine($"未能找到7z.exe ,请检查路径,当前路径是:{sevenZipExePath}");
                return;
            }
           if (compressedInformation.Password.Length > 98)
            {
                Console.WriteLine($"压缩取消,密码长度过长,最大长度是98,当前长度是:{compressedInformation.Password.Length}。");
                return;
            }
            string encryptionMethod;//压缩包的加密方式
            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;//设置压缩包的格式
            if (compressedInformation.CompressedFileType == CompressedFileType.Zip)
            {
                compressedInformation.CompressedFilePath += ".zip";//添加压缩包的文件后缀
                format = "zip";
            }
            else
            {
                format = "7z";
            }

            string arguments;//压缩的参数
            //构建7-Zip命令行参数 
            if (compressedInformation.Password == "")//当选择了压缩的加密方式但是密码为空的时候不能压缩
            {
                arguments = $"a -t{format} \"{compressedInformation.CompressedFilePath}\" \"{compressedInformation.FilePathToCompress}\"";
            }
            else
            {
                arguments = $"a -t{format} \"{compressedInformation.CompressedFilePath}\" \"{compressedInformation.FilePathToCompress}\" {encryptionMethod} -p{compressedInformation.Password}";
            }


            Console.WriteLine(arguments);

            // 创建一个新的进程来运行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;

            // 启动7-Zip进程并等待其完成
            process.Start();
            process.WaitForExit();

            // 处理输出结果
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            if (string.IsNullOrEmpty(error))
            {
                Console.WriteLine("文件压缩成功!");
            }
            else
            {
                Console.WriteLine("文件压缩失败,错误信息:" + error);
            }

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


        }


        /// 
        /// 压缩包类型
        /// 
        public enum CompressedFileType
        {
            Zip = 1,
            //  _7Z = 2

        }

        /// 
        /// 压缩包加密格式
        /// 
        public enum CompressedPackageEncryptionMode
        {
            AES256,
            //  LZMA,
        }


        public class CompressedInformation
        {
            /// 
            /// 压缩文件路径
            /// 
            private string filePathToCompress;

            /// 
            /// 输出文件路径
            /// 
            private string compressedFilePath;

            /// 
            /// 密码
            /// 
            private string password;

            /// 
            /// 压缩包类型
            /// 
            private CompressedFileType compressedFileType;


            /// 
            ///  压缩包加密格式
            /// 
            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; }

            /// 
            /// 压缩命令参数
            /// 
            /// 压缩文件路径
            /// 压缩包输出路径
            /// 密码
            /// 压缩包格式
            /// 压缩包加密方式
            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); // 去除文件后缀
            }


        }

    }

}




你可能感兴趣的:(#,C#,c#,windows,7-zip)