在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在當今的數位環境中,數據管理至關重要,擁有高效的壓縮和解壓縮工具是關鍵。 在 .NET 生態系統中脫穎而出的一個工具是SharpZipLib. 在本文中,我們將深入探討 SharpZipLib,探索其功能、應用以及如何將其整合到您的 .NET 專案中。
SharpZipLib是一個功能豐富的開源壓縮庫,專為.NET而設,完全使用C#編寫。 它為各種壓縮格式提供全面支持,包括ZIP、GZip和Tar。 由專注的社群開發,SharpZipLib 提供廣泛的功能,以高效地壓縮和解壓縮檔案。
支持多種壓縮格式: SharpZipLib 支援流行的壓縮格式,如 ZIP、GZip 和 Tar,以滿足不同的使用情境和需求。
基於流的操作: 該庫在流上運行,使開發人員能夠處理來自各種來源的數據,包括文件、記憶體流或網路流。 這種靈活性促進了應用程序不同部分的無縫整合。
壓縮等級: 根據特定需求,開發人員可以調整壓縮等級以在壓縮比和處理速度之間取得平衡。
密碼保護: SharpZipLib 允許创建受密碼保護的 ZIP 檔案,透過使用指定的密碼加密內容來確保資料安全。
檔案壓縮和歸檔: SharpZipLib 非常適合需要壓縮和歸檔檔案的應用程式,例如備份工具、檔案管理工具或數據導出功能。
網路服務和 API: 處理文件傳輸或數據交換的網路服務通常會使用壓縮來減少頻寬使用。 SharpZipLib 可以無縫整合到這些服務中,以有效壓縮傳出的數據或解壓縮接收到的載荷。
桌面應用程式: 處理大型數據集或資源文件的桌面應用程式可以利用 SharpZipLib 來壓縮文件以進行儲存或分發。 這對於軟體安裝程式或數據同步工具尤其有用。
開源: 作為一個開源庫,SharpZipLib 鼓勵合作和社區貢獻,確保持續改進和適應不斷演變的需求。
跨平台相容性: SharpZipLib 以 C# 編寫並針對 .NET 框架,兼容多種平台,包括 Windows、Linux 和 macOS,提升了其多功能性。
輕量高效: SharpZipLib 被設計為輕量且高效,能夠在提供高效能壓縮和解壓功能的同時,最大限度地降低資源消耗。
打開 Visual Studio,然後點擊「建立新專案」選項。
根據您的需求選擇合適的專案模板(例如,控制台應用程式,Windows Forms 應用程式).
指定專案名稱和位置,然後點擊「下一步」。
將 SharpZipLib 整合到您的 .NET 專案中:
在你的Visual Studio IDEC# ConsoleApp,右鍵點擊解決方案資源管理器中的專案,然後選擇「管理 NuGet 封裝...」。
在 NuGet 套件管理器窗口中,搜索 "SharpZipLib"。
從搜索結果中選擇 "SharpZipLib" 並點擊 "Install" 按鈕。
以下是一個簡化的示例,說明如何使用 SharpZipLib 壓縮和解壓文件:
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
namespace SharpZipLibExample
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\SourceDirectory";
string zipFilePath = @"C:\OutputDirectory\compressed.zip";
// Compress files
CompressDirectory(sourceDirectory, zipFilePath);
Console.WriteLine("Files compressed successfully.");
string extractPath = @"C:\OutputDirectory\extracted";
// Decompress files
Decompress(zipFilePath, extractPath);
Console.WriteLine("Files decompressed successfully.");
}
static void CompressDirectory(string sourceDirectory, string zipFilePath)
{
using (var zipOutputStream = new ZipOutputStream(File.Create(zipFilePath)))
{
zipOutputStream.SetLevel(5); // Compression level (0-9)
// Recursively add files in the source directory to the ZIP file
AddDirectoryFilesToZip(sourceDirectory, zipOutputStream);
zipOutputStream.Finish();
zipOutputStream.Close();
}
}
static void AddDirectoryFilesToZip(string sourceDirectory, ZipOutputStream zipOutputStream)
{
string[] files = Directory.GetFiles(sourceDirectory);
foreach (string file in files)
{
var entry = new ZipEntry(Path.GetFileName(file));
zipOutputStream.PutNextEntry(entry);
using (var fileStream = File.OpenRead(file))
{
byte[] buffer = new byte[4096];
int sourceBytes;
while ((sourceBytes = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
zipOutputStream.Write(buffer, 0, sourceBytes);
}
}
}
string[] subdirectories = Directory.GetDirectories(sourceDirectory);
foreach (string subdirectory in subdirectories)
{
AddDirectoryFilesToZip(subdirectory, zipOutputStream);
}
}
static void Decompress(string zipFilePath, string extractPath)
{
using (var zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry entry;
while ((entry = zipInputStream.GetNextEntry()) != null)
{
string entryPath = Path.Combine(extractPath, entry.Name);
if (entry.IsFile)
{
string directoryName = Path.GetDirectoryName(entryPath);
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
using (var fileStream = File.Create(entryPath))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = zipInputStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
else if (entry.IsDirectory)
{
Directory.CreateDirectory(entryPath);
}
}
}
}
}
}
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
namespace SharpZipLibExample
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\SourceDirectory";
string zipFilePath = @"C:\OutputDirectory\compressed.zip";
// Compress files
CompressDirectory(sourceDirectory, zipFilePath);
Console.WriteLine("Files compressed successfully.");
string extractPath = @"C:\OutputDirectory\extracted";
// Decompress files
Decompress(zipFilePath, extractPath);
Console.WriteLine("Files decompressed successfully.");
}
static void CompressDirectory(string sourceDirectory, string zipFilePath)
{
using (var zipOutputStream = new ZipOutputStream(File.Create(zipFilePath)))
{
zipOutputStream.SetLevel(5); // Compression level (0-9)
// Recursively add files in the source directory to the ZIP file
AddDirectoryFilesToZip(sourceDirectory, zipOutputStream);
zipOutputStream.Finish();
zipOutputStream.Close();
}
}
static void AddDirectoryFilesToZip(string sourceDirectory, ZipOutputStream zipOutputStream)
{
string[] files = Directory.GetFiles(sourceDirectory);
foreach (string file in files)
{
var entry = new ZipEntry(Path.GetFileName(file));
zipOutputStream.PutNextEntry(entry);
using (var fileStream = File.OpenRead(file))
{
byte[] buffer = new byte[4096];
int sourceBytes;
while ((sourceBytes = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
zipOutputStream.Write(buffer, 0, sourceBytes);
}
}
}
string[] subdirectories = Directory.GetDirectories(sourceDirectory);
foreach (string subdirectory in subdirectories)
{
AddDirectoryFilesToZip(subdirectory, zipOutputStream);
}
}
static void Decompress(string zipFilePath, string extractPath)
{
using (var zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry entry;
while ((entry = zipInputStream.GetNextEntry()) != null)
{
string entryPath = Path.Combine(extractPath, entry.Name);
if (entry.IsFile)
{
string directoryName = Path.GetDirectoryName(entryPath);
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
using (var fileStream = File.Create(entryPath))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = zipInputStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
else if (entry.IsDirectory)
{
Directory.CreateDirectory(entryPath);
}
}
}
}
}
}
SharpZipLib 長期以來一直是 .NET 語言開發社群中的主力,提供處理壓縮檔案(如 ZIP、GZip、Tar 和 BZip2)所需的基本功能。然而,隨著技術的不斷演進以及開發者尋求更先進的解決方案,SharpZipLib 的某些限制變得顯而易見。
複雜性:SharpZipLib 的 API 可能較為繁瑣冗長,要求開發人員編寫冗長的程式碼來執行如建立或提取 ZIP 檔案等簡單任務。
缺乏現代功能:SharpZipLib 缺乏對現代 .NET 功能和平台的支持,使其不太適合當代開發環境。
有限的文件:雖然SharpZipLib存在已久,但其文件通常稀少且過時,這使得開發者在入門或解決問題時面臨挑戰。
IronZIP 文件檔案由...開發Iron Software 概述, 是一種現代且高效的解決方案,用於在 .NET 應用程式中管理 ZIP 壓縮檔案。 憑藉其直觀的 API,開發人員可以輕鬆創建、閱讀和操作 ZIP 文件。 IronZIP 提供進階功能,如可自訂的壓縮等級和密碼保護,確保靈活性和資料安全。 兼容最新的 .NET 版本,IronZIP 經過性能優化,可輕鬆高效地簡化檔案管理任務。
IronZIP 功能成為解決SharpZipLib缺陷的強大且現代的方案。 以下是 IronZIP 如何填補空白:
高級 API:IronZIP 提供一個直觀且對開發者友好的 API,簡化檔案管理任務。 使用 IronZIP,開發人員只需撰寫少量代碼即可完成複雜操作,從而減少開發時間和精力。
完整的 .NET 支持:IronZIP 完全支持最新的 .NET 版本,包括 .NET Core、.NET Standard 和 .NET Framework,確保與現代開發環境和平台的兼容性。
完整的文件:IronZIP 附帶完整的文件和範例,使開發人員能夠快速掌握其功能和能力。 廣泛的文件有助於簡化學習曲線,促進在專案中的快速整合。
壓縮等級控制:IronZIP 為開發人員提供對壓縮等級的控制,允許他們根據需求調整壓縮等級。 此功能使開發人員能夠在減少文件大小與壓縮速度之間取得平衡。
密碼保護:IronZIP 支援對 ZIP 檔案進行密碼保護,增強敏感資料的安全性。 開發人員可以輕鬆使用傳統、AES128 和 AES256 密碼對 ZIP 壓縮檔進行加密,確保只有授權用戶能訪問壓縮檔的內容。
效能優化:IronZIP 進行了效能優化,提供比 SharpZipLib 更快的壓縮和解壓速度。 此優化確保開發人員能夠高效處理大量數據,而不會影響效能。
探索IronZIP 文件檔案如需有關開始使用IronZIP的更多資訊。IronZIP 代碼範例幫助您輕鬆開始。
以下是將XDocument與IronPDF整合的步驟:
Install-Package IronZip
從 NuGet 瀏覽標籤中選擇 IronZIP,然後點擊安裝:
以下程式碼展示了IronZIP如何高效地使用 IronZIP 創建 ZIP 文件輕鬆且只需幾行代碼。 在這裡,您可以添加多個文件到受密碼保護的 ZIP 檔案透過提供指定資料夾中的檔案名稱。 在建立 IronZipArchive
物件時,您還可以指定壓縮級別以減少輸出檔案的空間大小。
using IronZip;
using IronZip.Enum;
class Program
{
static void Main()
{
// Create an empty ZIP with the highest compression
using (var archive = new IronZipArchive(9))
{
// Password protect the ZIP (Support AES128 & AES256)
archive.SetPassword("P@ssw0rd", EncryptionMethods.Traditional);
archive.AddArchiveEntry("./assets/file1.txt");
archive.AddArchiveEntry("./assets/file2.txt");
// Export the ZIP
archive.SaveAs("output.zip");
}
}
}
using IronZip;
using IronZip.Enum;
class Program
{
static void Main()
{
// Create an empty ZIP with the highest compression
using (var archive = new IronZipArchive(9))
{
// Password protect the ZIP (Support AES128 & AES256)
archive.SetPassword("P@ssw0rd", EncryptionMethods.Traditional);
archive.AddArchiveEntry("./assets/file1.txt");
archive.AddArchiveEntry("./assets/file2.txt");
// Export the ZIP
archive.SaveAs("output.zip");
}
}
}
SharpZipLib 概述作為一個強大的 .NET 壓縮庫,提供豐富的功能和能力以高效處理壓縮文件。 無論是壓縮數據以進行儲存、存檔文件,還是在網路服務中優化帶寬使用,SharpZipLib 提供了必要的工具來簡化壓縮和解壓縮的操作。 由於其開源性、跨平台相容性和強大的功能,SharpZipLib 仍然是開發人員在 .NET 應用程式中尋求可靠壓縮解決方案的首選。
儘管SharpZipLib一直是處理.NET應用程式中壓縮檔案的可靠選擇,但在當今的開發環境中,其限制變得越來越明顯。 探索 IronZIP API填補了SharpZipLib留下的空缺,提供了一種現代且功能豐富的替代方案,優先考慮易用性、性能和兼容性。 使用 IronZIP,開發者可以在檔案管理中開啟新的可能性,並通過先進的功能和直觀的 API 簡化其開發工作流程。
IronZIP 提供一個免費試用授權概覽. 從下載該庫IronZIP 下載試試看。