add-files-or-folders-to-zip-archives-using-csharp

ZIP files are the most common archive format for compressing files and folders into a single container. C# developers can easily create encrypted or password‑protected ZIP archives programmatically in .NET. In this article, you will learn how to create encrypted ZIP files using C#.

The following topics are covered:

C# API to Create Encrypted ZIP Files

To create encrypted ZIP archives, we use Aspose.ZIP for .NET API. It compresses files and folders, adds them to ZIP archives, and can decompress formats such as ZIP, TAR, GZIP, BZ2, 7Zip, RAR, etc. The API supports password protection and AES encryption (AES128, AES192, AES256).

You can either download the DLL or install it via NuGet.

Install-Package Aspose.ZIP

Create Password-Protected ZIP Files

Follow these steps to create a password‑protected ZIP archive:

  1. Create an Archive instance with an ArchiveEntrySettings object.
  2. Set the password using the TraditionalEncryptionSettings object.
  3. Call CreatEntry() with the input file path to add a file.
  4. Repeat step 3 for additional files.
  5. Call _Save() with the output path to write the archive.

The code sample below demonstrates how to create a password‑protected ZIP file using C#.

Create Password-Protected ZIP Archives

Create Password-Protected ZIP Archives

The Archive class represents a ZIP file and provides methods to create, compose, extract, or update archives. CreatEntry() adds a single file entry; overloads allow adding from a stream or FileInfo. Save() writes the archive to disk.

ArchiveEntrySettings defines compression options. TraditionalEncryptionSettings configures the legacy ZipCrypto algorithm. Its Password property sets the password for encryption or decryption.

Create Encrypted ZIP Files with AES Encryption

Use these steps to encrypt a ZIP archive with AES:

  1. Create an Archive instance with an ArchiveEntrySettings object.
  2. Set the password using AesEncryptionSettings, passing the password string and an _EncryptionMethod_.
  3. Call CreatEntry() with the input file path.
  4. Repeat step 3 for additional files.
  5. Call _Save() with the output path.

The sample below shows how to create an AES‑encrypted ZIP file using C#.

AesEncryptionSettings configures the AES algorithm, a symmetric block cipher. Available encryption methods are:

  • Traditional — legacy PKWARE encryption
  • AES128 — 128‑bit key
  • AES192 — 192‑bit key
  • AES256 — 256‑bit key

Encrypt Folders in ZIP Files

Add password‑protected folders with these steps:

  1. Create an Archive instance with an ArchiveEntrySettings object.
  2. Set the password using TraditionalEncryptionSettings.
  3. Call CreatEntries() with the folder path.
  4. Repeat step 3 for additional folders.
  5. Call _Save() with the output path.

The code sample below demonstrates how to add an encrypted folder to a ZIP file using C#.

Encrypt Specific Files in ZIP Archives

Encrypt individual files with these steps:

  1. Create an Archive instance.
  2. Call CreatEntry() with the first file path.
  3. Set a password for that entry using ArchiveEntrySettings with TraditionalEncryptionSettings.
  4. Call CreatEntry() for the next file.
  5. Repeat as needed.
  6. Call _Save() with the output path.

The sample below shows how to encrypt specific files in a ZIP archive using C#.

Encrypt Specific Files in ZIP Archives

Encrypt Specific Files in ZIP archives

Create Encrypted ZIP Files with Mixed Encryption

Create archives that mix encryption types using these steps:

  1. Create an Archive instance.
  2. Call CreatEntry() with the first file path.
  3. Set its password using ArchiveEntrySettings with AesEncryptionSettings.
  4. Call CreatEntry() for a second file.
  5. Set its password using ArchiveEntrySettings with TraditionalEncryptionSettings.
  6. Call CreatEntry() for a third file.
  7. Call CreateEntries() with a folder path.
  8. Save the archive using _Save() with the output path.

The code sample below illustrates how to create a ZIP file with mixed encryption techniques using C#.

Create Encrypted ZIP Archives with Mixed Encryption

Create Encrypted ZIP File with Mixed Encryption

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you learned how to create encrypted ZIP files using C#. You also saw how to create password‑protected ZIP files, how to encrypt specific files, how to add password‑protected folders, and how to build ZIP archives with mixed encryption techniques. Explore more about Aspose.ZIP for .NET in the documentation. For questions, visit the forum.

See Also