Zip files are everywhere in the digital world, serving as a convenient way to bundle and compress data. In C#, extracting files from them is crucial for many applications. While various solutions exist, IronZip emerges as a powerful and user-friendly library for handling zip archives. This article simplifies extracting zip files using IronZip, guiding you through its methods and offering practical examples.
IronZip:
IronZip is a C# library that allows you to create, read, and extract Zip files in your .NET applications. It is easy to use and has a high-performance compression and decompression engine. Its intuitive API and rich features make it a popular choice for developers working with zip files in C#.
Extracting Zip Files in C#:
- Create or Open Existing Project
- Install the IronZip NuGet package into your project.
- Include the IronZip namespace in your code.
- Determine the destination folder where you want to extract the files.
- Extract all files and folders to the specified destination.
- Extract all files from the Password Protected Zip file
- Access Existing Zip file
Create or Open C# Project:
The very first step is to create or open the project where we need to integrate zip file functionality. We can use any project including Blazor, MAUI, ASP.NET WEB APIs, ASP.NET MVC, WEB Forms, Windows forms, or Console applications. In this tutorial, I will create a C# Console Application to make this tutorial simple and easier. The same code will work for each application type.
- Launch Visual Studio from your Start menu or desktop shortcut.
- Go to File > New > Project.
- Type "console application" in the search bar and select the Console Application template. Choose the desired programming language, like C# or Visual Basic. Press Next.
- Give your project a descriptive name. Choose a location to save your project files. Press Next.
- Choose a Target .NET Framework. Press Create.
Install IronZip Package
To begin using IronZip, quickly install the IronZip NuGet Package in your project. Enter the following command in the NuGet Package Manager Console:
Install-Package IronZip
Alternatively, download the package directly from the official IronZip NuGet website.
After completing the installation, kickstart your C# code by adding the 'using IronZip' statement at the beginning.
Apply License Key
To use IronZip, you need a license or trial key. You can get a free trial key from the IronZip Official Website. Set the LicenseKey property of the License class to your key. Do this after importing IronZip and before calling any IronZip methods.
IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01";
This step is vital for accessing all the features of IronZip in your project.
Using C# to extract Zip files
Below are code samples demonstrating how to work with Zip files in C#, focusing on extracting files, handling passwords, and accessing existing archives.
Extract a Zip file using C#:
The following code sample will unzip files into a new directory using IronZip.
public static void decompress()
{
IronZipArchive.ExtractArchiveToDirectory("PDFFiles.zip", "PDF Files");
}
The above code uses the IronZip library, which provides functionality for working with ZIP archives in C#. The code IronZipArchive.ExtractArchiveToDirectory("PDFFiles.zip", "PDF Files"); uses the IronZip library to extract the contents of the "PDFFiles.zip" archive to the specified directory, in this case, "PDF Files." The ExtractArchiveToDirectory method simplifies the extraction process by automatically handling the decompression of the ZIP file and placing its contents into the designated folder.
This one-liner encapsulates the functionality of extracting a ZIP archive concisely, contributing to a streamlined and efficient approach in C# for working with compressed files.
Extract Password Protected Zip File in C#:
IronZip provides us with methods to create and extract password-protected zip files. In this example, we will learn to extract password-protected zip files in C# using just 1 line of code. We will extract the following zip file.
Write the following code to extract files.
IronZipArchive.ExtractArchiveToDirectory("PDFDocs.zip", "Old dDocs","xyz" );
IronZip provides the ExtractArchiveToDirectory method in the IronZipArchive class, which extracts all the files from a ZIP archive to a specified directory.
It passes three arguments to the method: the path of the ZIP file (“OldDocs.zip”), the path of the destination directory (“Old Docs”), and the password of the ZIP file (“xyzPassword”). It assumes that the ZIP file is encrypted with AES-128, AES-256, or traditional algorithms which are supported by IronZip. If the password is incorrect the method will throw an exception.
Access Existing Archive:
We can access the existing zip file and view all the entries of the specified zip file. The following code will open the zipped file and list all the entries on the console.
public static void Main(string[] args)
{
using (var archive = new IronZipArchive("OldDocs.zip","xyzPassword"))
{
// Get Entries list
List<string> names = archive.GetArchiveEntryNames();
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
We have created a new instance of the IronZipArchive class, passing the path of the ZIP file (“OldDocs.zip”) and the password of the ZIP file (“xyzPassword”) as parameters. Pass null if the file is not encrypted. This opens the ZIP file and loads its contents into the memory stream. The instance is assigned to a variable named archive and is wrapped in a using statement, which ensures that the ZIP file is closed and disposed of when the code block ends.
It calls the GetArchiveEntryNames method of the archive object, which returns a list of strings containing the names of all the entries in the ZIP archive. The list is assigned to a variable named names. It iterates over the names list using a foreach loop and prints each name to the console using the Console.WriteLine method. This displays the names of the files and folders in the ZIP archive.
The Output is as:
Ironzip also provides methods to create and extract other zip archive format such as TAR (gz file), GZIP, and BZIP2.
Conclusion:
In conclusion, mastering the extraction of zip files in C# is an essential skill for developers dealing with data compression and bundling. The IronZip library emerges as a powerful ally in this regard, offering a user-friendly API and high-performance capabilities. Ironzip also provides methods to compress multiple files into a new zip file. Developers can seamlessly integrate IronZip into their C# projects by following the comprehensive guide outlined in this article, from setting up the environment to extracting files with and without passwords. The library's versatility extends beyond simple extraction, allowing create archives, access to existing archives, and handling various zip formats. IronZip offers a free trial allowing developers to make an informed decision. They can develop, test & deploy on production with a free trial. Developers can purchase a license once exploring all the features of IronZip.
I hope you have liked the tutorial, feel free to ask any question in the comment section.
Top comments (0)