Add Files or Folders to ZIP Archives Programmatically in C#

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:

  • Create a ZIP archive using C#.
  • Add multiple files to a ZIP archive.
  • Add folders to a ZIP archive.
  • Create a password-protected ZIP archive.
  • Encrypt ZIP archive with AES encryption.

Prerequisite – 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 archives. 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.

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#.

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#.

Add Folders to a ZIP Archive in C#

You can ZIP a folder as well 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:

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

Create a Password-Protected ZIP Archive in C#

You can protect the ZIP archives using the password. In order to specify a password for the ZIP archive, the ArchiveEntrySettings class is used in the constructor of the Archive. The following code sample shows how to create a password-protected ZIP archive in C#.

Create AES Encrypted ZIP Archives in C#

Aspose.ZIP for .NET also lets you apply AES encryption to 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 an AES encrypted ZIP archive in C#.

Learn more about Aspose.ZIP for .NET

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

See also