How to Create Zip File in C# .NET Core

Introduction

The name of the file format created by Phil Katz and first made popular by PKZIP is where the term "ZIP" originates. It is now a commonly used and well-known file compression standard for a variety of software programs and operating systems.

To create, extract, or work with ZIP files, users can use a variety of software tools, operating system built-in functions, or command-line interfaces. This allows for effective file management and compression.

A common archival file format and file compression tool for packing and compressing one or more files and directories into a single file is ZIP. Frequently, the ".zip" file extension serves as a clue. In this article, we will create a zip file C# .NET Core.

How to Create ZIP Files C# using .NET Core

  1. Launch a fresh Asp.NET Core project.
  2. To the generated project, install the IronZIP library.
  3. Using the IronArchive class, instantiate a blank ZIP file.
  4. To add files to the empty ZIP file, use the AddArchiveEntry method.
  5. You may use the FromFile method to open an existing ZIP file.

IronZIP Library

The .NET library IronZIP, developed by Iron Software, simplifies the handling and management of ZIP files in .NET applications. It provides programmers with capabilities and tools to enable them to work with ZIP archives in several ways.

These include creating, removing, compressing data, opening, decrypting, and editing the files and folders contained within ZIP files.

Features of IronZIP

  • The ability for .NET applications to create fresh ZIP archives from the beginning is known as ZIP File Creation.
  • Programmers can add, remove, update, and perform other operations on files and folders included in ZIP packages using file manipulation.
  • Performance optimization aims to maximize performance so that large ZIP files or datasets may be handled more efficiently by .NET programs.
  • Extraction and Decompression: The ability to extract content and delete files and directories from pre-existing ZIP packages.
  • Compression: Offers methods to reduce the size of files and folders into ZIP archives for storage or transportation.
  • Password protection and encryption: This feature allows you to encrypt ZIP files and apply password protection to secure the contents of ZIP archives.
  • Compatibility: Designed to work in a range of .NET environments and compatible with several framework versions.

IronZIP may be a helpful tool for developers to manage ZIP files for their apps. It offers a range of functions, both simple and versatile, for interacting with ZIP archives programmatically within the .NET framework.

Since software libraries are changed and improved over time, consult the official documentation, release notes, or the IronZIP website for the most up-to-date information on features, capabilities, compatibility, and any new functionality introduced in subsequent versions. To know more check here.

Creating a New Project in Visual Studio

Select File from the File menu after launching the Visual Studio program. After choosing "new project," choose ".NET Core console application."

How to Create Zip File in C# .NET Core: Figure 1

Enter the project name in the designated text field after choosing the file location. Next, as seen in the sample below, click the Create button and choose the required .NET Framework.

How to Create Zip File in C# .NET Core: Figure 2

The Visual Studio project will then construct its structure based on the selected application after that. If you want to create or execute the application via the console, Windows, or web application, you may add code by gaining access to the program.cs file.

How to Create Zip File in C# .NET Core: Figure 3

After that, the code may be tested and the library added.

3.1 Using Visual Studio

You may install packages directly into your solution using the Visual Studio software's NuGet Package Manager functionality. You may use the snapshot below to access the NuGet Package Manager.

How to Create Zip File in C# .NET Core: Figure 4

It provides a search box that pulls up a NuGet website package list. The screenshot below shows us where to look for the phrase "IronZip" in the package manager.

How to Create Zip File in C# .NET Core: Figure 5

In the image above, a list of pertinent search terms is displayed. Selecting the required option is required to install the solution package.

3.2 Using the Visual Studio Command-Line

To view the Package Manager Console, select Tools > NuGet Package Manager in Visual Studio.

Add the following line to the terminal tab of the package manager:

Install-Package IronZip

The package may now be used after downloading and installing it into the active project.

3.3 Direct download from the NuGet website

The NuGet package may be downloaded straight from the website by using the third method.

  • To access the Link, navigate there.
  • From the menu on the right, choose the download package option.
  • When the package has finished downloading, double-click it. It will install itself when it wants to.
  • Start utilizing the solution in the project after loading it again.

3.4 Direct download from the IronZIP website

To download the latest recent bundle straight away from the website, click this link. To include the package in the project, download it and adhere to the given guidelines.

  • From the solution window, choose the project by doing a right-click.
  • After it has been downloaded, pick the reference and look through its location.
  • Once the reference has been added, click OK.

Create a Zip file using IronZIP

To use IronZIP to produce a ZIP archive file in C#, you must add files or directories to an archive before saving it. An example of using IronZIP to produce a ZIP file can be found below.

using IronZip;
using (var archive = new IronZipArchive())
{
    archive.AddArchiveEntry("1.png");
    archive.AddArchiveEntry("2.png");
    archive.SaveAs("new.zip");
}
using IronZip;
using (var archive = new IronZipArchive())
{
    archive.AddArchiveEntry("1.png");
    archive.AddArchiveEntry("2.png");
    archive.SaveAs("new.zip");
}
Imports IronZip
Using archive = New IronZipArchive()
	archive.AddArchiveEntry("1.png")
	archive.AddArchiveEntry("2.png")
	archive.SaveAs("new.zip")
End Using
VB   C#

In the code first, we create an object for the IronZipArchive which will allow us to use the method AddArchiveEntry. We can also able to pass the compression level as a parameter while creating the object.

We can then begin creating ZIP files with multiple files at a time by passing the file path as a string array or string value it can support both values.

Then with the help of the SaveAs method, we can save the new zip file into the file system with the specified directory. It will save all the files into a single zip file.

To read more about the IronZIP click here.

Conclusion

IronZIP is a .NET library created especially for managing ZIP files in C# applications. It allows developers to manage the compression, extraction, and modification of ZIP files.

IronZIP uses a clear API to simplify complex processes and make file operations easier, such as extracting specific files, extracting files in bulk, adding entries, and altering existing archives.

Developers may enhance file management capabilities in C# projects without having to start from scratch by using this package. It fits in with ease. Because of its flexibility, files may be added, modified, and removed from ZIP files dynamically to meet the needs of different applications.

IronZIP's robust exception-handling tools provide a consistent workflow and facilitate error correction in scenarios such as missing or damaged ZIP files.

While my memory may not match the latest updates, IronZIP provided a reliable option for handling ZIP files in .NET settings in the past.

IronZIP offers a free trial, with pricing starting from $599.

We may go here to learn more about which licensing version would work best for you and your team. Check out this link for the Iron software product.