借助Aspose.ZIP-SDK,在-C-中压缩和提取-LZIP-文件
目录
借助Aspose.ZIP SDK,在 C# 中压缩和提取 LZIP 文件
如果您希望在 .NET 应用程序中集成文件压缩和解压功能,那么这篇博文可能会对您有所帮助。LZIP是一种无损数据压缩格式,广泛用于压缩和共享源代码、软件包和备份。为了实现这一自动化功能,我们将使用** **,因为这个 SDK 使得在 C# 中处理 LZIP 文件变得轻而易举。完成本指南后,您可以开发一个模块,以编程方式压缩和解压 LZIP 文件。
Aspose.ZIP for .NET - 安装
安装此 SDK 非常简单。只需** **** ** 文件或运行以下命令:
Install-Package Aspose.Zip
或者,通过 NuGet 包管理器安装。
如何在 C# 中以编程方式压缩 LZIP 文件
提供了一整套使用 C# 处理 LZIP 文件的功能。不过,我们可以使用此 SDK 压缩和解压 LZIP 文件。首先,让我们看看如何压缩 LZIP 文件。
您可以按照以下步骤操作:
- 定义工作目录的路径并加载许可证。
- 创建LzipArchive类的实例。
- 调用SetSource方法来设置档案中要压缩的内容。
- 调用Save方法将 LZIP 存档创建到提供的目标文件。
以下代码示例展示了如何在 C# 中压缩 LZIP 文件:
using Aspose.Zip;
using Aspose.Zip.Lzip;
namespace AsposeZip
{
class Program
{
static void Main(string[] args)
{
string dataDir = "files";
string licensePath = "License.lic";
// Apply license
License lic = new License();
lic.SetLicense(licensePath);
// Create an instance of the LzipArchive class.
using (LzipArchive archive = new LzipArchive())
{
// Invoke the SetSource method to set the content to be compressed within the archive.
archive.SetSource(dataDir + "index.html");
// Call the Save method to create LZIP archive to the destination file provided.
archive.Save(dataDir + "archive.lz");
}
Console.WriteLine("Successfully Compressed a lzip file");
}
}
}
将生成以下输出:
使用 Aspose.ZIP for .NET 提取 LZIP 文件
现在我们将介绍如何使用相同的 SDK 提取 LZIP 文件。为此,我们将使用Extract方法解压 LZIP 文件,如以下代码示例所示:
using Aspose.Zip;
using Aspose.Zip.Lzip;
namespace AsposeZip
{
class Program
{
static void Main(string[] args)
{
string dataDir = "files";
string licensePath = "License.lic";
// Apply license
License lic = new License();
lic.SetLicense(licensePath);
// Instantiate an object of the LzipArchive class.
using (var archive = new LzipArchive(dataDir + "archive.lz"))
{
// Create "web.html" in dataDir and return a FileStream
using (var extracted = File.Create(dataDir + "web.html"))
{
// The Extract method will extract lzip archive to a stream.
archive.Extract(extracted);
}
}
Console.WriteLine("Successfully Opened lzip Archive");
}
}
}
输出:
结论
我们已经了解了如何借助** **,通过几行源代码高效地处理大文件。归档压缩可以减小大文件的大小,以便您轻松分发它们。我们已经实现了如何在 C# 中以编程方式压缩和解压缩 LZIP 文件。