This brief tutorial covers how to create a ZIP file in C#. It contains all the details including the algorithm and the code snippet to develop a ZIP file maker in C#. Furthermore, you can follow this approach in any operating system where the .NET framework is installed.
Steps to Create ZIP in C#
- Configure Aspose.ZIP using the NuGet package manager to create a ZIP archive
- Create a FileStream class object to save the output ZIP archive
- Open the source files that need to be added to the output archive
- Add the loaded files to the ZIP directory with the CreateEntry method
- Write the generated ZIP file by calling the Save method
These steps reveal the workflow to create ZIP in C#. They include the configuration details and then provide the C# code snippet to work with this feature in your environment. Create the output file stream, append files to the ZIP archive, and finally, save the output directory.
Code to Create ZIP File Maker in C#
using System.Text; | |
using Aspose.Zip; | |
using Aspose.Zip.Saving; | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
// Create FileStream for output ZIP archive | |
using (FileStream zipFile = File.Open("csv_archive.zip", FileMode.Create)) | |
{ | |
// File to be added to archive | |
using (FileStream source1 = File.Open("ClientData.xml", FileMode.Open, FileAccess.Read)) | |
{ | |
// File to be added to archive | |
using (FileStream source2 = File.Open("TextBox_out.pdf", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var archive = new Archive()) | |
{ | |
// Add files to the archive | |
archive.CreateEntry("ClientData.xml", source1); | |
archive.CreateEntry("TextBox_out.pdf", source2); | |
// ZIP the files | |
archive.Save(zipFile, new ArchiveSaveOptions() | |
{ | |
Encoding = Encoding.ASCII, | |
ArchiveComment = | |
"Compressed Files" | |
}); | |
} | |
} | |
} | |
} |
This code snippet demonstrates how to make a ZIP file in C#. Moreover, this approach can be improvised by invoking the CreateEntry method as many times as you may need. You can also customize the ArchiveSaveOptions class properties, like SelfExtractingOptions, Encryption, ParallelOptions, etc., based on your requirements.
This tutorial explains the details of creating a ZIP maker in C#. However, if you want to learn to compress comma-separated files then read the article on Compress CSV in C#.