Back to Devexpress

How to: Password Protect Archive Files

officefileapi-15243-zip-compression-and-archive-api-examples-how-to-password-protect-archive-files.md

latest2.0 KB
Original Source

How to: Password Protect Archive Files

  • May 10, 2024

To encrypt and password protect files in an archive, do the following:

  1. Create a ZipArchive class instance.
  2. Call its ZipArchive.AddFile method for each selected file. The method returns an object of the ZipFileItem type.
  3. Specify the ZipArchive.Password and ZipItem.EncryptionType properties for each item.
  4. Call the ZipArchive.Save method to create an archive and save it to a specified location.

View Example

csharp
using DevExpress.Compression;

public void ProtectPassword() {
    string[] sourceFiles = this.sourceFiles;
    string password = "123";
    using (ZipArchive archive = new ZipArchive()) {
        foreach (string file in sourceFiles) {
            ZipFileItem zipFI = archive.AddFile(file, "/");
            zipFI.EncryptionType = EncryptionType.Aes128;
            zipFI.Password = password;
        }
        archive.Save("ProtectPassword.zip");
    }
}
vb
Imports DevExpress.Compression

Public Sub ProtectPassword()
    Dim sourceFiles() As String = Me.sourceFiles
    Dim password As String = "123"
    Using archive As New ZipArchive()
        For Each file As String In Me.sourceFiles
            Dim zipFI As ZipFileItem = archive.AddFile(file, "/")
            zipFI.EncryptionType = EncryptionType.Aes128
            zipFI.Password = password
        Next file
        archive.Save("ProtectPassword.zip")
    End Using
End Sub