How to protect zip file items with different passwords using C#?

16 Sep 20243 minutes to read

Syncfusion.Compression allows users to set a unique password for each item when creating a compressed Zip file. When the zip file is decompressed, the user can enter the password for each zip item and extract the zip items.

The following complete code snippet explains how to protect a zip file and its items with passwords using the ZipCrypto encryption algorithm.

using Syncfusion.Compression;
using Syncfusion.Compression.Zip;
class Program
{
  static void Main(string[] args)
  {
        //Initialize ZipArchive
        ZipArchive zipArchive = new ZipArchive();
        zipArchive.DefaultCompressionLevel = CompressionLevel.Best;

        //Add the file without password you want to zip.
        zipArchive.AddFile(@"../../Data/InputTemplate1.xlsx");

        //Add the file with password you want to zip
        zipArchive.AddFile(@"../../Data/InputTemplate2.xlsx", "password2");
        zipArchive.AddFile(@"../../Data/InputTemplate3.xlsx", "password3");

        //Protect the ZipArchive with password
        zipArchive.Protect("password", EncryptionAlgorithm.ZipCrypto);

        //Save the ZipArchive
        zipArchive.Save(@"../../Output/Output.zip");
        zipArchive.Close();
    }
}
Imports Syncfusion.Compression
Imports Syncfusion.Compression.Zip

Module Program
    Sub Main(args As String())
        'Initialize ZipArchive
        Dim zipArchive As New ZipArchive()
        zipArchive.DefaultCompressionLevel = CompressionLevel.Best

        'Add the file without password you want to zip.
        zipArchive.AddFile("../../Data/InputTemplate1.xlsx")

        'Add the files with password you want to zip.
        zipArchive.AddFile("../../Data/InputTemplate2.xlsx", "password2")
        zipArchive.AddFile("../../Data/InputTemplate3.xlsx", "password3")

        'Protect the ZipArchive with password
        zipArchive.Protect("password", EncryptionAlgorithm.ZipCrypto)

        'Save the ZipArchive
        zipArchive.Save("../../Output/Output.zip")
        zipArchive.Close()
    End Sub
End Module

See Also