Unzip password protected file (1 Viewer)

Harris@Z

Registered User.
Local time
Today, 14:34
Joined
Oct 28, 2019
Messages
73
Hi, hope someone can help!

The below code unzips a non-password protected file perfectly.
However not for a password protected file.
I have struggled to find a solution using the famous search engine.
Can anyone help, or at least aid with an alternative vba?

Thanks

Function Unzip1(ZippedFile As String)
Dim fso As Object
Dim oApp As Object
Dim fname
Dim DefPath As String
Dim strDate As String
Dim strRootPDFPath As String

strRootPDFPath = "C:\Users\" + Environ("Username") + "\Downloads\"
''fname = strRootPDFPath & ZippedFile 'fname = CurrentProject.Path & "\Tek-Tips.zip"
fname = strRootPDFPath & "Ethics_Booklet.zip" 'fname = CurrentProject.Path & "\Tek-Tips.zip"




If fname = False Then
'do nothing
Else
'Set default path to current database folder
DefPath = CurrentProject.Path
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

'strDate = Format(Now, " dd-mm-yy h-mm-ss")
''Create normal folder
Dim FileNameFolder As String
FileNameFolder = strRootPDFPath & "PDFs"

If FolderExistsCreate(strRootPDFPath & "PDFs", True) Then
Else
MkDir strRootPDFPath & "PDFs"
End If


Set oApp = CreateObject("Shell.Application")
'Copy the files in the newly created folder
oApp.NameSpace(strRootPDFPath & "PDFs").CopyHere oApp.NameSpace(fname).Items, 4 + 16 '4 + 16 To override and suppress dialog

'MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set fso = CreateObject("scripting.filesystemobject")
fso.DeleteFolder Environ("Temp") & "\Temporary Directory*", True

Set oApp = Nothing
Set fso = Nothing
End If
End Function
 

CJ_London

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Feb 19, 2013
Messages
16,616
please edit your post to use the code tags to preserve indentation. Your code is difficult to read

Also clarify what you mean by 'password protected file'? the zip file? the contained file? any type of file?
 

Harris@Z

Registered User.
Local time
Today, 14:34
Joined
Oct 28, 2019
Messages
73
Apologies!
The file in question is the zipped file.
If I run the function, and the zipped file is not password protected, the contents are extracted.
However, if password protected, it 'ignores' the file, i.e., nothing extracted.
If I try to open the zipped file in the normal way, i.e., not code, then it asks for a password.

Code:
Function Unzip1(ZippedFile As String)
    Dim fso As Object
    Dim oApp As Object
    Dim fname
    Dim DefPath As String
    Dim strDate As String
    Dim strRootPDFPath As String

    strRootPDFPath = "C:\Users\" + Environ("Username") + "\Downloads\"
    'fname = strRootPDFPath & ZippedFile        'fname = CurrentProject.Path & "\Tek-Tips.zip"
    fname = strRootPDFPath & "33936-1212.zip"        'fname = CurrentProject.Path & "\Tek-Tips.zip"
      

    If fname = False Then
        'do nothing
    Else
    'Set default path to current database folder
        DefPath = CurrentProject.Path
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        'strDate = Format(Now, " dd-mm-yy h-mm-ss")
       ''Create normal folder
        Dim FileNameFolder As String
        FileNameFolder = strRootPDFPath & "PDFs"
        
        If FolderExistsCreate(strRootPDFPath & "PDFs", True) Then
        Else
             MkDir strRootPDFPath & "PDFs"
        End If
        

        Set oApp = CreateObject("Shell.Application")
        'Copy the files in the newly created folder
        oApp.NameSpace(strRootPDFPath & "PDFs").CopyHere oApp.NameSpace(fname).Items, 4 + 16   '4 + 16 To override and suppress dialog

        'MsgBox "You find the files here: " & FileNameFolder
        On Error Resume Next
        Set fso = CreateObject("scripting.filesystemobject")
        fso.DeleteFolder Environ("Temp") & "\Temporary Directory*", True

        Set oApp = Nothing
        Set fso = Nothing
    End If
End Function
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Feb 19, 2013
Messages
16,616
OK - see this link - I presume you have the password


and for the future use the </> button rather than the >_ button which is intended for a single line of code
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:34
Joined
May 7, 2009
Messages
19,245
it can be easily done using DOS Command line of Winrar (Unrar).
 

Harris@Z

Registered User.
Local time
Today, 14:34
Joined
Oct 28, 2019
Messages
73
I have not used this before. Do you by any chance have an example you can direct me to?
 

KitaYama

Well-known member
Local time
Today, 20:34
Joined
Jan 6, 2022
Messages
1,541
I have not used this before. Do you by any chance have an example you can direct me to?
If you have winrar you can use this command line.
The following line extracts all files (*.*) within a zip file (D:\Myfile.zip) and extract it to D:\test folder where the password is 4321

winrar x D:\Myfile.zip *.* D:\test -p4321

This requires winrar is added to your system environment variables (system path).
Otherwise you have to add the full path to your winrar.exe

C:\Program Files\Winrar\winrar.exe x D:\Myfile.zip *.* D:\test -p4321


I'm sure winzip has this utility, but I've never used it. You can search its help.
 
Last edited:

Harris@Z

Registered User.
Local time
Today, 14:34
Joined
Oct 28, 2019
Messages
73
Thanks KitaYama, much appreciated for the guidance.
Thanks everyone for helping me to resolve this issue
 

Users who are viewing this thread

Top Bottom