Password Protect ZIP

Password-protecting a ZIP archive involves adding a password to the archive to secure its contents. This ensures that only individuals with the correct password can extract or access the files within the ZIP archive. This password acts as a form of encryption, preventing unauthorized access to the files stored in the archive. To extract files from a password-protected ZIP archive, users must enter the correct password.

Here's how you can create or open a ZIP file and apply password encryption to it:

using IronZip;
using IronZip.Enum;
using System;

class CreateEncryptedZip
{
    static void Main()
    {
        string[] filesToInclude = { "file1.txt", "file2.txt" };
        string outputZip = "my_secure_archive.zip";
        string password = "secretpassword";

        // Compression level: 9 = highest (optional)
        using (var archive = new IronZipArchive(9))
        {
            // Apply password protection using Traditional or AES encryption
            archive.Encrypt(password, EncryptionMethods.AES256);

            // Add each file
            foreach (var file in filesToInclude)
            {
                archive.Add(file);
            }

            // Save the encrypted ZIP
            archive.SaveAs(outputZip);
        }

        Console.WriteLine($"Encrypted ZIP saved as '{outputZip}'");
    }
}
using IronZip;
using IronZip.Enum;
using System;

class CreateEncryptedZip
{
    static void Main()
    {
        string[] filesToInclude = { "file1.txt", "file2.txt" };
        string outputZip = "my_secure_archive.zip";
        string password = "secretpassword";

        // Compression level: 9 = highest (optional)
        using (var archive = new IronZipArchive(9))
        {
            // Apply password protection using Traditional or AES encryption
            archive.Encrypt(password, EncryptionMethods.AES256);

            // Add each file
            foreach (var file in filesToInclude)
            {
                archive.Add(file);
            }

            // Save the encrypted ZIP
            archive.SaveAs(outputZip);
        }

        Console.WriteLine($"Encrypted ZIP saved as '{outputZip}'");
    }
}
Imports IronZip
Imports IronZip.Enum
Imports System

Friend Class CreateEncryptedZip
	Shared Sub Main()
		Dim filesToInclude() As String = { "file1.txt", "file2.txt" }
		Dim outputZip As String = "my_secure_archive.zip"
		Dim password As String = "secretpassword"

		' Compression level: 9 = highest (optional)
		Using archive = New IronZipArchive(9)
			' Apply password protection using Traditional or AES encryption
			archive.Encrypt(password, EncryptionMethods.AES256)

			' Add each file
			For Each file In filesToInclude
				archive.Add(file)
			Next file

			' Save the encrypted ZIP
			archive.SaveAs(outputZip)
		End Using

		Console.WriteLine($"Encrypted ZIP saved as '{outputZip}'")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation

  • Creating the archive:
    new IronZipArchive(9) initializes the ZIP with highest compression level (0‑9)
  • Applying encryption:
    archive.Encrypt(password, EncryptionMethods.AES256) configures password protection. Supports Traditional, AES128, and AES256 encryption methods
  • Adding files:
    archive.Add(filePath) includes individual files into the archive
  • Saving: archive.SaveAs(outputZip) writes out the encrypted ZIP file—further additions aren’t allowed afterward.
  • Resource cleanup:
    Using the using statement ensures proper disposal of resources via Dispose(), releasing internal buffers and handles

Accessing an Existing Password‑Protected ZIP

using IronZip;
using IronZip.Enum;
using System;

class ModifyEncryptedZip
{
    static void Main()
    {
        string existingZip = "my_secure_archive.zip";
        string password = "secretpassword";

        using var archive = new IronZipArchive(existingZip, password);
        Console.WriteLine($"Archive entries loaded: {archive.Count}");

        // Example: change encryption method on same archive
        archive.Encrypt(password, EncryptionMethods.AES128);
        archive.Save();

        Console.WriteLine("Encryption method updated to AES128");
    }
}
using IronZip;
using IronZip.Enum;
using System;

class ModifyEncryptedZip
{
    static void Main()
    {
        string existingZip = "my_secure_archive.zip";
        string password = "secretpassword";

        using var archive = new IronZipArchive(existingZip, password);
        Console.WriteLine($"Archive entries loaded: {archive.Count}");

        // Example: change encryption method on same archive
        archive.Encrypt(password, EncryptionMethods.AES128);
        archive.Save();

        Console.WriteLine("Encryption method updated to AES128");
    }
}
Imports IronZip
Imports IronZip.Enum
Imports System

Friend Class ModifyEncryptedZip
	Shared Sub Main()
		Dim existingZip As String = "my_secure_archive.zip"
		Dim password As String = "secretpassword"

		Dim archive = New IronZipArchive(existingZip, password)
		Console.WriteLine($"Archive entries loaded: {archive.Count}")

		' Example: change encryption method on same archive
		archive.Encrypt(password, EncryptionMethods.AES128)
		archive.Save()

		Console.WriteLine("Encryption method updated to AES128")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • Opening the archive via constructor with (path, password) decrypts it.
  • Changing encryption is supported via Encrypt(...) and persisting with Save()

Extracting a Password‑Protected ZIP

using IronZip;
using System;

class ExtractProtectedZip
{
    static void Main()
    {
        string zipPath = "my_secure_archive.zip";
        string extractTo = "myExtractedFiles";
        string password = "secretpassword";

        IronZipArchive.ExtractArchiveToDirectory(zipPath, extractTo, password);

        Console.WriteLine($"Extracted to '{extractTo}'");
    }
}
using IronZip;
using System;

class ExtractProtectedZip
{
    static void Main()
    {
        string zipPath = "my_secure_archive.zip";
        string extractTo = "myExtractedFiles";
        string password = "secretpassword";

        IronZipArchive.ExtractArchiveToDirectory(zipPath, extractTo, password);

        Console.WriteLine($"Extracted to '{extractTo}'");
    }
}
Imports IronZip
Imports System

Friend Class ExtractProtectedZip
	Shared Sub Main()
		Dim zipPath As String = "my_secure_archive.zip"
		Dim extractTo As String = "myExtractedFiles"
		Dim password As String = "secretpassword"

		IronZipArchive.ExtractArchiveToDirectory(zipPath, extractTo, password)

		Console.WriteLine($"Extracted to '{extractTo}'")
	End Sub
End Class
$vbLabelText   $csharpLabel

ExtractArchiveToDirectory(zipPath, extractDir, password) handles password-authenticated extraction seamlessly.