Question : How to unzip a single file from a zipped subdirectory

Dear Experts,

after searching for a looooong, long time, I have finally managed to write code to unzip ONE SINGLE FILE from a zip file.

However, when I have a ZIP file with subdirectories, this code doesn't work.

So my question is, can you show (or rewrite my) sample code so I will be able to unzip one single file (called A.TXT), from a zipped subdirectory?

Here's how to recreate my specific situation:

  • create a directory C:\TEMP

  • put the attached file in that directory

  • paste my sample code in the immediate window of a Microsoft Office program

  • try to extract A.TXT


Thank you for your time!
Keimpe Wiersma
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP"                          THIS WORKS
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP", "SUBDIR"                THIS WORKS
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP", "A.TXT"		    THIS DOESN'T WORK
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP", "SUBDIR\A.TXT"	    THIS DOESN'T WORK
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP", "\SUBDIR\A.TXT"	    THIS DOESN'T WORK
'UnzipTEST "C:\TEMP\ZIPFILE.ZIP","C:\TEMP", "C:\TEMP\SUBDIR\A.TXT"  THIS DOESN'T WORK
Sub UnzipTEST(strZIPfile As String, strOutputFolder As String, Optional strSingleFileName As String)
    Dim FSO As Object
    Dim oApp As Object

    'Extract the files into the newly created folder
    Set oApp = CreateObject("Shell.Application")

    If strSingleFileName <> "" Then
        oApp.Namespace((strOutputFolder)).CopyHere oApp.Namespace((strZIPfile)).Items.item((strSingleFileName))
    Else
        oApp.Namespace((strOutputFolder)).CopyHere oApp.Namespace((strZIPfile)).Items
    End If
        
End Sub
Attachments:
 
Sample ZIP file
 

Answer : How to unzip a single file from a zipped subdirectory

Your code works fine for me that is extracting a single from the zip in a sub folder. You might want to check if you have permissions to copy the file to the location or make sure the file isn't locked or being used by another application.

Random Solutions  
 
programming4us programming4us