欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#在Windows上調用7-zip實現(xiàn)壓縮文件

 更新時間:2023年10月27日 14:20:45   作者:先生沉默先  
這篇文章主要為大家詳細介紹了C#如何在Windows上調用7-zip實現(xiàn)壓縮文件,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以學習一下

使用C#在Windows上調用7-zip壓縮文件

  • 可以設置輸出文件的路徑也可以留空,留空則會在壓縮文件創(chuàng)建一個同名的.壓縮包
  • 可以設置壓縮包的密碼
  • 可以設置壓縮包的加密方式(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();

        }
    }

    /// <summary>
    /// zip文件壓縮
    /// </summary>
    public class ZipsHelper
    {

        /// <summary>
        /// 壓縮文件
        /// </summary>
        public static void DoCompressedFile(CompressedInformation compressedInformation)
        {
            // 設置7-Zip可執(zhí)行文件的路徑,根據(jù)你的安裝路徑進行修改
            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;//壓縮的參數(shù)
            //構建7-Zip命令行參數(shù) 
            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);

            // 創(chuàng)建一個新的進程來運行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);


        }


        /// <summary>
        /// 壓縮包類型
        /// </summary>
        public enum CompressedFileType
        {
            Zip = 1,
            //  _7Z = 2

        }

        /// <summary>
        /// 壓縮包加密格式
        /// </summary>
        public enum CompressedPackageEncryptionMode
        {
            AES256,
            //  LZMA,
        }


        public class CompressedInformation
        {
            /// <summary>
            /// 壓縮文件路徑
            /// </summary>
            private string filePathToCompress;

            /// <summary>
            /// 輸出文件路徑
            /// </summary>
            private string compressedFilePath;

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

            /// <summary>
            /// 壓縮包類型
            /// </summary>
            private CompressedFileType compressedFileType;


            /// <summary>
            ///  壓縮包加密格式
            /// </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>
            /// 壓縮命令參數(shù)
            /// </summary>
            /// <param name="filePathToCompress">壓縮文件路徑</param>
            /// <param name="compressedFilePath">壓縮包輸出路徑</param>
            /// <param name="password">密碼</param>
            /// <param name="compressedFileType">壓縮包格式</param>
            /// <param name="compressedPackageEncryptionMode">壓縮包加密方式</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); // 去除文件后綴
            }


        }

    }

}

到此這篇關于C#在Windows上調用7-zip實現(xiàn)壓縮文件的文章就介紹到這了,更多相關C#壓縮文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#獲取指定文件著作權信息的方法

    C#獲取指定文件著作權信息的方法

    這篇文章主要介紹了C#獲取指定文件著作權信息的方法,涉及C#中FileVersionInfo類的使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#利用iTextSharp組件給PDF文檔添加圖片/文字水印

    C#利用iTextSharp組件給PDF文檔添加圖片/文字水印

    這篇文章主要給大家介紹了關于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 使用C#實現(xiàn)數(shù)據(jù)結構堆的代碼

    使用C#實現(xiàn)數(shù)據(jù)結構堆的代碼

    這篇文章主要介紹了使用C#實現(xiàn)數(shù)據(jù)結構堆,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 解析C#中#region與#if的作用

    解析C#中#region與#if的作用

    以下是對C#中#region與#if的作用進行了詳細的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C#中ListView控件實現(xiàn)窗體代碼

    C#中ListView控件實現(xiàn)窗體代碼

    這篇文章主要介紹了C#中ListView控件實現(xiàn)窗體的核心代碼,非常不錯,具有參考借鑒價值,對c#listview相關知識感興趣的朋友一起學習吧
    2016-08-08
  • c#后臺輸出javascript語句示例程序

    c#后臺輸出javascript語句示例程序

    一個很不錯的b/s前臺輸出彈出對話框、后臺寫javascript語句、后臺直接關閉web頁面及一個集成了常用驗證的通用類,十分的方便。代碼如下
    2013-12-12
  • C#(.net)水印圖片的生成完整實例

    C#(.net)水印圖片的生成完整實例

    這篇文章主要介紹了C#(.net)水印圖片的生成方法,以一個完整實例的形式講述了水印圖片的生成技巧,非常實用,需要的朋友可以參考下
    2014-09-09
  • C#的FileSystemWatcher用法實例詳解

    C#的FileSystemWatcher用法實例詳解

    這篇文章主要介紹了C#的FileSystemWatcher用法,以實例形似詳細分析了FileSystemWatcher控件主要功能,并總結了FileSystemWatcher控件使用的技巧,需要的朋友可以參考下
    2014-11-11
  • c#日志記錄幫助類分享

    c#日志記錄幫助類分享

    這篇文章主要介紹了c#日志記錄幫助類,可以設置記錄的日志類型,需要的朋友可以參考下
    2014-03-03
  • C#獲取DICOM圖像像素的像素值的代碼詳解

    C#獲取DICOM圖像像素的像素值的代碼詳解

    DICOM即醫(yī)學數(shù)字成像和通信,是醫(yī)學圖像和相關信息的國際標準(ISO 12052),它定義了質量能滿足臨床需要的可用于數(shù)據(jù)交換的醫(yī)學圖像格式,這篇文章主要介紹了C#獲取DICOM圖像像素的像素值的方法,需要的朋友可以參考下
    2024-07-07

最新評論