RAR is a commonly used archive file format that supports lossless compression of data. Often, people prefer RAR over ZIP because it provides a better compression rate and keeps the archive size smaller. However, in certain cases, you may need to convert RAR files to ZIP archives. To achieve that, this article covers how to convert a RAR file to ZIP programmatically in C#.
C# .NET API for RAR to ZIP Conversion
Aspose.ZIP for .NET is a powerful archiving API that supports the creation and manipulation of popular archive formats. We will utilize this API to convert the RAR files to ZIP format. To use the API, you can install it from NuGet or download its DLL from the downloads section.
PM> Install-Package Aspose.Zip
Convert a RAR File to ZIP in C#
The RAR files can be converted to ZIP format within a few steps using Aspose.ZIP for .NET. The following are the steps to convert a RAR file to a ZIP archive in C#.
Create an instance of Archive class for ZIP archive.
Load the RAR archive using RarArchive class.
Loop through the entries of RAR archive using RarArchive.Entries collection.
For each entry in RAR, perform the following steps:
Extract the entry to MemoryStream object using RarArchive.Entries[int].Extract(MemoryStream) method.
Add the entry to ZIP using Archive.CreateEntry(String, MemoryStream) method.
Finally, save the resultant ZIP archive using Archive.Save(String) method.
The following code sample shows how to convert a RAR file to ZIP format in C#.
Get a Free API License
You can get a free temporary license to use Aspose.ZIP for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to convert a RAR archive to ZIP format programmatically in C#. The step-by-step guide and code sample have demonstrated the conversion of a RAR file to a ZIP archive within a few steps. You can read the documentation of Aspose.ZIP for .NET to explore other features. Moreover, you can share your queries with us via our forum.