自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

搞懂C#文件壓縮:SharpZipLib vs. DotNetZip,實用代碼一網(wǎng)打盡!

開發(fā) 后端
在C#中,有兩個熱門的文件壓縮解析類庫分別是SharpZipLib和DotNetZip。以下是它們的簡要介紹以及使用實例代碼。

1. SharpZipLib

功能:

  • 支持ZIP和GZip格式的壓縮和解壓縮。
  • 提供了對Tar和BZip2格式的支持。
  • 輕量級,易于使用。

優(yōu)點:

  • 開源,廣泛使用。
  • 靈活性較高,適用于多種壓縮需求。

使用實例:

using System;
using ICSharpCode.SharpZipLib.Zip;

class Program
{
    static void Main()
    {
        string sourceFolder = @"C:\Path\To\Your\Folder";
        string zipFile = @"C:\Path\To\Your\Archive.zip";

        ZipDirectory(sourceFolder, zipFile);
        Console.WriteLine("Compression completed.");

        string extractFolder = @"C:\Path\To\Your\Extracted";
        UnzipFile(zipFile, extractFolder);
        Console.WriteLine("Extraction completed.");
    }

    static void ZipDirectory(string sourceFolder, string zipFile)
    {
        using (ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipFile)))
        {
            zipStream.SetLevel(9); // 0 - store only to 9 - means best compression

            ZipFolder(sourceFolder, sourceFolder, zipStream);

            zipStream.Finish();
            zipStream.Close();
        }
    }

    static void ZipFolder(string rootFolder, string currentFolder, ZipOutputStream zipStream)
    {
        string[] files = System.IO.Directory.GetFiles(currentFolder);

        foreach (string file in files)
        {
            ZipFile(zipStream, currentFolder, file);
        }

        string[] subFolders = System.IO.Directory.GetDirectories(currentFolder);
        foreach (string folder in subFolders)
        {
            ZipFolder(rootFolder, folder, zipStream);
        }
    }

    static void ZipFile(ZipOutputStream zipStream, string rootFolder, string filePath)
    {
        byte[] buffer = new byte[4096];

        string relativePath = filePath.Substring(rootFolder.Length + 1);

        ZipEntry entry = new ZipEntry(relativePath);
        zipStream.PutNextEntry(entry);

        using (System.IO.FileStream fs = System.IO.File.OpenRead(filePath))
        {
            int sourceBytes;
            do
            {
                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                zipStream.Write(buffer, 0, sourceBytes);
            } while (sourceBytes > 0);
        }
    }

    static void UnzipFile(string zipFile, string extractFolder)
    {
        using (ZipInputStream zipStream = new ZipInputStream(System.IO.File.OpenRead(zipFile)))
        {
            ZipEntry entry;
            while ((entry = zipStream.GetNextEntry()) != null)
            {
                string entryName = entry.Name;

                string fullZipToPath = System.IO.Path.Combine(extractFolder, entryName);

                string directoryName = System.IO.Path.GetDirectoryName(fullZipToPath);
                if (directoryName.Length > 0)
                {
                    System.IO.Directory.CreateDirectory(directoryName);
                }

                if (entry.IsFile)
                {
                    byte[] buffer = new byte[4096];
                    using (System.IO.FileStream streamWriter = System.IO.File.Create(fullZipToPath))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = zipStream.Read(buffer, 0, buffer.Length);
                            streamWriter.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
            }
        }
    }
}

2. DotNetZip

功能:

  • 支持ZIP格式的壓縮和解壓縮。
  • 提供了對多卷和自解壓縮ZIP文件的支持。
  • 具有更簡單的API,易于使用。

優(yōu)點:

  • 使用方便,簡潔明了。
  • 集成度高,適合快速實現(xiàn)文件壓縮解壓縮功能。

使用實例:

using System;
using Ionic.Zip;

class Program
{
    static void Main()
    {
        string sourceFolder = @"C:\Path\To\Your\Folder";
        string zipFile = @"C:\Path\To\Your\Archive.zip";

        ZipDirectory(sourceFolder, zipFile);
        Console.WriteLine("Compression completed.");

        string extractFolder = @"C:\Path\To\Your\Extracted";
        UnzipFile(zipFile, extractFolder);
        Console.WriteLine("Extraction completed.");
    }

    static void ZipDirectory(string sourceFolder, string zipFile)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory(sourceFolder);
            zip.Save(zipFile);
        }
    }

    static void UnzipFile(string zipFile, string extractFolder)
    {
        using (ZipFile zip = ZipFile.Read(zipFile))
        {
            zip.ExtractAll(extractFolder, ExtractExistingFileAction.OverwriteSilently);
        }
    }
}

以上兩個例子都提供了基本的目錄壓縮和解壓縮功能,你可以根據(jù)具體需求進行進一步定制。確保在實際項目中進行充分的測試和適當?shù)腻e誤處理。

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-04-26 00:25:52

Rust語法生命周期

2021-08-05 06:54:05

流程控制default

2024-02-27 10:11:36

前端CSS@規(guī)則

2021-10-11 07:55:42

瀏覽器語法Webpack

2013-08-02 10:52:10

Android UI控件

2024-04-07 08:41:34

2024-08-26 10:01:50

2024-06-12 00:00:05

2010-08-25 01:59:00

2011-12-02 09:22:23

網(wǎng)絡(luò)管理NetQos

2013-10-16 14:18:02

工具圖像處理

2023-04-06 09:08:41

BPM流程引擎

2019-07-24 15:30:00

SQL注入數(shù)據(jù)庫

2020-02-21 08:45:45

PythonWeb開發(fā)框架

2021-05-20 11:17:49

加密貨幣區(qū)塊鏈印度

2021-10-29 09:32:33

springboot 靜態(tài)變量項目

2023-09-06 18:37:45

CSS選擇器符號

2020-10-19 06:43:53

Redis腳本原子

2023-04-03 08:30:54

項目源碼操作流程

2009-04-02 10:17:00

交換機產(chǎn)品選購
點贊
收藏

51CTO技術(shù)棧公眾號