This project is read-only.

Detecting password on zip

Jun 18, 2010 at 7:14 PM

Hi Cheeso,

Is it possible to test a zip before extraction to see if it has a password?
I would like to prompt if it does and use this to extact the protected zip.


Jun 18, 2010 at 7:48 PM

The password is not on the zip file, but on the individual entries.  It's possible to have some entries with a password, some not.  It's also possible to have different passwords on different entries.

if you'd like to determine if there is any password required on any entry, you can examine the ZipEntry.UsesEncryption property, for each entry. But that wouldn't allow you to just prompt once for a password.

You'd still need to handle the case where the passwords across different entries differ, or the case where the user enters an incorrect password.

The way I do this in my apps is to handle the BadPasswordException within the call to ZipEntry.Extract().  When that happens, I prompt, and try again (up to a limited number of retries).


Jun 19, 2010 at 6:45 PM

I see, I've caught the exception you mentioned, works great. Thanks.

I've noticed when saving a zip it takes around ten seconds to start if I use "zip.Encryption = EncryptionAlgorithm.PkzipWeak" (or do not specify) Is this normal?

Jun 20, 2010 at 1:55 PM

About the 10-second latency, no I've never seen that or heard of that, so I'd say it's not normal.

Not sure what would cause that. 

To diagnose it, if you have Visual Studio you could use the profiler to examine where the time goes.  


Jun 22, 2010 at 9:58 AM

Hi Cheeso, I'm using Visual Studio 2008 Profressional - I believe only the team versions come with profiler?
I cannot see any reason why it has this delay - only with PKZipWeak?


    Private Sub zipUpFile(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        Dim extractCancelled As Boolean = False
        Dim args() As String = e.Argument
        Dim FileToZip As String = args(0)
        Dim ZipDir As String = args(1)
        Dim ZipPassword As String = args(2)
        Dim ZipAES As Boolean = args(3)

            Using zip As ZipFile = New ZipFile
                If ZipPassword <> Nothing Then
                    zip.Password = ZipPassword  ' do not forget this!
                    If ZipAES Then
                        zip.Encryption = EncryptionAlgorithm.WinZipAes256 'Encrypt AES256
                    End If
                End If
                zip.AddFile(FileToZip, "")
                zip.Comment = ("This zip file was created at " & DateTime.Now.ToString("G")) & " By " & GetFullName(SystemInformation.UserName)
                AddHandler zip.SaveProgress, New EventHandler(Of SaveProgressEventArgs)(AddressOf Me.zip_CompressProgress)
            End Using
        Catch ex1 As Exception
        End Try

    End Sub

Jun 23, 2010 at 1:31 PM
I'm not sure which versions of the Visual Studio tool have the profiler, but I do know that a profiler would quickly pinpoint the source of the 10s delay in your app. If not Visual Studio profiler, maybe there's a third party profiler you can use. Maybe you can use a trial version of VS with the profiler (I don't know). All I'm saying is a profiler would give you the answer quickly. Your code looks fine, and I cannot think of anything that would lead to a 10s delay. Is this on a PC, full framework? (not a mobile device, right? not compact framework?) It could be a transient effect. If for some reason the use of zip encryption requires the loading of an external assembly, the first time through may result in a delay. subsequent runs through the same code (in the same process) won't incur that cost and won't give you the delay. So if you don't have a profiler, try running 10 iterations and see if the delay happens each time. Just a few ideas...