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

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

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

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

urD28資訊網(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ò)誤處理。urD28資訊網(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)簽:
  • 熱門(mén)焦點(diǎn)
Top 主站蜘蛛池模板: 奈曼旗| 江门市| 永昌县| 昌乐县| 阿克苏市| 乌拉特前旗| 霍林郭勒市| 甘德县| 蓬莱市| 砚山县| 涡阳县| 普兰县| 垣曲县| 东阿县| 澄城县| 嘉荫县| 昆山市| 新乡县| 江门市| 林口县| 肥西县| 兰西县| 宁海县| 庄河市| 公主岭市| 调兵山市| 阿巴嘎旗| 博湖县| 万州区| 安远县| 衡水市| 岚皋县| 马关县| 昌黎县| 綦江县| 柏乡县| 辉县市| 北碚区| 旬邑县| 玛曲县| 兰西县|