最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

使用C#在Windows上調(diào)用7-zip壓縮文件

  • 可以設(shè)置輸出文件的路徑也可以留空,留空則會(huì)在壓縮文件創(chuàng)建一個(gè)同名的.壓縮包
  • 可以設(shè)置壓縮包的密碼
  • 可以設(shè)置壓縮包的加密方式(ASE-256),可以使用LZMA但是加密碼會(huì)報(bào)錯(cuò)
  • 可以設(shè)置壓縮包的格式(zip),可以使用7z但是加密碼會(huì)報(bào)錯(cuò)
  • 添加了密碼最大長(zhǎng)度的限制(98個(gè)字符,7zip限制的)
  • 在7-ZIP的圖形界面可以選擇7z格式壓縮可以輸入中文的密碼

示例代碼

using System;
using System.Diagnostics;

namespace 文件的壓縮
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("你好,接下來(lái)開(kāi)始?jí)嚎s文件");

            ZipsHelper.CompressedInformation compressedInformation = new ZipsHelper.CompressedInformation(
               @"E:\壓縮文件測(cè)試\壓縮文件_Orgion\V_1696602827.txt",
               "",
                "",
               ZipsHelper.CompressedFileType.Zip,
               ZipsHelper.CompressedPackageEncryptionMode.AES256);
            //壓縮  E:\壓縮文件測(cè)試\壓縮文件_Orgion\V1696602827.txt
            //到     E:\壓縮文件測(cè)試\壓縮文件_Orgion\V1696602827.zip

            ZipsHelper.DoCompressedFile(compressedInformation);

            Console.ReadKey();

        }
    }

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

        /// <summary>
        /// 壓縮文件
        /// </summary>
        public static void DoCompressedFile(CompressedInformation compressedInformation)
        {
            // 設(shè)置7-Zip可執(zhí)行文件的路徑,根據(jù)你的安裝路徑進(jìn)行修改
            string sevenZipExePath = @"C:\Program Files\7-Zip\7z.exe";
            if (!System.IO.File.Exists(sevenZipExePath))
            {
                Console.WriteLine($"未能找到7z.exe ,請(qǐng)檢查路徑,當(dāng)前路徑是:{sevenZipExePath}");
                return;
            }
           if (compressedInformation.Password.Length > 98)
            {
                Console.WriteLine($"壓縮取消,密碼長(zhǎng)度過(guò)長(zhǎng),最大長(zhǎng)度是98,當(dāng)前長(zhǎng)度是:{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;//設(shè)置壓縮包的格式
            if (compressedInformation.CompressedFileType == CompressedFileType.Zip)
            {
                compressedInformation.CompressedFilePath += ".zip";//添加壓縮包的文件后綴
                format = "zip";
            }
            else
            {
                format = "7z";
            }

            string arguments;//壓縮的參數(shù)
            //構(gòu)建7-Zip命令行參數(shù) 
            if (compressedInformation.Password == "")//當(dāng)選擇了壓縮的加密方式但是密碼為空的時(shí)候不能壓縮
            {
                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)建一個(gè)新的進(jìn)程來(lái)運(yùn)行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;

            // 啟動(dòng)7-Zip進(jìn)程并等待其完成
            process.Start();
            process.WaitForExit();

            // 處理輸出結(jié)果
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            if (string.IsNullOrEmpty(error))
            {
                Console.WriteLine("文件壓縮成功!");
            }
            else
            {
                Console.WriteLine("文件壓縮失敗,錯(cuò)誤信息:" + 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); // 去除文件后綴
            }


        }

    }

}

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

相關(guān)文章

  • C# 線程安全詳解

    C# 線程安全詳解

    這篇文章主要介紹了c# 線程安全的用法原理及使用示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下,希望能夠給你帶來(lái)幫助
    2021-09-09
  • 使用C#和SQL Server實(shí)現(xiàn)自動(dòng)清理功能的完整方案

    使用C#和SQL Server實(shí)現(xiàn)自動(dòng)清理功能的完整方案

    在現(xiàn)代軟件開(kāi)發(fā)中,數(shù)據(jù)管理是一個(gè)至關(guān)重要的環(huán)節(jié),隨著時(shí)間的推移,數(shù)據(jù)庫(kù)中會(huì)積累大量過(guò)期或冗余的數(shù)據(jù),這些數(shù)據(jù)不僅占用存儲(chǔ)空間,還會(huì)影響系統(tǒng)性能,本文將詳細(xì)介紹如何使用C#和SQL Server構(gòu)建一個(gè)功能完善的自動(dòng)清理系統(tǒng),需要的朋友可以參考下
    2026-01-01
  • Unity實(shí)現(xiàn)批量Build打包詳解

    Unity實(shí)現(xiàn)批量Build打包詳解

    一般來(lái)講如果項(xiàng)目是PC或Android、IOS端不會(huì)有批量Build打包這樣的需求,但如果項(xiàng)目是WebGL端可能會(huì)遇到這樣的需求。本文主要為大家介紹Unity中如何實(shí)現(xiàn)Build批量打包的,需要的朋友可以參考一下
    2021-12-12
  • C#讀寫(xiě)文本文件的方法

    C#讀寫(xiě)文本文件的方法

    這篇文章主要介紹了C#讀寫(xiě)文本文件的方法,實(shí)例分析了C#操作文本文件的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • C#自定義控件添加右鍵菜單的方法

    C#自定義控件添加右鍵菜單的方法

    這篇文章主要介紹了C#自定義控件添加右鍵菜單的方法,本文用到control控件,專門(mén)自定義右鍵菜單,下面小編給大家整理下,有需要的小伙伴可以來(lái)參考下
    2015-08-08
  • C# 調(diào)用騰訊即時(shí)通信 IM的示例

    C# 調(diào)用騰訊即時(shí)通信 IM的示例

    這篇文章主要介紹了C# 調(diào)用騰訊即時(shí)通信 IM的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • 用C#操縱IIS(代碼)

    用C#操縱IIS(代碼)

    用C#操縱IIS(代碼)...
    2007-03-03
  • C#操作Excel相關(guān)方法總結(jié)

    C#操作Excel相關(guān)方法總結(jié)

    這篇文章主要介紹了C#操作Excel相關(guān)方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 深入解析c#中枚舉類型的定義與使用

    深入解析c#中枚舉類型的定義與使用

    以下是對(duì)c#中枚舉類型的定義與使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下
    2013-08-08
  • 使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能

    使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能

    這篇文章主要為大家介紹了使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評(píng)論

镇平县| 天水市| 右玉县| 牡丹江市| 吉安市| 陇西县| 镇赉县| 景泰县| 勃利县| 辛集市| 天气| 同江市| 紫金县| 林州市| 容城县| 六盘水市| 阳高县| 邵武市| 横峰县| 宜章县| 吉木乃县| 全南县| 余姚市| 沽源县| 香河县| 文登市| 玛纳斯县| 乐业县| 嘉峪关市| 安丘市| 藁城市| 冀州市| 万州区| 江源县| 清水河县| 桑日县| 远安县| 唐海县| 专栏| 中山市| 庆城县|