DEV Community

Cover image for How to Create a Zip Archive in C#
Zeeshan Wazir
Zeeshan Wazir

Posted on • Edited on

How to Create a Zip Archive in C#

Introduction

In the field of software development, efficient file management plays a pivotal role. Archive files, commonly known as zip files, serve as a versatile solution for compressing and bundling multiple files and directories. Zip files employ compression algorithms to reduce file sizes, store compressed files with their relative paths, and support both text and binary data. This makes them a widely adopted choice for organizing, sharing, and storing diverse data types. The advantages include facilitating data storage and transfer, as well as optimizing file sizes for better manageability. Offering compression algorithms that reduce file sizes, zip files are instrumental in organizing, sharing, and storing data.

This article will help you to manage ZIP archives in C# and introduces IronZip, a powerful C# ZIP archive library, to streamline the process.

How to Manage Zip Archive in C

  1. Create a C# project in Visual Studio.

  2. Install Zip Library via NuGet Package Manager.

  3. Create a Zip File using IronArchive class.

  4. Extract a Zip File using ExtractArchiveToDirectory method.

  5. Add Files to existing Zip using IronArchive.FromFile method.

Introduction to IronZip

IronZip stands out as a premier C# ZIP archive format library designed for creating, reading, and extracting archives in .NET applications. Prioritizing accuracy, ease of use, and speed, IronZip boasts a user-friendly API suitable for desktop, mobile, and cloud applications alike.

Image description

Key Features

Here are some important key features of IronZip:

Cross-Platform Support:

  • Compatible with Windows, Linux, Mac, iOS, Android, Docker, Azure, and AWS.

  • Designed for C#, F#, and VB.NET running on .NET 7, 6, 5, Core, Standard, or Framework.

.NET Compatibility:

  • Fully supports .NET 7, 6, Core, Standard, and Framework.

File Format Support:

  • Handles ZIP, TAR, GZIP, and BZIP2 archives.

Versatility:

  • Supports various project types, including web (Blazor & WebForms), mobile (Xamarin & MAUI), desktop (WPF & MAUI), and console (App & Library) applications.

Compatibility with Environments and IDEs:

  • Supported on Windows (10+, Server 2018+), Linux (Ubuntu, Debian, CentOS, etc.), iOS (12+), Android API 21+ (v5 “Lollipop”), Docker (Windows, Linux, Azure), Azure (VPS, WebApp, Function), AWS (EC2, Lambda).

  • Compatible with IDEs such as Microsoft Visual Studio and JetBrains ReSharper & Rider.

Binary Certification:

  • DigiCert Signed Binaries for enhanced security.

Supported File Formats:

  • Build Archive from File Formats including images (jpg, png, gif, tiff, svg, bmp), text files, documents (PDFs, DOCX, XLSX), and audio (MP3, WAV).

  • Open and compress ZIP, TAR, GZIP, BZIP2 archives.

Prerequisites

Before moving onto the implementation, please ensure you have the following prerequisites in place:

  1. Visual Studio: Install Visual Studio, a comprehensive integrated development environment (IDE) for building C# applications. If not installed, download it from its official website.

  2. IronZip Package: Use NuGet Package Manager to install the IronZip library for your project.

Create a C# Zip Archive Project

Creating a project in C# that deals with zip archives is simplified with the powerful IronZip library, providing seamless management of compressed files. Developers can easily incorporate IronZip's robust capabilities to create, manipulate, and read zip archives in their C# projects.

To kickstart the process, a C# console application to create zip files is set up using Visual Studio, ensuring compatibility with the desired .NET version. The integration of IronZip is streamlined through the NuGet Package Manager, where developers can effortlessly install the library to unlock comprehensive zip functionality. Let's get started!

Steps to Create Visual Studio Console Project

  1. Open Visual Studio.

  2. Create a new C# console project.

  3. Configure project name and location.

  4. Choose the appropriate .NET version. IronZip supports the latest version so you can even choose .NET 8.0.

Install IronZip using NuGet Package Manager

IronZip seamlessly integrates 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 or from the tools menu.

  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

Managing Zip Archive in C# using IronZip Library

Now, we will look how to manage zip archive files in C#. Let's take a look at the code samples to create, extract and add files to existing zip file using IronZip.

Create a Zip Archive - Compress Files

using IronZip;

class Program
{
    public static void Main(string[] args)
    {
    // Output filename
    var file = "output.zip";

        // Create an empty ZIP archive
        using (var archive = new IronArchive(file))
        {
            // Add all the files to the ZIP
            archive.AddArchiveEntry("./assets/image1.jpg");
            archive.AddArchiveEntry("./assets/image2.jpg");
            archive.AddArchiveEntry("./assets/image3.jpg");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

The above code utilizes IronZip IronArchive class to create an empty ZIP archive named "output.zip" and adds three image files from the "./assets/" directory using AddArchiveEntry method. With the simplicity of IronZip, developers can efficiently generate a zip archive and include specified files in just a few lines of code. You can also control the compression level as an optional parameter in IronArchive object.

Extract an Archive File System

The following code sample decompress files from a zip file and place it in the specified directory:

using IronZip;

class Program
{
    public static void Main(string[] args)
    {
        // Extract ZIP
        IronArchive.ExtractArchiveToDirectory("output.zip", "extracted");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

IronZip provides a simple but yet powerful ExtractArchiveToDirectory method with extract the contents from any zip file provided. First parameter accepts the zip archive file and second parameter accepts the path to the directory where the files should be extracted.

Add Files to an Existing Archive

The following source code sample allows you to add multiple files to the existing file:

using IronZip;

class Program
{
    public static void Main(string[] args)
    {
    // Output filename
    string filename = "result.zip";

        // Open existing ZIP and export ZIP
        using (var archive = IronArchive.FromFile("existing.zip", filename))
        {
            // Add files
            archive.AddArchiveEntry("./assets/image3.png");
            archive.AddArchiveEntry("./assets/image4.png");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This code opens an existing zip, creates a new zip named "result.zip," and adds two image files to it. IronArchive FromFile method takes two parameters; one existing file and second new file name. Same AddArchive method is used to add files.

Image description

For a comprehensive list of methods and in-depth documentation on zipping and extracting files using IronZip, refer to the IronZip API documentation.

Conclusion

In conclusion, IronZip stands out as a robust 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. Seamlessly creating, reading, and extracting zip archives in your C# projects enhances the efficiency of your file management processes. IronZip's user-friendly API provides a straightforward solution. To explore the efficiency of IronZip in your projects for seamless archive management visit the documentation page with more detailed information.

For commercial projects, a license is required, with pricing starting from $749. A free trial license is also available for users to explore its features before making a purchase decision. Download the IronZip library and elevate your C# file management capabilities.

Top comments (0)