Calling 7-zip compressed files on Windows using C#
- You can set the path of the output file or leave it blank. Leaving it blank will create a compressed package of the same name in the compressed file.
- You can set the password of the compressed package
- You can set the encryption method of the compressed packet (ASE-256). You can use LZMA but the encryption code will report an error.
- You can set the format (zip) of the compressed package, you can use 7z, but the password will report an error.
- Added a limit on password maximum length (98 characters, 7zip limit)
- In the 7-ZIP graphical interface, you can select the 7z format compression and enter the Chinese password.
Sample code
using System; using ; namespace File compression { internal class Program { private static void Main(string[] args) { ("Hello, start zipping the file next"); compressedInformation = new ( @"E:\Compressed File Test\Compressed File_Orgion\V_1696602827.txt", "", "", , .AES256); //Compress E:\Compressed file test\Compressed file_Orgion\ //To E:\Compressed File Test\Compressed File_Orgion\ (compressedInformation); (); } } /// <summary> /// zip file compression /// </summary> public class ZipsHelper { /// <summary> /// Compress files /// </summary> public static void DoCompressedFile(CompressedInformation compressedInformation) { // Set the path of the 7-Zip executable file and modify it according to your installation path string sevenZipExePath = @"C:\Program Files\7-Zip\"; if (!(sevenZipExePath)) { ($"Not found ,Please check the path,The current path is:{sevenZipExePath}"); return; } if ( > 98) { ($"Compression Cancel,Password length is too long,The maximum length is98,The current length is:{}。"); return; } string encryptionMethod;//Encryption method of compressed packet if ( == CompressedPackageEncryptionMode.AES256) { encryptionMethod = "-mem=AES256"; } //else if ( == ) //{ //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 ( == ) { += ".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 ( == "")//When the compression encryption method is selected but the password is empty, it cannot be compressed { arguments = $"a -t{format} \"{}\" \"{}\""; } else { arguments = $"a -t{format} \"{}\" \"{}\" {encryptionMethod} -p{}"; } (arguments); // Create a new process to run 7-Zip Process process = new Process(); = sevenZipExePath; = arguments; = false; = true; = true; = true; // Start the 7-Zip process and wait for it to complete (); (); // Process the output result string output = (); string error = (); if ((error)) { ("File compression succeeded!"); } else { ("File compression failed, error message:" + error); } (); (); (); (); (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> /// Compress 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> /// Compress command parameters /// </summary> /// <param name="filePathToCompress">Compress 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 = , CompressedPackageEncryptionMode compressedPackageEncryptionMode = CompressedPackageEncryptionMode.AES256) { = filePathToCompress; = compressedFilePath; = password; = compressedFileType; = compressedPackageEncryptionMode; if (compressedFilePath == "") { GetFileNameAndExtension(filePathToCompress, out compressedFilePath); = compressedFilePath; } } public static void GetFileNameAndExtension(string filePath, out string pathWithoutExtension) { pathWithoutExtension = (filePath, null); // Remove file suffix } } } }
This is the article about C# calling 7-zip to implement compressed files on Windows. For more related C# compressed files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!