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

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. 錯誤處理與最佳實踐

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

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 (第三方)
易用性
功能豐富度基礎中等高(加密、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)文章

最新評論

偏关县| 罗定市| 喀喇| 海伦市| 许昌县| 洱源县| 台南市| 开封市| 民权县| 锡林郭勒盟| 新巴尔虎右旗| 海原县| 宁阳县| 武宁县| 垣曲县| 巴东县| 进贤县| 蓬莱市| 班戈县| 临夏市| 永平县| 潞西市| 沭阳县| 黔南| 江油市| 北辰区| 宝鸡市| 凤翔县| 德化县| 葫芦岛市| 孝昌县| 孟连| 民丰县| 三江| 南皮县| 南木林县| 绥德县| 府谷县| 三江| 塘沽区| 扶风县|