Extracting A File From An Archive

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
i have a file written by another program, but changing the file's extension to zip, i could view the contents of the file.
Is it now possible to extract a specific content from that file without changing the extension using vba?
Actually the content i want to extract is a picture file and i want to set it into an image box on a userform
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I haven't sent or received a ZIP file in years, but the best I can remember is that when you unzip it, it automatically opens in the file format that it was originally created in. ie. If it was a Word file it will open as a .doc* and if it was an Excel file it will open as a .xls* with the asterisks being the appropriate file format type. I don't know that you can access a .zip file without unzipping it. Never occurred to me to try it and wouldn't know where to start since it is a compressed file format.
 
Last edited:
Upvote 0
I haven't sent or received a ZIP file in years, but the best I can remember is that when you unzip it, it automatically opens in the file format that it was originally created in. ie. If it was a Word file it will open as a .doc* and if it was an Excel file it will open as a .xls* with the asterisks being the appropriate file format type. I don't know that you can access a .zip file without unzipping it. Never occurred to me to try it and wouldn't know where to start since it is a compressed file format.
Take a look at this code
Code:
 Sub Unzip2()    Dim FSO As Object    Dim oApp As Object    Dim Fname As Variant    Dim FileNameFolder As Variant    Dim DefPath As String    Dim strDate As String    Dim fileNameInZip As Variant    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _                                        MultiSelect:=False)    If Fname = False Then        'Do nothing    Else        'Root folder for the new folder.        'You can also use DefPath = "C:\Users\Ron\test\"        DefPath = Application.DefaultFilePath        If Right(DefPath, 1) <> "\" Then            DefPath = DefPath & "\"        End If        'Create the folder name        strDate = Format(Now, " dd-mm-yy h-mm-ss")        FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"        'Make the normal folder in DefPath        MkDir FileNameFolder        'Extract the files into the newly created folder        Set oApp = CreateObject("Shell.Application")        'Change this "*.txt" to extract the files you want        For Each fileNameInZip In oApp.Namespace(Fname).items            If LCase(fileNameInZip) Like LCase("*.txt") Then                oApp.Namespace(FileNameFolder).CopyHere _                        oApp.Namespace(Fname).items.Item(CStr(fileNameInZip))            End If        Next        MsgBox "You find the files here: " & FileNameFolder        On Error Resume Next        Set FSO = CreateObject("scripting.filesystemobject")        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True    End IfEnd Sub
One of the issues i have with this is that it can't check subfolders of the archive
 
Upvote 0
Take a look at this
Code:
[COLOR=#3366CC]Sub Unzip2()[/COLOR]    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim DefPath As String
    Dim strDate As String
    Dim fileNameInZip As Variant

    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                        MultiSelect:=False)
    If Fname = False Then
       [COLOR=black] 'Do nothing[/COLOR]
    Else
      [COLOR=black]  'Root folder for the new folder.
        'You can also use DefPath = "C:\Users\Ron\test\"
        DefPath = Application.DefaultFilePath[/COLOR]
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        [COLOR=black]'Create the folder name[/COLOR]
        strDate = Format(Now, " dd-mm-yy h-mm-ss")
        FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"

       [COLOR=black] 'Make the normal folder in DefPath[/COLOR]
        MkDir FileNameFolder

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

        [COLOR=black]'Change this "*.txt" to extract the files you want[/COLOR]
        For Each fileNameInZip In oApp.Namespace(Fname).items
            If LCase(fileNameInZip) Like LCase("*.txt") Then
                oApp.Namespace(FileNameFolder).CopyHere _
                        oApp.Namespace(Fname).items.Item(CStr(fileNameInZip))
            End If
        Next

        MsgBox "You find the files here: " & FileNameFolder

        On Error Resume Next
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    End If [COLOR=#3366CC]End Sub[/COLOR]
one of my problem with this is that it does not access subfolders
 
Upvote 0
I am a little confused. If you have a .zip file and you know where it is in the directories, why would you need to look any further for your target? I understood that the target was within the .zip file. At any rate, if you want to access subfolders then you can use something like
Code:
[COLOR=#0000FF]Set[/COLOR] fd = Application.FileDialog(msoFileDialogFilePicker)
which allows you to brows the folders and choose the one you want to work with.
 
Last edited:
Upvote 0
I am a little confused. If you have a .zip file and you know where it is in the directories, why would you need to look any further for your target? I understood that the target was within the .zip file. At any rate, if you want to access subfolders then you can use something like
Code:
[COLOR=#0000FF]Set[/COLOR] fd = Application.FileDialog(msoFileDialogFilePicker)
which allows you to brows the folders and choose the one you want to work with.

You did not get me well. i mean how to access the subfolders of the archive
 
Upvote 0
If you have a sub folder in the zip file, then it is way beyound my experience and expertise. All that I have ever dealt with in ZIP is single files. No directories or sub directories. If you are referring to something other than the .zip file as the "Archive" then that is not very clear in the OP nor any of the subsequent posts. I think this is out of my league so I will drop off this thread,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top