日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

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

來(lái)源: 責(zé)編: 時(shí)間:2024-03-20 17:27:43 224觀看
導(dǎo)讀1. SharpZipLib功能:支持ZIP和GZip格式的壓縮和解壓縮。提供了對(duì)Tar和BZip2格式的支持。輕量級(jí),易于使用。優(yōu)點(diǎn):開(kāi)源,廣泛使用。靈活性較高,適用于多種壓縮需求。使用實(shí)例:using System;using ICSharpCode.SharpZipLib.Zip;

pbm28資訊網(wǎng)——每日最新資訊28at.com

1. SharpZipLib

功能:

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

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

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

使用實(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格式的壓縮和解壓縮。
  • 提供了對(duì)多卷和自解壓縮ZIP文件的支持。
  • 具有更簡(jiǎn)單的API,易于使用。

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

  • 使用方便,簡(jiǎ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)行充分的測(cè)試和適當(dāng)?shù)腻e(cuò)誤處理。pbm28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-78147-0.html搞懂C#文件壓縮:SharpZipLib vs. DotNetZip,實(shí)用代碼一網(wǎng)打盡!

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: Go 團(tuán)隊(duì)近兩年在做什么,AI 方面如何發(fā)力?

下一篇: 行業(yè)機(jī)構(gòu) SEMI:全球 12 英寸晶圓廠設(shè)備投資 2025 年將破千億美元大關(guān)

標(biāo)簽:
  • 熱門焦點(diǎn)
  • 如何正確使用:Has和:Nth-Last-Child

    我們可以用CSS檢查,以了解一組元素的數(shù)量是否小于或等于一個(gè)數(shù)字。例如,一個(gè)擁有三個(gè)或更多子項(xiàng)的grid。你可能會(huì)想,為什么需要這樣做呢?在某些情況下,一個(gè)組件或一個(gè)布局可能會(huì)
  • 之家push系統(tǒng)迭代之路

    前言在這個(gè)信息爆炸的互聯(lián)網(wǎng)時(shí)代,能夠及時(shí)準(zhǔn)確獲取信息是當(dāng)今社會(huì)要解決的關(guān)鍵問(wèn)題之一。隨著之家用戶體量和內(nèi)容規(guī)模的不斷增大,傳統(tǒng)的靠"主動(dòng)拉"獲取信息的方式已不能滿足用
  • 一篇文章帶你了解 CSS 屬性選擇器

    屬性選擇器對(duì)帶有指定屬性的 HTML 元素設(shè)置樣式。可以為擁有指定屬性的 HTML 元素設(shè)置樣式,而不僅限于 class 和 id 屬性。一、了解屬性選擇器CSS屬性選擇器提供了一種簡(jiǎn)單而
  • 使用LLM插件從命令行訪問(wèn)Llama 2

    最近的一個(gè)大新聞是Meta AI推出了新的開(kāi)源授權(quán)的大型語(yǔ)言模型Llama 2。這是一項(xiàng)非常重要的進(jìn)展:Llama 2可免費(fèi)用于研究和商業(yè)用途。(幾小時(shí)前,swyy發(fā)現(xiàn)它已從LLaMA 2更名為L(zhǎng)la
  • Python異步IO編程的進(jìn)程/線程通信實(shí)現(xiàn)

    這篇文章再講3種方式,同時(shí)講4中進(jìn)程間通信的方式一、 Python 中線程間通信的實(shí)現(xiàn)方式共享變量共享變量是多個(gè)線程可以共同訪問(wèn)的變量。在Python中,可以使用threading模塊中的L
  • 雅柏威士忌多款單品價(jià)格大跌,泥煤頂流也不香了?

    來(lái)源 | 烈酒商業(yè)觀察編 | 肖海林今年以來(lái),威士忌市場(chǎng)開(kāi)始出現(xiàn)了降溫跡象,越來(lái)越多不斷暴漲的網(wǎng)紅威士忌也開(kāi)始悄然回歸市場(chǎng)理性。近日,LVMH集團(tuán)旗下蘇格蘭威士忌品牌雅柏(Ardbeg
  • 電視息屏休眠仍有網(wǎng)絡(luò)上傳 愛(ài)奇藝被質(zhì)疑“薅消費(fèi)者羊毛”

    記者丨寧曉敏 見(jiàn)習(xí)生丨汗青出品丨鰲頭財(cái)經(jīng)(theSankei) 前不久,愛(ài)奇藝發(fā)布了一份亮眼的一季報(bào),不僅營(yíng)收和會(huì)員營(yíng)收創(chuàng)造歷史最佳表現(xiàn),其運(yùn)營(yíng)利潤(rùn)也連續(xù)6個(gè)月實(shí)現(xiàn)增長(zhǎng)。自去年年初
  • 2299元起!iQOO Pad明晚首銷:性能最強(qiáng)天璣平板

    5月23日,iQOO如期舉行了新品發(fā)布會(huì),除了首發(fā)安卓最強(qiáng)旗艦處理器的iQOO Neo8系列新機(jī)外,還在發(fā)布會(huì)上推出了旗下首款平板電腦——iQOO Pad,其最大的賣點(diǎn)
  • 電博會(huì)上海爾智家模擬500平大平層,還原生活空間沉浸式體驗(yàn)

    電博會(huì)為了更好地讓參展觀眾真正感受到智能家居的絕妙之處,海爾智家的程傳嶺先生同樣介紹了展會(huì)上海爾智家的模擬500平大平層,還原生活空間沉浸式體驗(yàn)。程傳
Top 主站蜘蛛池模板: 乐东| 夏河县| 廉江市| 咸阳市| 满洲里市| 拜城县| 缙云县| 图木舒克市| 兴国县| 莱西市| 合山市| 衡阳市| 林周县| 清丰县| 莱阳市| 罗源县| 塔城市| 达孜县| 漳浦县| 文昌市| 商都县| 子长县| 邹平县| 英超| 墨脱县| 普陀区| 广元市| 盘山县| 嵊泗县| 靖州| 土默特左旗| 祥云县| 靖边县| 邢台县| 彰化市| 二手房| 广州市| 清丰县| 新平| 韩城市| 连江县|