Encrypt existing ZIP
Encrypting Existing ZIP Archives with Aspose.Zip for .NET
Aspose.Zip for .NET isn’t just for compressing files or composing new archives — it also provides tools to secure existing ZIP archives by applying encryption, without modifying their contents or repacking. This is crucial for scenarios where you want to enhance security without overhead or risk of data loss.
When composing an encrypted ZIP archive, compression is performed first, and then the compressed data is encrypted. If you want to encrypt the regular ZIP archive, you do not need to decompress it first — it is sufficient to simply apply encryption to the already compressed data.
How to Encrypt an Existing ZIP Archive
The API provides the
EncryptionOptions property on ArchiveSaveOptions for this encryption scenario. You simply:
- Open the unencrypted
.zip - Call Save() on the archive, specifying a new filename and an ArchiveSaveOptions with EncryptionOptions set to your preferred encryption details
Below is a practical example in C# that encrypts an existing ZIP file with AES-256:
1string sourceZip = "plain.zip";
2string encryptedZip = "encrypted.zip";
3string password = "p@s$";
4
5using (var archive = new Archive(sourceZip))
6{
7 archive.Save(encryptedZip, new ArchiveSaveOptions
8 {
9 EncryptionOptions = new AesEcryptionSettings(password, EncryptionMethod.AES256)
10 });
11}Notes:
- This does not decompress or repack the archive; the process simply applies encryption.
- If you want to encrypt a newly created archive, use the EncryptionSettings property for each entry, not EncryptionOptions.
- Do not combine DataDescriptorPolicy.ForAllFileEntries with EncryptionOptions — it’s unsupported.
With Aspose.Zip for .NET, you can upgrade the security of existing ZIP archives in-place — just open, save, and set the proper encryption settings, all with minimal code and no repacking required.