Create ZIP in C#

The ZIP archives are used to compress and keep one or more files or folders into a single container. A ZIP archive encapsulates the files and folders as well as holds their metadata information. The most common usage of archiving is reducing the size of the files for storage or transmission and applying encryption for security. Apart from the file compression tools, the automated compression/extraction features are also used within various desktop and web applications for uploading, downloading, sharing, or encrypting the files. This article also targets similar scenarios and presents some easy ways of compressing files or folders and creating ZIP archives programmatically using C#.

In this article, you will learn how to perform the following ZIP archiving operations:

C# ZIP Library

Aspose.ZIP for .NET is a powerful and easy to use API for zipping or unzipping files and folders within .NET applications. It also provides AES encryption techniques to encrypt the files in ZIP in C#. You can install the API from NuGet or download its binaries from the Downloads section.

Create a ZIP Archive in C#

The following are the steps to compress a file by adding it into a ZIP archive:

The following code sample shows how to add a file into a ZIP archive using C#.

Add Multiple Files into ZIP Archive in C#

In case you want to add multiple files into a ZIP archive, you can do it using one of the following ways.

C# ZIP Multiple Files using FileStream

In this method, the FileStream class is used to add files to the ZIP archive using Archive.CreateEntry(String, FileStream) method. The following code sample shows how to add multiple files into a ZIP in C#.

C# ZIP Multiple Files using FileInfo

You can also use the FileInfo class for adding multiple files into a ZIP archive. In this method, the files will be loaded using the FileInfo class and added to the ZIP archive using Archive.CreateEntry(String, FileInfo) method. The following code sample shows how to ZIP multiple files using the FileInfo class in C#.

ZIP Files using Path

Instead of using FileInfo or FileStream classes for the ZIP entries, you can provide the file’s path directly to Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. The following code sample shows how to ZIP files using their path.

Add Folders to a ZIP Archive in C#

You can ZIP a folder with C# ZIP library, which could be another alternative of adding multiple files to a ZIP archive. Just put the source files into a folder and add that folder to the ZIP archive. The following are the steps to ZIP a folder in C#:

The following code sample shows how to add a folder to ZIP in C#.

Create a Password Protected ZIP using ZipCrypto in C#

You can protect the ZIP archives using passwords and apply ZipCrypto encryption. For this, the ArchiveEntrySettings class is used in the constructor of the Archive that accepts the encryption type as the second parameter.

The following code sample shows how to create a password-protected ZIP archive using ZipCrypto in C#.

C# Create Password Protected ZIP with AES Encryption

Aspose.ZIP for .NET also lets you apply AES encryption to protect the ZIP archives. You can use the following AES encryption methods:

  • AES128
  • AES192
  • AES256

In order to apply AES encryption, API offers the AesEcryptionSettings class. The following code sample shows how to create a password protected ZIP with AES encryption in C#.

Set Parallel Compression Mode

You can also configure the API for parallel compression in case of multiple entries. For this, you can specify the parallel compression mode using ParallelOptions class. Aspose.ZIP for .NET provides the following parallel compression modes.

  • Never - Do not compress in parallel.
  • Always - Do compress in parallel (beware out of memory).
  • Auto - Decide whether to use parallel compression or not upon the entries. This option may compress some entries in parallel only.

The following code sample shows how to set parallel compression mode while zipping multiple files with Aspose’s C# ZIP library.

Learn More About C# .NET ZIP Library

Explore more about our C# ZIP API using the following resources:

Make ZIP Archives Online

You can also try our free online application, which is based on Aspose.ZIP for .NET, to compress files into ZIP archives.

Conclusion

In this article, you have learned how to create ZIP archives programmatically in C#. The code samples have demonstrated how to add files and folders in the ZIP archives. In addition, we have also covered how to create password-protected ZIP archives with ZipCrypto and AES encryption methods using C# Aspose’ ZIP library. Alongside, parallel compression of multiple entries is also discussed at the end. In case you would have any questions or queries, you can ask us via our forum.

See also

Unrar or Extract Files using C#Unzip Files in ZIP Archives using C#Create 7z (7-Zip) Archives in C# .NET
Open or Extract 7z (7zip) File in C# .NETCreate and Extract GZip Archives using C#Convert RAR Files to ZIP Archive in C#
Convert ZIP Archives to TAR in C#Create a Flat ZIP Archive in C#Create Executable Self-extracting Archive in C#
Create TAR.GZ and TAR.XZ Files in C#Delete Files in a ZIP Archive in C#Extract Nested ZIP Archives in C#
Merge Multiple ZIP or TAR Archives in C#How to Create ZIP FilesExtract 7z Online