C# | Visual Basic | Visual C++ |
public EncryptionAlgorithm Encryption { get; set; }
Public Property Encryption As EncryptionAlgorithm Get Set
public: property EncryptionAlgorithm Encryption { EncryptionAlgorithm get (); void set (EncryptionAlgorithm value); }
Set this when creating a zip archive, or when updating a zip archive. The specified Encryption is applied to the entries subsequently added to the ZipFile instance. Applications do not need to set the Encryption property when reading or extracting a zip archive.
If you set this to something other than EncryptionAlgorithm.None, you will also need to set the Password.
As with some other properties on the ZipFile class, like Password and CompressionLevel, setting this property on a ZipFile instance will cause the specified EncryptionAlgorithm to be used on all ZipEntry items that are subsequently added to the ZipFile instance. In other words, if you set this property after you have added items to the ZipFile, but before you have called Save(), those items will not be encrypted or protected with a password in the resulting zip archive. To get a zip archive with encrypted entries, set this property, along with the Password property, before calling AddFile, AddItem, or AddDirectory (etc.) on the ZipFile instance.
If you read a ZipFile, you can modify the Encryption on an encrypted entry, only by setting the Encryption property on the ZipEntry itself. Setting the Encryption property on the ZipFile, once it has been created via a call to ZipFile.Read(), does not affect entries that were previously read.
For example, suppose you read a ZipFile, and there is an encrypted entry. Setting the Encryption property on that ZipFile and then calling Save() on the ZipFile does not update the Encryption used for the entries in the archive. Neither is an exception thrown. Instead, what happens during the Save() is that all previously existing entries are copied through to the new zip archive, with whatever encryption and password that was used when originally creating the zip archive. Upon re-reading that archive, to extract entries, applications should use the original password or passwords, if any.
Suppose an application reads a ZipFile, and there is an encrypted entry. Setting the Encryption property on that ZipFile and then adding new entries (via AddFile(), AddEntry(), etc) and then calling Save() on the ZipFile does not update the Encryption on any of the entries that had previously been in the ZipFile. The Encryption property applies only to the newly-added entries.
This example creates a zip archive that uses encryption, and then extracts entries from the archive. When creating the zip archive, the ReadMe.txt file is zipped without using a password or encryption. The other files use encryption.
// Create a zip archive with AES Encryption. using (ZipFile zip = new ZipFile()) { zip.AddFile("ReadMe.txt"); zip.Encryption= EncryptionAlgorithm.WinZipAes256; zip.Password= "Top.Secret.No.Peeking!"; zip.AddFile("7440-N49th.png"); zip.AddFile("2008-Regional-Sales-Report.pdf"); zip.Save("EncryptedArchive.zip"); } // Extract a zip archive that uses AES Encryption. // You do not need to specify the algorithm during extraction. using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip")) { zip.Password= "Top.Secret.No.Peeking!"; zip.ExtractAll("extractDirectory"); }
' Create a zip that uses Encryption. Using zip As New ZipFile() zip.Encryption= EncryptionAlgorithm.WinZipAes256 zip.Password= "Top.Secret.No.Peeking!" zip.AddFile("ReadMe.txt") zip.AddFile("7440-N49th.png") zip.AddFile("2008-Regional-Sales-Report.pdf") zip.Save("EncryptedArchive.zip") End Using ' Extract a zip archive that uses AES Encryption. ' You do not need to specify the algorithm during extraction. Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip")) zip.Password= "Top.Secret.No.Peeking!" zip.ExtractAll("extractDirectory") End Using