在本指南中,我们将介绍如何使用 C# 提取 RAR 文件的过程。它具有设置开发环境的资源、详细说明编程逻辑的步骤列表以及使用 C#** 解压缩 RAR 文件的可运行示例代码。您将了解仅根据不同条件提取所选文件的选项。
使用 C# 解压 RAR 文件的步骤
- 设置环境以使用Aspose.Zip for .NET提取RAR文件
- 使用 RarArchive 类对象加载源 RAR 文件
- 解析 RAR 文件中的所有条目
- 在每次迭代中使用 entry 名称创建文件流
- 从源条目中读取所有字节并将其保存在文件流中
- 写入所有字节后将每个文件保存在磁盘上
这些步骤详细说明了如何使用 C#* 提取 RAR。该过程首先加载源 RAR 文件,然后解析其中的所有条目。使用其名称为每个条目创建一个单独的文件,并将存档中的所有字节保存到相应的文件中,然后再将其保存到磁盘上。
使用 C# 提取 RAR 文件的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Aspose.Zip; | |
using Aspose.Zip.Rar; | |
class Program | |
{ | |
static void Main(string[] args) // Extract RAR | |
{ | |
// Set the license | |
new License().SetLicense("Aspose.Total.Product.Family.lic"); | |
// LLoad the RAR file | |
using (RarArchive rarArchive = new RarArchive("Sample.rar")) | |
{ | |
// Parse all the entries in the archive | |
foreach(var entry in rarArchive.Entries) | |
{ | |
// Create a file | |
var file = File.Create(entry.Name); | |
// Open the archive and save data to the file | |
using (var fileEntry = entry.Open()) | |
{ | |
byte[] data = new byte[1024]; | |
int bytesCount; | |
while ((bytesCount = fileEntry.Read(data, 0, data.Length)) > 0) | |
file.Write(data, 0, bytesCount); | |
// Close the file | |
file.Close(); | |
file.Dispose(); | |
} | |
} | |
} | |
Console.WriteLine("Done"); | |
} | |
} |
本文向我们介绍了使用 C# 解压 RAR 的过程。如果您想解压 ZIP 文件,请参阅 如何在 C# 中提取 ZIP 文件 上的文章。