Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleShell WinZip to zip and unzip files
Description
KeywordsShell, ShellExecute, Zip, UnZip, archive, compress, extract
CategoriesUtilities
 
By Neil Crosby.

The WinZipSub subroutine plays a central role. This routine uses Shell to start WinZip32.exe, passing it -a to archive files or -e to extract files. The cmdWinZip_Click and cmdUnWinZip_Click event handlers call this routine to zip and unzip files.

 
' Zip or unzip the files.
' The default location of the winzip32.exe is
' C:\Program Files\WinZip\. If your winzip32.exe
' is in another folder, change ZIP_EXE accordingly.
Sub WinZipSub(ByVal source As String, ByVal target As _
    String, ByVal zip_it As Boolean)
Const ZIP_EXE As String = """C:\Program " & _
    "Files\WinZip\winzip32"""

    If zip_it = True Then
        ' Zip appending the source file to the target.
        Shell ZIP_EXE & " -a " & target & " " & source, _
            vbHide
    Else
        ' Extract the target to the source directory.
        Shell ZIP_EXE & " -e " & target & " " & source, _
            vbHide
    End If
End Sub
 
Subroutine cmdViewWinZip_Click uses the ShellExecute API function to "open" the zip file. That invokes the file's default action which is to open the file using WinZip and display its contents.

Subroutine cmdViewUnzip_Click uses Shell Execute to "open" the temp directory. That invokes the directory's default action which is to display the directory's contents.

 
' Zip up the files in the current directory.
Private Sub cmdWinZip_Click()
Dim next_file As String
Dim target As String
Dim source As String

    ' Loop over all the files in the current directory.
    next_file = Dir(m_AppPath & "*.*", vbNormal)
    Do While next_file <> ""
        source = """" & m_AppPath & next_file & """"
        target = """" & m_AppPath & "temp\Project.zip" & _
            """"

        ' Add this file to Project.zip
        WinZipSub source, target, True
        next_file = Dir
    Loop

    cmdViewWinZip.Visible = True
End Sub

' Open the zip file.
Private Sub cmdViewWinZip_Click()
    ' Open the Zip file.
    ShellExecute 0, "Open", """" & m_AppPath & _
        "temp\Project.zip" & """", vbNullString, _
        vbNullString, 1
    cmdUnWinZip.Visible = True
End Sub

' Unzip the zip file.
Private Sub cmdUnWinZip_Click()
Dim target As String
Dim source As String

    ' Compose the source (destination directory)
    ' and target (Zip file) names.
    source = """" & m_TempPath & """"
    target = """" & m_AppPath & "temp\Project.zip" & """"

    ' Extract the files.
    WinZipSub source, target, False

    cmdViewUnzip.Visible = True
End Sub

' Open the temp directory to display its files.
Private Sub cmdViewUnzip_Click()
    ' Open the directory.
    ShellExecute 0, "Open", """" & m_TempPath & """", _
        vbNullString, vbNullString, 1
End Sub
 
The last piece deals with the temporary directory. When the program starts, it creates a temp directory inside the current directory (if it's not already present). When the program unloads, it removes that directory and any files it contains.
 
' Create the temp directory.
Private Sub Form_Load()
    ' Get the application and temp directories.
    m_AppPath = App.Path
    If Right$(m_AppPath, 1) <> "\" Then m_AppPath = _
        m_AppPath & "\"
    m_TempPath = m_AppPath & "temp\"

    ' Make the temp directory if it doesn't exist.
    If Dir(m_TempPath, vbDirectory) = "" _
        Then MkDir m_TempPath
End Sub

' Remove the temp directory.
Private Sub Form_Unload(Cancel As Integer)
Dim next_file As String

    ' Remove the files in the directory.
    next_file = Dir(m_TempPath & "*.*", vbNormal)
    Do While next_file <> ""
        Kill m_TempPath & next_file
        next_file = Dir
    Loop

    ' Remove the directory.
    RmDir m_TempPath
End Sub
 
Apparently you can only shell WinZip in vbHide mode if you have licensed WinZip. If you are using the evaluation version, you need to run the shell in vbNormalFocus mode. Then WinZip displays a message saying it is running in evaluation mode and you must press a key to continue.

Thanks to Pooya Kashefifar for pointing this out!

WinZip is such a worthwhile program that I encourage you to register.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated