Password Property

DotNetZip

Ionic Zip Library v1.9.1.6 Password Property
ReferenceIonic.ZipZipEntryPassword
The Password to be used when encrypting a ZipEntry upon ZipFile.Save(), or when decrypting an entry upon Extract().
Declaration Syntax
C# Visual Basic Visual C++
public string Password { private get; set; }
Public Property Password As String
	Private Get
	Set
public:
property String^ Password {
	private: String^ get ();
	void set (String^ value);
}
Remarks

This is a write-only property on the entry. Set this to request that the entry be encrypted when writing the zip archive, or set it to specify the password to be used when extracting an existing entry that is encrypted.

The password set here is implicitly used to encrypt the entry during the Save()()()() operation, or to decrypt during the Extract()()()() or OpenReader()()()() operation. If you set the Password on a ZipEntry after calling Save(), there is no effect.

Consider setting the Encryption property when using a password. Answering concerns that the standard password protection supported by all zip tools is weak, WinZip has extended the ZIP specification with a way to use AES Encryption to protect entries in the Zip file. Unlike the "PKZIP 2.0" encryption specified in the PKZIP specification, AES Encryption uses a standard, strong, tested, encryption algorithm. DotNetZip can create zip archives that use WinZip-compatible AES encryption, if you set the Encryption property. But, archives created that use AES encryption may not be readable by all other tools and libraries. For example, Windows Explorer cannot read a "compressed folder" (a zip file) that uses AES encryption, though it can read a zip file that uses "PKZIP encryption."

The ZipFile class also has a Password property. This property takes precedence over any password set on the ZipFile itself. Typically, you would use the per-entry Password when most entries in the zip archive use one password, and a few entries use a different password. If all entries in the zip file use the same password, then it is simpler to just set this property on the ZipFile itself, whether creating a zip archive or extracting a zip archive.

Some comments on updating archives: If you read a ZipFile, you cannot modify the password on any encrypted entry, except by extracting the entry with the original password (if any), removing the original entry via RemoveEntry(ZipEntry), and then adding a new entry with a new Password.

For example, suppose you read a ZipFile, and there is an encrypted entry. Setting the Password property on that ZipEntry and then calling Save() on the ZipFile does not update the password on that entry in the archive. Neither is an exception thrown. Instead, what happens during the Save() is the existing entry is copied through to the new zip archive, in its original encrypted form. Upon re-reading that archive, the entry can be decrypted with its original password.

If you read a ZipFile, and there is an un-encrypted entry, you can set the Password on the entry and then call Save() on the ZipFile, and get encryption on that entry.

Examples

This example creates a zip file with two entries, and then extracts the entries from the zip file. When creating the zip file, the two files are added to the zip file using password protection. Each entry uses a different password. During extraction, each file is extracted with the appropriate password.

CopyC#
// create a file with encryption
using (ZipFile zip = new ZipFile())
{
    ZipEntry entry;
    entry= zip.AddFile("Declaration.txt");
    entry.Password= "123456!";
    entry = zip.AddFile("Report.xls");
    entry.Password= "1Secret!";
    zip.Save("EncryptedArchive.zip");
}

// extract entries that use encryption
using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip"))
{
    ZipEntry entry;
    entry = zip["Declaration.txt"];
    entry.Password = "123456!";
    entry.Extract("extractDir");
    entry = zip["Report.xls"];
    entry.Password = "1Secret!";
    entry.Extract("extractDir");
}
CopyVB.NET
Using zip As New ZipFile
    Dim entry as ZipEntry
    entry= zip.AddFile("Declaration.txt")
    entry.Password= "123456!"
    entry = zip.AddFile("Report.xls")
    entry.Password= "1Secret!"
    zip.Save("EncryptedArchive.zip")
End Using


' extract entries that use encryption
Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip"))
    Dim entry as ZipEntry
    entry = zip("Declaration.txt")
    entry.Password = "123456!"
    entry.Extract("extractDir")
    entry = zip("Report.xls")
    entry.Password = "1Secret!"
    entry.Extract("extractDir")
End Using

Assembly: Ionic.Zip (Module: Ionic.Zip) Version: 1.9.1.8 (1.9.1.8)