How to Lock Zip Archive Using C#

For protecting a zip archive, we’ll use

Aspose.ZIP for .NET

API which is a feature-rich, powerful and easy to use API. It allows to compress & decompress ZIP, TAR, GZIP, BZ2 file formats on .NET platform without installing any software like WinRAR or 7ZIP. Moreover, developers can convert archive to another format. To add the reference, Open

NuGet

package manager, search for Aspose.ZIP and install. You may also use the following command from the Package Manager Console Install-Package Aspose.ZIP .

Steps to Protect Zip Archive in C#

Protecting archive using password with

Aspose.ZIP for .NET

APIs can be done with just few lines of code.

  • Create FileStream object with the output ZIP archive file path as parameter with mod FileMode.Create.
  • Open and read the source file into a FileStream object with mod FileMode.Open and FileAccess.Read.
  • Create Archive class object with ArchiveEntrySettings object as parameter.
  • Use TraditionalEncryptionSettings as parameters to ArchiveEntrySettings.
  • Use Archive.Save(FileStream) method for creating the archive.

Password Protection Requirements

Before executing the code below, please make sure that you have the following prerequisites on your system.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core
  • Development environment like Microsoft Visual Studio
  • Aspose.ZIP for .NET DLL referenced in your project
  • Add namespace in relevant class file
 

Lock ZIP Archive - C# code example

 

Protected ZIP Archiving Operations

Aspose.Zip also provides some other ways of locking ZIP archives with AES Encryption AES128, AES192 and AES 256. Even developers can encrypt multiple files with mixed encryption techniques.