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

C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄

 更新時(shí)間:2025年01月08日 11:39:26   作者:秋月的私語(yǔ)  
SevenZipSharp壓縮/解壓(.7z?.zip)”是指使用SevenZipSharp庫(kù)進(jìn)行7z和zip格式的文件壓縮與解壓縮操作,SevenZipSharp是C#語(yǔ)言封裝的7-Zip?API,它使得在.NET環(huán)境中調(diào)用7-Zip的功能變得簡(jiǎn)單易行,本文給大家介紹了C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄

C#使用SevenZipSharp的操作

封裝了一個(gè)類,方便使用SevenZipSharp,支持加入進(jìn)度顯示事件。

雙重加密壓縮工具范例:

using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ProcessItems
{
    class SevenZipSharpUser
    {
        // 假設(shè)這是某個(gè)類中的一個(gè)事件定義
        public  event EventHandler<ProgressEventArgs> ProgressUpdated = null;
 
        public  static bool SetSetLibraryPath()
        {
            //設(shè)置庫(kù)路徑
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string dllPath = GetAppropriate7zDllPath(currentDirectory);
 
            if (!File.Exists(dllPath))
            {
                return false;
            }
 
            SevenZipSharpUser.SetSetLibraryPath(dllPath);
 
            return true;
        }
 
        public static void SetSetLibraryPath(string s7zDllPath)
        {
            SevenZipBase.SetLibraryPath(s7zDllPath);
        }
 
        public  bool CompressItem(string inputItem, string outputFile, string password = null)
        {
            string directory = Path.GetDirectoryName(outputFile);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
 
            if (Directory.Exists(inputItem))
            {
                return CompressDir(inputItem, outputFile, password);
            }
            else if (File.Exists(inputItem))
            {
                return CompressFile(inputItem, outputFile, password);
            }
 
            return false;
        }
 
        public  bool DoubleCompressItem(string inputItem, string outputFile, string password1 = null, string password2 = null)
        {
            string directory = Path.GetDirectoryName(outputFile);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(outputFile);
 
            string sFirstDstPath = Path.Combine(directory, $"{fileNameWithoutExtension}{".7zx"}");
 
            CompressItem(inputItem, sFirstDstPath, password1);
            CompressItem(sFirstDstPath, outputFile, password2);
 
            File.Delete(sFirstDstPath);
 
            return false;
        }
 
        public  string GetUniqueFilePath(string filePath)
        {
            string directory = Path.GetDirectoryName(filePath);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            string extension = Path.GetExtension(filePath);
 
            int counter = 1;
            string newFilePath = filePath;
 
            while (File.Exists(newFilePath))
            {
                newFilePath = Path.Combine(directory, $"{fileNameWithoutExtension}{counter}{extension}");
                counter++;
            }
 
            return newFilePath;
        }
 
        public  bool CompressFile(string inputFile, string outputFile, string password = null)
        {
            try
            {
                // 檢查輸入文件是否存在
                if (!File.Exists(inputFile))
                {
                    throw new FileNotFoundException("輸入文件不存在。", inputFile);
                }
 
                // 創(chuàng)建 SevenZipCompressor 實(shí)例
                var compressor = new SevenZipCompressor();
                // 設(shè)置壓縮級(jí)別和檔案格式
                compressor.CompressionLevel = CompressionLevel.Normal;
                compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
 
                // 訂閱進(jìn)度更新事件
                if (ProgressUpdated != null)
                {
                    compressor.Compressing += ProgressUpdated;
                }
 
                // 壓縮文件
                compressor.CompressFilesEncrypted(outputFile, password, inputFile);
 
                if (ProgressUpdated != null)
                {
                    compressor.Compressing -= ProgressUpdated;
                }
 
                // 壓縮成功后返回 true
                return true;
            }
            catch (Exception ex)
            {
                // 在發(fā)生異常時(shí)記錄日志、拋出異?;蚍祷?false
                // 這里簡(jiǎn)單地返回 false,但你可以根據(jù)需要更改此行為
                Console.WriteLine($"壓縮文件時(shí)發(fā)生錯(cuò)誤: {ex.Message}");
                return false;
            }
            finally
            {
 
            }
        }
 
        public  bool CompressDir(string stInputDir, string stOutputFile, string stPwd)
        {
            try
            {
                // 檢查輸入文件是否存在
                if (!Directory.Exists(stInputDir))
                {
                    throw new FileNotFoundException("輸入目錄不存在。", stInputDir);
                }
 
                // 創(chuàng)建 SevenZipCompressor 實(shí)例
                var compressor = new SevenZipCompressor();
                // 設(shè)置壓縮級(jí)別和檔案格式
                compressor.CompressionLevel = CompressionLevel.Normal;
                compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
 
                // 訂閱進(jìn)度更新事件
                if (ProgressUpdated != null)
                {
                    compressor.Compressing += ProgressUpdated;
                }
 
                // 壓縮文件
                compressor.CompressDirectory(stInputDir, stOutputFile, stPwd);
 
                if (ProgressUpdated != null)
                {
                    compressor.Compressing -= ProgressUpdated;
                }
 
                // 壓縮成功后返回 true
                return true;
            }
            catch (Exception ex)
            {
                // 在發(fā)生異常時(shí)記錄日志、拋出異?;蚍祷?false
                // 這里簡(jiǎn)單地返回 false,但你可以根據(jù)需要更改此行為
                Console.WriteLine($"壓縮文件時(shí)發(fā)生錯(cuò)誤: {ex.Message}");
                return false;
            }
            finally
            {
 
            }
        }
 
        private static string GetAppropriate7zDllPath(string basePath)
        {
            string dllName = "7z.dll";
            string dllPath = Path.Combine(basePath, dllName);
 
            // Check if the system is 64-bit or 32-bit
            if (Environment.Is64BitOperatingSystem)
            {
                // If the system is 64-bit, check for a specific 64-bit version of the DLL
                string dll64Path = Path.Combine(basePath, "7z.dll"); // Example name for 64-bit version
                if (File.Exists(dll64Path))
                {
                    return dll64Path;
                }
                // If the specific 64-bit version is not found, fall back to the generic name
            }
            else
            {
                // If the system is 32-bit, check for a specific 32-bit version of the DLL
                string dll32Path = Path.Combine(basePath, "7-zip32.dll"); // Example name for 32-bit version
                if (File.Exists(dll32Path))
                {
                    return dll32Path;
                }
                // If the specific 32-bit version is not found, fall back to the generic name
            }
 
            // If neither specific version is found, return the generic DLL name (which might be a universal version or an error)
            return dllPath;
        }
    }
}

使用方法:

            //設(shè)置庫(kù)
            if (!SevenZipSharpUser.SetSetLibraryPath())
            {
                MessageBox.Show("7z.dll庫(kù)引用失敗!", "錯(cuò)誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
            //這里是處理任務(wù)邏輯開(kāi)始========start===========
            if (itemInfo.bDoubleCompress)
            {
                szu.DoubleCompressItem(itemInfo.sItemPath, itemInfo.sDstPath, itemInfo.sPassword1, itemInfo.sPassword2);
            }
            else
            {
                szu.CompressItem(itemInfo.sItemPath, itemInfo.sDstPath, itemInfo.sPassword1);
            }
 
            //這里是處理任務(wù)邏輯結(jié)束========end=============

拓展:C#使用SevenZipSharp壓縮解壓文件

首先程序需要用到三個(gè)DLL文件,分別是:SevenZipSharp.dll、7z.dll、7z64.dll,其中SevenZipSharp.dll需要程序進(jìn)行引用,而其他兩個(gè)文件給代碼使用,其中7z.dll是32位,7z64.dll是64位的。(此處需要注意,這里的32位與64位指的是程序,而不是操作系統(tǒng),即指的是VS中右鍵項(xiàng)目屬性里的目標(biāo)平臺(tái),可由System.IntPtr.Size判斷,4為32位,8為64位,當(dāng)時(shí)因?yàn)檫@里的歧義踩過(guò)坑)

解壓

偽代碼:

	if(IntPtr.Size == 4)    //32位操作系統(tǒng)
	{
     	 SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + @"\7z.dll"); //路徑指向dll文件,此處dll放在與程序相同目錄,以下相同。
     	 
	}
	else    //64位操作系統(tǒng)
	{
    	SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + @"\7z64.dll");
	}
	using (var tmp = new SevenZipExtractor(“壓縮文件全名稱”))    //這里的全名稱包含路徑
    {
         tmp.ExtractArchive(“解壓到的路徑”);
    }

壓縮

偽代碼:

	if(IntPtr.Size == 4)    //32位操作系統(tǒng)
	{
     	 SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + @"\7z.dll");
	}
	else    //64位操作系統(tǒng)
	{
    	SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + @"\7z64.dll");
	}
	var compressor = new SevenZipCompressor();
	//壓縮文件夾:
	compressor.CompressDirectory(目錄名,壓縮文件名稱);//此處有多個(gè)重載,不一一列出。

	//壓縮文件:
	var zipTool = new SevenZipCompressor();
	zipTool.ArchiveFormat = OutArchiveFormat.Zip;   //壓縮文件類型
    string[] fileNames = {“文件全路徑”,“文件全路徑” }; //需要添加到壓縮文件的文件的全路徑數(shù)組。
    zipTool.CompressFiles(“壓縮文件名稱”, fileNames); //傳遞壓縮文件名稱,及文件全路徑數(shù)組。

以上就是C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄的詳細(xì)內(nèi)容,更多關(guān)于C# SevenZipSharp壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實(shí)現(xiàn)啟動(dòng),關(guān)閉與查找進(jìn)程的方法

    C#實(shí)現(xiàn)啟動(dòng),關(guān)閉與查找進(jìn)程的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)啟動(dòng),關(guān)閉與查找進(jìn)程的方法,通過(guò)簡(jiǎn)單實(shí)例形式分析了C#針對(duì)進(jìn)程的啟動(dòng),關(guān)閉與查找的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C#寫(xiě)差異文件備份工具的示例

    C#寫(xiě)差異文件備份工具的示例

    這篇文章主要介紹了C#寫(xiě)差異文件備份工具的示例,幫助大家利用c#備份,管理文件,感興趣的朋友可以了解下
    2020-10-10
  • C#程序優(yōu)化-有效減少CPU占用率

    C#程序優(yōu)化-有效減少CPU占用率

    本文給大家介紹的是C#程序優(yōu)化的小技巧,通過(guò)此方法可以有效的降低CPU的占用率,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
    2015-06-06
  • C#中通過(guò)API實(shí)現(xiàn)的打印類 實(shí)例代碼

    C#中通過(guò)API實(shí)現(xiàn)的打印類 實(shí)例代碼

    這篇文章介紹了,C#中通過(guò)API實(shí)現(xiàn)的打印類 實(shí)例代碼,有需要的朋友可以參考一下
    2013-08-08
  • C#中的TemplateMethod模式問(wèn)題分析

    C#中的TemplateMethod模式問(wèn)題分析

    這篇文章主要介紹了C#中的TemplateMethod模式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • C#簡(jiǎn)單實(shí)現(xiàn)SNMP的方法

    C#簡(jiǎn)單實(shí)現(xiàn)SNMP的方法

    這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)SNMP的方法,通過(guò)一個(gè)簡(jiǎn)單的自定義類分析了C#實(shí)現(xiàn)SNMP的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • c# 編寫(xiě)的簡(jiǎn)單飛行棋游戲

    c# 編寫(xiě)的簡(jiǎn)單飛行棋游戲

    這個(gè)簡(jiǎn)單的飛行棋游戲主要是講的方法怎么應(yīng)用,充分的去理解方法和方法的調(diào)用。整體收獲還是很大的。感興趣的朋友可以參考下
    2021-06-06
  • C#實(shí)現(xiàn)在Word文檔中添加或移除可編輯區(qū)域

    C#實(shí)現(xiàn)在Word文檔中添加或移除可編輯區(qū)域

    在日常辦公和自動(dòng)化流程中,Word文檔扮演著不可或缺的角色,本文將深入探討如何利用C#結(jié)合強(qiáng)大的第三方庫(kù)Spire.Doc for .NET在Word文檔中添加和移除可編輯區(qū)域,感興趣的小伙伴可以了解下
    2026-01-01
  • 聊聊Unity自定義組件之序列幀播放組件問(wèn)題

    聊聊Unity自定義組件之序列幀播放組件問(wèn)題

    由于最近的項(xiàng)目中需要用到大量的序列幀動(dòng)畫(huà)以及邏輯處理,本來(lái)想用Unity自帶的Animation組件來(lái)實(shí)現(xiàn)的,但由于甲方需求一再變更,需要處理的邏輯太多,為了方便修改和拓展,所以就根據(jù)自己項(xiàng)目的需求自定義了一個(gè)序列幀播放組件來(lái)輔助開(kāi)發(fā)
    2022-01-01
  • C#中LINQ的Select與SelectMany函數(shù)使用

    C#中LINQ的Select與SelectMany函數(shù)使用

    這篇文章主要介紹了C#中LINQ的Select與SelectMany函數(shù)使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

宣汉县| 封开县| 汝阳县| 全椒县| 阿尔山市| 盱眙县| 高安市| 乌拉特前旗| 霍林郭勒市| 安岳县| 三河市| 浪卡子县| 奇台县| 来凤县| 四子王旗| 仪陇县| 静乐县| 犍为县| 潼南县| 漾濞| 上饶市| 洞头县| 雷州市| 罗甸县| 贡觉县| 鄂托克旗| 五寨县| 昭平县| 平邑县| 黎川县| 卢氏县| 扎囊县| 泌阳县| 白山市| 元氏县| 安阳县| 洛扎县| 天长市| 商丘市| 轮台县| 绥中县|