亚洲乱色熟女一区二区三区丝袜,天堂√中文最新版在线,亚洲精品乱码久久久久久蜜桃图片,香蕉久久久久久av成人,欧美丰满熟妇bbb久久久

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

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

admin
2023年12月3日 22:51 本文熱度 2200


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

1. SharpZipLib

功能:

  • 支持ZIP和GZip格式的壓縮和解壓縮。

  • 提供了對Tar和BZip2格式的支持。

  • 輕量級,易于使用。

優(yōu)點(diǎn):

  • 開源,廣泛使用。

  • 靈活性較高,適用于多種壓縮需求。

使用實(shí)例:

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)點(diǎn):

  • 使用方便,簡潔明了。

  • 集成度高,適合快速實(shí)現(xiàn)文件壓縮解壓縮功能。

使用實(shí)例:

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);

        }

    }

}

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


該文章在 2023/12/3 22:51:17 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved