USING IRONZIP

.NET ZipArchive (Developer Tutorial)

Updated December 13, 2023
Share:

In the ever-growing field of software development, efficient file management is of utmost importance while sending and receiving files in large numbers. The need to compress, bundle, and extract large files is a common requirement and ZIP archives serve as a versatile solution.

Microsoft .NET simplifies working with ZipArchive files through its dedicated class. For a comprehensive guide on utilizing this functionality, refer to the official Microsoft documentation on the ZipArchive Class.

In this article, we will take you beyond the .NET ecosystem and explore how IronZIP, as a robust C# ZIP archive library, simplifies archive management and provides developers with a user-friendly API for creating, reading, and extracting archives seamlessly.

Quick Overview on Managing a ZipArchive File in C#

  1. Create a C# Console Application in Visual Studio.
  2. Install Zip Library from NuGet Package Manager.
  3. Create an Empty Zip File using IronArchive.
  4. Add Files to Zip Archive using the AddArchiveEntry method.
  5. Extract Files from ZipArchive using the ExtractArchiveToDirectory method.

Introducing IronZIP

IronZIP stands out as a leading C# ZIP archive library designed to meet the demands of .NET developers. Prioritizing accuracy, ease of use, and speed, IronZIP offers a comprehensive set of features that make it a go-to solution for archive-related tasks.

Whether you're working on desktop, mobile, or cloud applications, IronZIP's cross-platform support ensures consistent performance across various environments.

Key Features of IronZIP:

  1. Cross-Platform Support: IronZIP seamlessly runs on Windows, Linux, Mac, iOS, Android, Docker, Azure, and AWS, catering to diverse development environments.
  2. .NET Compatibility: Fully supporting .NET 7, 6, Core, Standard, and Framework, IronZIP adapts to the latest technologies in the .NET ecosystem.
  3. File Format Support: IronZIP handles ZIP, TAR, GZIP, and BZIP2 archives, offering versatility in dealing with different archive formats.
  4. Versatility: Designed for C#, F#, and VB.NET, IronZIP supports various project types, including web, mobile, desktop, and console applications.
  5. Easy Integration: IronZIP effortlessly integrates into popular IDEs such as Microsoft Visual Studio and JetBrains ReSharper & Rider, enhancing the developer experience.
  6. Compression Level: IronZIP provides a compression level feature that can be set from scale 0 to 9 to compress files and generate storage-efficient Zip files.

Prerequisites

Before moving on to the implementation, ensure the following prerequisites are in place:

  1. Visual Studio: Install Visual Studio, the comprehensive integrated development environment for building .NET C# applications. If not installed, you can download it from its official website.
  2. IronZIP Package: Use NuGet Package Manager to install the IronZIP library for your project.

Steps to Create a ZipArchive File System in .NET

Step 1 Create a C# Console Project in Visual Studio

  • Open Visual Studio and create a new C# console project.
  • Configure the project name and location.

    .NET ZipArchive (Developer Tutorial): Figure 1 - Configuring a New VS Project

  • Choose the appropriate .NET version based on your project requirements. IronZIP supports the latest version and older versions of .NET and .NET core, so you can choose any version from the available list.

    .NET ZipArchive (Developer Tutorial): Figure 2 - Selecting your .NET framework version

Step 2 Installing IronZIP using NuGet Package Manager

Integrating IronZIP into the project is a very straightforward process. You can install the IronZIP library as so:

Open the Manage NuGet Packages for Solution by right-clicking the Solution Explorer.

.NET ZipArchive (Developer Tutorial): Figure 3 - Using the Solution Explorer

In the NuGet browse tab, search for IronZIP and click install.

.NET ZipArchive (Developer Tutorial): Figure 4 - Installing the IronZIP package

Alternatively, you use the NuGet Package Manager Console and run the command:

Install-Package IronZip

That's it! We have set up everything, and we can proceed to create Zip files in our application.

Step 3 Creating a .NET ZipArchive File Stream

1. Import IronZIP Namespace

First, we need to include the IronZIP namespace in our source code main file by using the statement:

using IronZIP;
using IronZIP;
Imports IronZIP
VB   C#

2. Create a Zip File

Creating a zip file becomes remarkably easy with IronZIP, thanks to its user-friendly API and straightforward syntax. Follow these steps to create a C# zip archive file system

Code Example:

class Program
{
    public static void Main(string[] args)
    {
    var file = "output.zip";
        // Create an empty ZIP archive
        using (var archive = new IronArchive(file))
        {
            // Add files to the ZIP
            archive.AddArchiveEntry("./assets/image1.jpg");
            archive.AddArchiveEntry("./assets/image2.jpg");
            archive.AddArchiveEntry("./assets/image3.jpg");
        }
        Console.WriteLine("Zip file generated successfully!");
    }
}
class Program
{
    public static void Main(string[] args)
    {
    var file = "output.zip";
        // Create an empty ZIP archive
        using (var archive = new IronArchive(file))
        {
            // Add files to the ZIP
            archive.AddArchiveEntry("./assets/image1.jpg");
            archive.AddArchiveEntry("./assets/image2.jpg");
            archive.AddArchiveEntry("./assets/image3.jpg");
        }
        Console.WriteLine("Zip file generated successfully!");
    }
}
Friend Class Program
	Public Shared Sub Main(ByVal args() As String)
	Dim file = "output.zip"
		' Create an empty ZIP archive
		Using archive = New IronArchive(file)
			' Add files to the ZIP
			archive.AddArchiveEntry("./assets/image1.jpg")
			archive.AddArchiveEntry("./assets/image2.jpg")
			archive.AddArchiveEntry("./assets/image3.jpg")
		End Using
		Console.WriteLine("Zip file generated successfully!")
	End Sub
End Class
VB   C#

Explanation:

  • The using statement ensures proper management of the IronArchive object, releasing resources when the block is exited.
  • The IronArchive constructor takes in a string parameter, which represents the path and name of the entire zip file to be created.
  • The AddArchiveEntry method is employed to include three image files as entries in the zip archive. Adjust file paths based on your file locations.

3. Extract Zip File Content

Extracting the archive contents from zip files with an application is another handy feature to have. IronZIP with its one-line code helps to extract data from the ZipArchive archive file system effortlessly. Write the sample code to achieve this task:

using IronZIP;
// Extract ZIP
IronArchive.ExtractArchiveToDirectory("output.zip", "extracted");
using IronZIP;
// Extract ZIP
IronArchive.ExtractArchiveToDirectory("output.zip", "extracted");
Imports IronZIP
' Extract ZIP
IronArchive.ExtractArchiveToDirectory("output.zip", "extracted")
VB   C#

That's it! Zip file extracted. IronZIP also offers aid with adding files to the existing file system of ZipArchive. For more detailed information please visit this tutorial on create, read, and extract Zip on the IronZIP website.

Step 4 Execute the Program to Get the New ZipArchive

Build and run the Zip archive application. After successful execution, check your project directory with the specified entry name in the program for the resulting Zip file: "output.zip" containing the specified image files. Here is the output Zip file content-disposition:

.NET ZipArchive (Developer Tutorial): Figure 5 - Output Zip return file of above code

Conclusion

In conclusion, IronZIP emerges as a powerful and versatile solution for handling zip archives in C#. Its cross-platform support, compatibility with various .NET versions, and straightforward integration make it a valuable tool for developers.

IronZIP simplifies the creation, reading, and extraction of zip archives in your C# projects and enhances the efficiency of file management processes.

To learn more about IronZIP and its capabilities, please visit this documentation page.

IronZIP is a commercial product, and for use in commercial projects, a license is required. The lite package starts from $599, and detailed licensing information is available on the IronZIP license page version of IronZIP, allowing developers to explore its features before making a purchase decision. Download the IronZIP library from here.

< PREVIOUS
How to Decompress Zip Files in C#
NEXT >
.NET MAUI Data Extraction SDK (Developer Tutorial)

Ready to get started? Version: 2024.5 just released

Start Free Trial Total downloads: 1,900
View Licenses >