• Welcome to Theos PowerBasic Museum 2017.

News:

Attachments are only available to registered users.
Please register using your full, real name.

Main Menu

Using ZIP/UNZIP built into Windows API

Started by Jim Dunn, May 02, 2010, 06:19:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Jim Dunn

José,

I would like to use the ZIP/UNZIP built into the Windows API (maybe the Shell.Application?)

(instead of using LiteZip or zlib, etc.)

Do you have any sample code on your site?

Thx!
Jim

José Roca

 
Sorry, I don't know of any documented Windows API to zip/unzip files.

José Roca


Theo Gottwald

Here is an example in VBA for UNZIP using the SHELLAPI.

http://www.rondebruin.nl/windowsxpunzip.htm

Information

QuoteWarning: The code below is not supported by Microsoft
It is not possible to hide the copy dialog when you copy from a zip folder (only working with normal folders).
Also there is no possibility to avoid that someone can cancel the CopyHere operation or that your VBA
code will be notified that the operation has been cancelled.

Note: Do not Dim for example FileNameFolder as String in the code examples.
This must be a Variant, if you change this the code will not work.

If you want to zip files see this page on my site.
http://www.rondebruin.nl/windowsxpzip.htm

See also the the Zip (compress) section on my site for examples for 7-zip and WinZip.

With this example you can browse to the zip file.
After you select the zip file the macro will create a new folder in your DefaultFilePath
and unzip the Zip file in that folder. You can run the code without any changes.


Sub Unzip1()
    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim DefPath As String
    Dim strDate As String

    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")

        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

        'If you want to extract only one file you can use this:
        'oApp.Namespace(FileNameFolder).CopyHere _
         'oApp.Namespace(Fname).items.Item("test.txt")

        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
End Sub