借助Aspose.ZIP SDK,在 C# 中压缩和提取 LZIP 文件
<p><img src="https://image.evget.com/attachment/keditor/image/20250903/105518_6.png"></p><p>如果您希望在 .NET 应用程序中集成文件压缩和解压功能,那么这篇博文可能会对您有所帮助。LZIP是一种无损数据压缩格式,广泛用于压缩和共享源代码、软件包和备份。为了实现这一自动化功能,我们将使用<strong><u>Aspose.ZIP for .NET</u></strong>,因为这个 SDK 使得在 C# 中处理 LZIP 文件变得轻而易举。完成本指南后,您可以开发一个模块,以编程方式压缩和解压 LZIP 文件。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.ZIP官方试用版免费下载,请联系Aspose官方授权代理商<span style="text-decoration: underline">慧都科技</span></strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="asposezip-for-net---installation">Aspose.ZIP for .NET - 安装</h2>
<p>安装此 SDK 非常简单。只需<strong><u>下载</u></strong><strong><u>SDK</u></strong> 文件或运行以下命令:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>Install-Package Aspose.Zip</code></pre>
<p>或者,通过 NuGet 包管理器安装。</p>
<h2 id="code">如何在 C# 中以编程方式压缩 LZIP 文件</h2>
<p><strong>Aspose.ZIP for .NET</strong>提供了一整套使用 C# 处理 LZIP 文件的功能。不过,我们可以使用此 SDK 压缩和解压 LZIP 文件。首先,让我们看看如何压缩 LZIP 文件。</p>
<p>您可以按照以下步骤操作:</p>
<ul>
<li>定义工作目录的路径并加载许可证。</li>
<li>创建LzipArchive类的实例。</li>
<li>调用SetSource方法来设置档案中要压缩的内容。</li>
<li>调用Save方法将 LZIP 存档创建到提供的目标文件。</li>
</ul>
<p>以下代码示例展示了如何在 C# 中压缩 LZIP 文件:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>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");
}
}
}</code></pre>
<p>将生成以下输出:</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20250903/105720_7.png"></p>
<h2 id="extract-lzip-files-using-asposezip-for-net">使用 Aspose.ZIP for .NET 提取 LZIP 文件</h2>
<p>现在我们将介绍如何使用相同的 SDK 提取 LZIP 文件。为此,我们将使用Extract方法解压 LZIP 文件,如以下代码示例所示:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>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");
}
}
}</code></pre>
<p>输出:</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20250903/105916_8.png"></p>
<h2 id="conclusion">结论</h2>
<p>我们已经了解了如何借助<strong><u>Aspose.ZIP for .NET</u></strong>,通过几行源代码高效地处理大文件。归档压缩可以减小大文件的大小,以便您轻松分发它们。我们已经实现了如何在 C# 中以编程方式压缩和解压缩 LZIP 文件。</p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19071747
頁:
[1]