In this blog post, we’ll explore how to create ZIP files in C# for various scenarios. Whether you want to compress a single file, multiple files, or even an entire folder, you can quickly create a ZIP archive from within your .NET applications. Moreover, you will also explore the quick process to create a ZIP file Online and Free.
- Online Create ZIP File for Free
- Create ZIP File – C# API Installation
- Create ZIP for File in C#
- Create ZIP for Folder in C#
Online Create ZIP File for Free
You can compress and organize your files into a single, convenient package with this free ZIP file creator. Moreover, you can also pick the files from your system or an online link to access the files. You do not need to enter any credit card details or an email to work with this utility.
Create ZIP File – C# API Installation
You can create a ZIP archive to compress multiple files into a single file. Simply configure Conholdate.Total for .NET by accessing it from the Releases section or install it with the following NuGet command:
Install-Package Conholdate.Total
Create ZIP for File in C#
This section covers different approaches to creating a ZIP archive for compressing files. It discusses compressing a single file as well as multiple files into a single ZIP directory as per your requirements.
Create a ZIP File for a Single File in C#
Learn the step-by-step process of creating a ZIP file for a single file using C#. The following steps and the code snippet will guide you through the process and explain how to efficiently package your file into a compressed archive.
- Create an instance of FileStream class and specify the name for the output ZIP file.
- Specify the file to be added to the archive.
- Initialize an instance of the Archive class.
- Create the output ZIP archive.
The following code snippet demonstrates how to create a ZIP archive for a single file in C#:
using (FileStream zipFile = File.Open(dataDir + "CompressSingleFile_out.zip", FileMode.Create)) | |
{ | |
//File to be added to archive | |
using (FileStream source1 = File.Open(dataDir + "test.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var archive = new Archive(new ArchiveEntrySettings())) | |
{ | |
archive.CreateEntry("test.txt", source1); | |
archive.Save(zipFile); | |
} | |
} | |
} |
Create a ZIP Archive for Multiple Files in C#
The following steps show how to create a ZIP directory for multiple files in C#:
- Specify the source files to be zipped, as well as the name of the output file.
- Create entries for the target files using the CreateEntry method.
- Save the output ZIP file containing multiple files.
The sample code below shows how to create a ZIP archive for multiple files in C#:
using (FileStream zipFile = File.Open(dataDir + "CompressMultipleFiles_out.zip", FileMode.Create)) | |
{ | |
using (FileStream source1 = File.Open(dataDir + "test.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (FileStream source2 = File.Open(dataDir + "test2.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var archive = new Archive()) | |
{ | |
archive.CreateEntry("test.txt", source1); | |
archive.CreateEntry("test1.txt", source2); | |
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII, ArchiveComment = "There are two poems from Canterbury corpus" }); | |
} | |
} | |
} | |
} |
Create ZIP for Folder in C#
Sometimes you need to package an entire folder, including all the files in it into a ZIP file. The steps below summarize how to create a ZIP file for a folder in C#:
- Create a FileStream class object and specify the output ZIP file name.
- Create an object of the Archive class.
- Set the path to the folder.
- Create and export the output ZIP archive.
The following code sample demonstrates how to create a ZIP archive for a folder in C#:
using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Create)) | |
{ | |
using (Archive archive = new Archive()) | |
{ | |
DirectoryInfo corpus = new DirectoryInfo(dataDir + "zipfolder"); | |
archive.CreateEntries(corpus); | |
archive.Save(zipFile); | |
} | |
} |
Get a Free Evaluation License
You may request a free temporary license to evaluate the API to its full capacity without any restrictions.
Conholdate.Total for .NET - Learning Resources
You can go through the following resource to further explore the features offered by the API.
Summing Up
Creating ZIP files in C# opens up a world of possibilities for efficient data management and sharing. Learning the details shared in this blog post, you can easily add file compression capabilities to your applications. It will enable you to save space, transmit data easily, and streamline your workflows. In case of any queries, please feel free to write to us at the free support forum.