USING IRONZIP

ZipArchive C# (Developer Tutorial)

Published January 4, 2024
Share:

In the field of software development, the need to manage and organize files efficiently is of utmost importance. One way to achieve this is through the use of archive files, commonly known as zip files. A zip file is a versatile archive format that excels at compressing and bundling multiple files and directories.

It uses compression algorithms to reduce file sizes, stores compressed files with their relative path, supports both text and binary data, and can include multiple entries within a single file or archive. This makes zip files a widely used and efficient solution for organizing, sharing, and storing various types of data. These files not only facilitate the storage and transfer of data but also help in reducing existing file sizes, making them more manageable.

In this article, we will explore creating zip archives in C# and how IronZIP, a powerful C# ZIP archive library, can streamline the process.

IronZIP - C# ZIP Archive Library

IronZIP stands out as a leading C# ZIP archive library designed for creating, reading, and extracting archives in a .NET workspace.

What sets IronZIP apart is its user-friendly API, prioritizing accuracy, ease of use, and speed. Whether you are working on desktop, mobile, or cloud applications, IronZIP provides a robust solution for managing archive files seamlessly.

Key Features of IronZIP

  • Cross-Platform Support: IronZIP is compatible with a wide range of platforms, including Windows, Linux, Mac, iOS, Android, Docker, Azure, and AWS.
  • .NET Compatibility: Fully supports .NET 7, 6, Core, Standard, and Framework.
  • File Format Support: Handles ZIP, TAR, GZIP, and BZIP2 archives.
  • Versatility: Designed for C#, F#, and VB.NET, supporting various project types, including web, mobile, desktop, and console applications.
  • Easy Integration: Integrates seamlessly with popular IDEs such as Microsoft Visual Studio and JetBrains ReSharper & Rider.

Prerequisites

Before diving into the implementation, ensure you have the following prerequisites in place:

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

Steps to Create C# Console Project in Visual Studio

  1. Open Visual Studio and create a new C# console project.
  2. Configure the project name and location.
  3. Choose the appropriate .NET version based on your project requirements. IronZIP supports the latest .NET Framework as well.

Installing IronZIP using NuGet Package Manager

IronZIP can be easily integrated into the project using the NuGet Package Manager. Follow these steps:

  1. Open the Manage NuGet Packages for Solution by right-clicking the Solution Explorer.
  2. In the NuGet browse tab, search for IronZIP and click install.
  3. Alternatively, from the tools menu select NuGet Package Manager Console. In the console, run the following command to install IronZip:
Install-Package IronZip

Now that IronZIP is integrated into our project and ready for use, let's look at the steps of creating a new ZipArchive file system.

Steps to Create Zip Archive File System

Creating a zip file becomes remarkably easy with IronZIP, thanks to its user-friendly API and straightforward syntax, allowing developers to efficiently compress and organize files in just a few lines of code. Follow these steps to create a C# zip archive file system:

Step 1: Import Namespace

Import the IronZIP namespace in your C# file:

using IronZip;
using IronZip;
Imports IronZip
VB   C#

Step 2: Create an Empty ZipArchive and Add files to the Zip File

In C# with IronZIP, the process begins by instantiating an IronArchive object, which represents the ZIP archive. This object acts as a container for the files you want to include in the archive. Initially, the archive is empty, and you add files to it one by one.

Once the IronArchive object is created, you can use the Add method to specify the files you want to include in the zip archive. This method takes the file paths as parameters, allowing you to add multiple files as a new entry in the archive.

The beauty of IronZIP lies in its simplicity and efficiency. With just a few lines of code, you can create a zip archive and populate it with the desired files. Now, let's take a look at the code in the following example that accomplishes this task:

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

The above example shows a code snippet:

  • The using statement ensures that the IronArchive object is properly managed, and resources are released when the block is exited. This is especially important for handling file resources.
  • The IronArchive constructor takes a string parameter, which is the path and name of the zip file to be created. In this case, it's "output.zip".
  • The Add method is then employed to include three image files (image1.jpg, image2.jpg, and image3.jpg) in the zip archive. Adjust the file paths according to the location of your files. The paths are relative to the location of the executable or the working directory of the application.

By utilizing IronZIP in this manner, you can seamlessly create an empty zip archive folder and populate it with the necessary files, making it a straightforward and efficient process. For more detailed information on IronZIP, please visit this documentation page.

Step 3: Execute the Program to get the New ZipAchive

Now let's build and run our zip archive application. After the application runs successfully, check the project directory for the generated "output.zip" file. This file should contain the specified image files. Here is the output zip file created by the zip package with 3 images we added:

`ZipArchive` C# (Developer Tutorial): Figure 1

Conclusion

In conclusion, IronZIP emerges as a robust and versatile solution for handling zip archives in C#, whether you want to compress a single file, multiple files, files of different types (such as a compressed text file or PNG), or decompress files. Its cross-platform support, compatibility with various .NET versions, and straightforward integration make it a valuable tool for developers. You can seamlessly create, read, and extract zip archives in your C# projects, enhancing the efficiency of your file management processes.

IronZIP in a commercial project, you would need to purchase a license. Its lite package starts from $749. For more detailed licensing information please visit this license page.

However, Iron Software also provides a free trial version of its products, including IronZIP. This allows developers to explore the features and capabilities of the library before making a purchase decision. During the trial period, users can evaluate how well IronZIP meets their requirements and assess its suitability for their projects.

You can download the IronZIP library from here.

< PREVIOUS
How to Extract ZIP File in C# Windows Application
NEXT >
How to Unzip Files in .NET Core

Ready to get started? Version: 2024.11 just released

Free NuGet Download Total downloads: 5,522 View Licenses >