Password Property

DotNetZip

Ionic Zip Library v1.9.1.6 Password Property
ReferenceIonic.ZipZipFilePassword
Sets the password to be used on the ZipFile instance.
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

When writing a zip archive, this password is applied to the entries, not to the zip archive itself. It applies to any ZipEntry subsequently added to the ZipFile, using one of the AddFile, AddDirectory, AddEntry, or AddItem methods, etc. When reading a zip archive, this property applies to any entry subsequently extracted from the ZipFile using one of the Extract methods on the ZipFile class.

When writing a zip archive, keep this in mind: though the password is set on the ZipFile object, according to the Zip spec, the "directory" of the archive - in other words the list of entries or files contained in the archive - is not encrypted with the password, or protected in any way. If you set the Password property, the password actually applies to individual entries that are added to the archive, subsequent to the setting of this property. The list of filenames in the archive that is eventually created will appear in clear text, but the contents of the individual files are encrypted. This is how Zip encryption works.

One simple way around this limitation is to simply double-wrap sensitive filenames: Store the files in a zip file, and then store that zip file within a second, "outer" zip file. If you apply a password to the outer zip file, then readers will be able to see that the outer zip file contains an inner zip file. But readers will not be able to read the directory or file list of the inner zip file.

If you set the password on the ZipFile, and then add a set of files to the archive, then each entry is encrypted with that password. You may also want to change the password between adding different entries. If you set the password, add an entry, then set the password to null (Nothing in VB), and add another entry, the first entry is encrypted and the second is not. If you call AddFile(), then set the Password property, then call ZipFile.Save, the file added will not be password-protected, and no warning will be generated.

When setting the Password, you may also want to explicitly set the Encryption property, to specify how to encrypt the entries added to the ZipFile. If you set the Password to a non-null value and do not set Encryption, then PKZip 2.0 ("Weak") encryption is used. This encryption is relatively weak but is very interoperable. If you set the password to a null value (Nothing in VB), Encryption is reset to None.

All of the preceding applies to writing zip archives, in other words when you use one of the Save methods. To use this property when reading or an existing ZipFile, do the following: set the Password property on the ZipFile, then call one of the Extract() overloads on the ZipEntry. In this case, the entry is extracted using the Password that is specified on the ZipFile instance. If you have not set the Password property, then the password is null, and the entry is extracted with no password.

If you set the Password property on the ZipFile, then call Extract() an entry that has not been encrypted with a password, the password is not used for that entry, and the ZipEntry is extracted as normal. In other words, the password is used only if necessary.

The ZipEntry class also has a Password property. It takes precedence over this property on the ZipFile. 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.

Examples

This example creates a zip file, using password protection for the entries, and then extracts the entries from the zip file. When creating the zip file, the Readme.txt file is not protected with a password, but the other two are password-protected as they are saved. During extraction, each file is extracted with the appropriate password.

CopyC#
// create a file with encryption
using (ZipFile zip = new ZipFile())
{
    zip.AddFile("ReadMe.txt");
    zip.Password= "!Secret1";
    zip.AddFile("MapToTheSite-7440-N49th.png");
    zip.AddFile("2008-Regional-Sales-Report.pdf");
    zip.Save("EncryptedArchive.zip");
}

// extract entries that use encryption
using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip"))
{
    zip.Password= "!Secret1";
    zip.ExtractAll("extractDir");
}
CopyVB.NET
Using zip As New ZipFile
    zip.AddFile("ReadMe.txt")
    zip.Password = "123456!"
    zip.AddFile("MapToTheSite-7440-N49th.png")
    zip.Password= "!Secret1";
    zip.AddFile("2008-Regional-Sales-Report.pdf")
    zip.Save("EncryptedArchive.zip")
End Using


' extract entries that use encryption
Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip"))
    zip.Password= "!Secret1"
    zip.ExtractAll("extractDir")
End Using

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