保护ZipArchive
RadZipLibrary允许您使用密码保护ZIP存档。这篇帮助文章将教你如何使用RadZipLibrary以密码保护文件,以及如何打开有密码保护的文件。保护一个ZIP档案和所有ZipArchiveEntry项时,应在创建ZipArchive对象。
RadZipLibrary仅支持传统的PKWARE加密。类型表示此加密类型的设置DefaultEncryptionSettings类。
创建一个密码保护的ZipArchive
为了创建受密码保护的ZIP存档,您需要传递一个DefaultEncryptionSettings对象的ZipArchive的构造函数ZipArchiveMode。创建参数。
DefaultEncryptionSettings有一个密码属性,该属性表示使用的密码。
例1:创建一个受密码保护的ZIP归档文件
using (Stream Stream = File.Open("test.zip", FileMode.Create)) {DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings();encryptionSettings。密码= "password"; using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null, null, encryptionSettings)) { using (ZipArchiveEntry entry = archive.CreateEntry("text.txt")) { StreamWriter writer = new StreamWriter(entry.Open()); writer.WriteLine("Hello world!"); writer.Flush(); } } }
使用stream作为stream = File.Open("test.zip", FileMode.Create) Dim encryptionSettings作为New DefaultEncryptionSettings() encryptionSettings。密码= " Password "使用归档作为新的ZipArchive(流,ZipArchiveMode. zip。创建, False, Nothing, Nothing, encryptionSettings) Using entry As ZipArchiveEntry = archive.CreateEntry("text.txt") Dim writer As New StreamWriter(entry.Open()) writer.WriteLine("Hello world!") writer.Flush() End Using End Using End Using
当所有使用ZIP归档对象的操作都完成时,必须始终释放该ZIP归档对象。Telerik Support建议您在using语句中声明和实例化ZIP归档对象。如果由于某种原因不可能,那么不要忘记打电话给处理()方法。
阅读密码保护的ZipArchive
才能打开有密码保护的ZipArchive,你需要通过一个DefaultEncryptionSettings使用最初用于创建存档的密码初始化。
例2:打开并读取有密码保护的ZIP归档文件
using (Stream Stream = File.Open("test.zip", FileMode.Open)) {DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings();encryptionSettings。密码= "password"; using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, null, null, encryptionSettings)) { // Display the list of the files in the selected zip file using the ZipArchive.Entries property. } }
使用stream作为stream = File.Open("test.zip", FileMode.Open) Dim encryptionSettings作为New DefaultEncryptionSettings() encryptionSettings。密码= " Password "使用归档作为新的ZipArchive(流,ZipArchiveMode. zip。Read, False, Nothing, Nothing, encryptionSettings) '使用ZipArchive显示所选zip文件中的文件列表。条目属性。结束使用结束使用
当所有使用ZIP归档对象的操作都存在竞争时,必须始终释放该ZIP归档对象。Telerik Support建议您在using语句中声明和实例化ZIP归档对象。如果由于某种原因不可能,那么不要忘记打电话给处理()方法。