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

C#壓縮解壓文件的常用方法

 更新時間:2025年09月24日 09:53:38   作者:Humbunklung  
在C#中處理文件和文件夾的壓縮與解壓,我們可使用微軟內(nèi)置的 System.IO.Compression 命名空間,也可選擇功能更豐富的第三方庫如 SharpZipLib,下面我將分別介紹幾種常見方法,需要的朋友可以參考下

引言

在C#中處理文件和文件夾的壓縮與解壓,我們可使用微軟內(nèi)置的 System.IO.Compression 命名空間,也可選擇功能更豐富的第三方庫如 SharpZipLib。下面我將分別介紹幾種常見方法,并提供處理多文件夾和文件混合壓縮的方案。

1. 使用 .NET 內(nèi)置的 ZipFile 類

.NET Framework 4.5 及以上版本和 .NET Core/.NET 5+ 提供了 ZipFile 類,適用于簡單的壓縮和解壓場景。

壓縮單個文件夾

using System.IO.Compression;

public static void CompressDirectory(string sourceDirectoryName, string destinationArchiveFileName)
{
    ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, CompressionLevel.Optimal, false);
}

值得注意的是,使用ZipFile創(chuàng)建壓縮包,默認不會包含根目錄。如果需要包含根目錄,可以先將文件夾復制到一個臨時目錄,然后壓縮該臨時目錄。

例如,我對一個名為“Build a Large Language Model”的目錄進行壓縮,它里面包含了“doc”和“src”兩個目錄,壓縮后的效果如下圖:

解壓整個壓縮包

public static void ExtractArchive(string sourceArchiveFileName, string destinationDirectoryName)
{
    ZipFile.ExtractToDirectory(sourceArchiveFileName, destinationDirectoryName);
}

壓縮單個文件

壓縮單個文件需要先創(chuàng)建臨時目錄,將文件復制進去后再壓縮。

public static void CompressFile(string sourceFileName, string destinationArchiveFileName)
{
    string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
    Directory.CreateDirectory(tempDir);
    try
    {
        string destFile = Path.Combine(tempDir, Path.GetFileName(sourceFileName));
        File.Copy(sourceFileName, destFile);
        ZipFile.CreateFromDirectory(tempDir, destinationArchiveFileName, CompressionLevel.Optimal, false);
    }
    finally
    {
        Directory.Delete(tempDir, true);
    }
}

2. 使用 ZipArchive 進行靈活操作

ZipArchive 類提供了更精細的控制,適合混合壓縮多個文件和文件夾。

壓縮多個文件和文件夾

此方法遞歸地添加文件和文件夾,保持目錄結(jié)構(gòu)。

using System.IO.Compression;

        /// <summary>
        /// 壓縮文件和文件夾到一個ZIP文件中
        /// </summary>
        /// <param name="zipPath">生成的壓縮包路徑</param>
        /// <param name="filesToZip">需要壓縮的文件</param>
        /// <param name="foldersToZip">需要壓縮的文件夾,默認沒有</param>
        public static void CreateZipFile(string zipPath, string[]? filesToZip, string[]? foldersToZip = null, 
            CompressionLevel compressionLevel = CompressionLevel.Optimal)
        {
            using (FileStream zipStream = new FileStream(zipPath, FileMode.Create))
            using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
            {
                // 添加單個文件
                if (filesToZip != null)
                {
                    foreach (string file in filesToZip)
                    {
                        if (File.Exists(file))
                        {
                            string entryName = Path.GetFileName(file);
                            archive.CreateEntryFromFile(file, entryName);
                        }
                    }
                }

                // 添加文件夾(遞歸)
                if (foldersToZip != null)
                {
                    foreach (string folder in foldersToZip)
                    {
                        if (Directory.Exists(folder))
                        {
                            AddFolderToZip(archive, folder, Path.GetFileName(folder), compressionLevel);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 添加文件夾到ZIP歸檔中(遞歸)
        /// </summary>
        /// <param name="archive">ZIP壓縮包</param>
        /// <param name="folderPath">文件夾路徑</param>
        /// <param name="relativePath">相對路徑</param>
        private static void AddFolderToZip(ZipArchive archive, string folderPath, string relativePath, 
            CompressionLevel compressionLevel)
        {
            string[] files = Directory.GetFiles(folderPath);
            foreach (string file in files)
            {
                string entryName = Path.Combine(relativePath, Path.GetFileName(file));
                archive.CreateEntryFromFile(file, entryName);
            }

            string[] subfolders = Directory.GetDirectories(folderPath);
            foreach (string subfolder in subfolders)
            {
                string newRelativePath = Path.Combine(relativePath, Path.GetFileName(subfolder));
                AddFolderToZip(archive, subfolder, newRelativePath, compressionLevel);
            }
        }

解壓(保留目錄結(jié)構(gòu))

public static void ExtractZipFile(string zipPath, string extractPath)
{
    using (ZipArchive archive = ZipFile.OpenRead(zipPath))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            string fullPath = Path.Combine(extractPath, entry.FullName);
            string directory = Path.GetDirectoryName(fullPath);
            
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
                
            if (!string.IsNullOrEmpty(entry.Name))
                entry.ExtractToFile(fullPath, overwrite: true);
        }
    }
}

3. 使用 SharpZipLib 庫

對于更高級的需求(如加密、壓縮級別控制、Unicode支持),可使用 SharpZipLib。

安裝 SharpZipLib

通過 NuGet 包管理器安裝:

Install-Package SharpZipLib

壓縮多個文件和文件夾

using ICSharpCode.SharpZipLib.Zip;

public static void CreateZipWithSharpZipLib(string zipPath, string[] files, string[] folders, string password = null)
{
    using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipPath)))
    {
        zipStream.SetLevel(9); // 壓縮級別 (0-9)
        if (!string.IsNullOrEmpty(password))
            zipStream.Password = password;

        byte[] buffer = new byte[4096];

        // 添加文件
        foreach (string file in files)
        {
            if (File.Exists(file))
            {
                ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                entry.DateTime = DateTime.Now;
                zipStream.PutNextEntry(entry);

                using (FileStream fs = File.OpenRead(file))
                {
                    int sourceBytes;
                    while ((sourceBytes = fs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        zipStream.Write(buffer, 0, sourceBytes);
                    }
                }
                zipStream.CloseEntry();
            }
        }

        // 添加文件夾
        foreach (string folder in folders)
        {
            if (Directory.Exists(folder))
            {
                AddFolderToSharpZip(zipStream, folder, "", buffer);
            }
        }
        zipStream.Finish();
    }
}

private static void AddFolderToSharpZip(ZipOutputStream zipStream, string folderPath, string relativePath, byte[] buffer)
{
    string[] files = Directory.GetFiles(folderPath);
    foreach (string file in files)
    {
        string entryName = Path.Combine(relativePath, Path.GetFileName(file));
        ZipEntry entry = new ZipEntry(entryName);
        entry.DateTime = DateTime.Now;
        zipStream.PutNextEntry(entry);

        using (FileStream fs = File.OpenRead(file))
        {
            int sourceBytes;
            while ((sourceBytes = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                zipStream.Write(buffer, 0, sourceBytes);
            }
        }
        zipStream.CloseEntry();
    }

    string[] subfolders = Directory.GetDirectories(folderPath);
    foreach (string subfolder in subfolders)
    {
        string newRelativePath = Path.Combine(relativePath, Path.GetFileName(subfolder));
        AddFolderToSharpZip(zipStream, subfolder, newRelativePath, buffer);
    }
}

使用 SharpZipLib 解壓

public static void ExtractWithSharpZipLib(string zipPath, string extractPath, string password = null)
{
    using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipPath)))
    {
        zipStream.Password = password;
        ZipEntry entry;
        while ((entry = zipStream.GetNextEntry()) != null)
        {
            string fullPath = Path.Combine(extractPath, entry.Name);
            string directory = Path.GetDirectoryName(fullPath);
            
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
                
            if (!string.IsNullOrEmpty(entry.Name))
            {
                using (FileStream streamWriter = File.Create(fullPath))
                {
                    byte[] data = new byte[4096];
                    int size;
                    while ((size = zipStream.Read(data, 0, data.Length)) > 0)
                    {
                        streamWriter.Write(data, 0, size);
                    }
                }
            }
        }
    }
}

4. 錯誤處理與最佳實踐

進行壓縮和解壓操作時,務(wù)必添加錯誤處理。

try
{
    // 你的壓縮或解壓代碼
}
catch (FileNotFoundException ex)
{
    Console.WriteLine($"文件未找到: {ex.Message}");
}
catch (DirectoryNotFoundException ex)
{
    Console.WriteLine($"目錄未找到: {ex.Message}");
}
catch (IOException ex)
{
    Console.WriteLine($"IO錯誤: {ex.Message}");
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"權(quán)限錯誤: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"未知錯誤: {ex.Message}");
}

5. 方法對比與選擇

我們可以根據(jù)需求選擇合適的方法:

方法特性ZipFile (內(nèi)置)ZipArchive (內(nèi)置)SharpZipLib (第三方)
易用性
功能豐富度基礎(chǔ)中等高(加密、Unicode支持等)
性能良好良好良好
無需額外依賴
跨平臺兼容性
推薦場景簡單壓縮/解壓需精細控制復雜需求(如加密)

總結(jié)

在C#中實現(xiàn)zip壓縮和解壓,我們可根據(jù)需求選擇:

  • 簡單場景:使用內(nèi)置的 ZipFile 類(ZipFile.CreateFromDirectoryZipFile.ExtractToDirectory)最方便。
  • 需精細控制或多文件/文件夾混合:使用 ZipArchive 類逐項添加內(nèi)容更靈活。
  • 有高級需求(如加密、更高壓縮比)SharpZipLib 等第三方庫功能更強大。

處理多文件和文件夾時,遞歸添加是保持目錄結(jié)構(gòu)的關(guān)鍵。無論用哪種方法,都請注意添加適當?shù)腻e誤處理(如 try-catch 塊)以確保程序健壯性。

到此這篇關(guān)于C#壓縮解壓文件的常用方法的文章就介紹到這了,更多相關(guān)C#壓縮解壓文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Unity中Mask和RectMask2D組件的對比與測試

    詳解Unity中Mask和RectMask2D組件的對比與測試

    本篇文章給大家介紹Unity中Mask和RectMask2D組件的對比與測試,包括組件用法及RectMask2D的基本用法,通過Mask的原理分析實例代碼相結(jié)合給大家講解的非常詳細,需要的朋友參考下吧
    2021-06-06
  • C#實現(xiàn)讀寫CSV文件的方法詳解

    C#實現(xiàn)讀寫CSV文件的方法詳解

    項目中經(jīng)常遇到CSV文件的讀寫需求,其中的難點主要是CSV文件的解析。本文會介紹CsvHelper、TextFieldParser、正則表達式三種解析CSV文件的方法,需要的可以參考一下
    2022-06-06
  • c# Bitmap轉(zhuǎn)bitmapImage高效方法

    c# Bitmap轉(zhuǎn)bitmapImage高效方法

    本文主要介紹了c# Bitmap轉(zhuǎn)bitmapImage高效方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C# 繪制實時折線圖,波形圖

    C# 繪制實時折線圖,波形圖

    這篇文章主要介紹了C# 繪制實時折線圖,波形圖的方法,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • C# Websocket連接實現(xiàn)wss協(xié)議

    C# Websocket連接實現(xiàn)wss協(xié)議

    本文主要介紹了C# Websocket連接實現(xiàn)wss協(xié)議,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • js substr,substring與java substring和C# substring的區(qū)別解析

    js substr,substring與java substring和C# substring的區(qū)別解析

    本篇文章主要是對js中substr,substring與java中substring和C#中substring的區(qū)別進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#使用控制臺列出當前所有可用的打印機列表

    C#使用控制臺列出當前所有可用的打印機列表

    這篇文章主要介紹了C#使用控制臺列出當前所有可用的打印機列表,涉及C#操作計算機硬件的相關(guān)使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C#中的Linq?to?JSON操作詳解

    C#中的Linq?to?JSON操作詳解

    本文詳細講解了C#中的Linq?to?JSON操作,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • .NET?Core使用C#掃描并讀取圖片中的文字

    .NET?Core使用C#掃描并讀取圖片中的文字

    本文詳細講解了.NET?Core使用C#掃描并讀取圖片中的文字,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • UnityWebRequest前后端交互實現(xiàn)過程解析

    UnityWebRequest前后端交互實現(xiàn)過程解析

    這篇文章主要介紹了UnityWebRequest前后端交互實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論