搞懂C#文件壓縮:SharpZipLib vs. DotNetZip,功能對(duì)比與實(shí)用代碼一網(wǎng)打盡!
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
在C#中,有兩個(gè)熱門(mén)的文件壓縮解析類庫(kù)分別是SharpZipLib和DotNetZip。以下是它們的簡(jiǎn)要介紹以及使用實(shí)例代碼。 1. SharpZipLib功能:
優(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功能:
優(yōu)點(diǎ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ò)誤處理。 該文章在 2023/12/3 22:51:17 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |