Provide a simple method to detect that a password for a ZIP file is not valid.
The current implemention usually throws an exception (which does not definitively identify the password as wrong), but not always. Sometimes it just returns corrupt data.
It would be nice to either have a typed exception consistently thrown, or a method to specifically check for an invalid password.
Thanks.
Steps to reproduce:
1.Create a zip archive for a txt file with password protection
2. Try updating the protected file and insert a new text line for example.
As a result, an error occurs: System.ObjectDisposedException: 'Cannot access a closed Stream.'
Imports System.IO
Imports System.Text
Imports Telerik.Windows.Zip
Module Module1
Sub Main()
Dim sZipFilePath As String = "..\..\test.zip"
File.Delete(sZipFilePath)
CreateArchive(sZipFilePath)
Dim decryptionSettings As DecryptionSettings = EncryptionSettings.CreateDecryptionSettings()
AddHandler decryptionSettings.PasswordRequired, AddressOf DecryptionSettings_PasswordRequired
Dim compressionSettings As CompressionSettings = Nothing
Dim encoding As Encoding = Nothing
Using oFS As FileStream = File.Open(sZipFilePath, FileMode.OpenOrCreate)
Using oArchive As ZipArchive = ZipArchive.Update(oFS, encoding, compressionSettings, decryptionSettings)
For Each entry As ZipArchiveEntry In oArchive.Entries
Using entryStream As Stream = entry.Open()
Dim reader As New StreamReader(entryStream)
Dim content As String = reader.ReadToEnd()
entryStream.Seek(0, SeekOrigin.End)
Dim writer As New StreamWriter(entryStream)
writer.WriteLine("Updated line.")
writer.Flush()
End Using
Next
End Using
End Using
End Sub
Private Sub CreateArchive(sZipFilePath As String)
Using stream As Stream = File.Open(sZipFilePath, FileMode.Create)
Dim encryptionSettings As PasswordEncryptionSettings = encryptionSettings.CreatePkzipPasswordEncryptionSettings()
encryptionSettings.Password = "MyPassword"
Dim compressionSettings As CompressionSettings = Nothing
Dim encoding As Encoding = Nothing
Using archive As ZipArchive = ZipArchive.Create(stream, encoding, compressionSettings, encryptionSettings)
Using entry As ZipArchiveEntry = archive.CreateEntry("text.txt")
Dim writer As StreamWriter = New StreamWriter(entry.Open())
writer.WriteLine("Hello world!")
writer.Flush()
End Using
End Using
End Using
End Sub
Private Sub DecryptionSettings_PasswordRequired(ByVal sender As Object, ByVal e As PasswordRequiredEventArgs)
e.Password = "MyPassword"
End Sub
End Module