CodeItBetter Programming Another VB Programming Blog

How to use WinZip to zip and unzip files

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'Miscellaneous - How to use WinZip to zip and unzip files
Option Explicit
 
' From WinZip Help files:
' "filenames containing spaces must be enclosed in double quotes."
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private m_AppPath As String
Private m_TempPath As String
 
' 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
 
' Unzip the zip file.
Private Sub UnWinZip()
    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
End Sub
 
' Open the temp directory to display its files.
Sub ViewUnzip()
    ShellExecute 0, "Open", """" & m_TempPath & """", vbNullString, vbNullString, 1
End Sub
 
' Open the zip file.
Sub ViewWinZip()
    ' Open the Zip file.
    ShellExecute 0, "Open", """" & m_AppPath & "temp\Project.zip" & """", vbNullString, vbNullString, 1
End Sub
 
' Zip up the files in the current directory.
Sub ZipIt()
    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
End Sub
 
' Create the temp directory.
Private Sub Form_Load()
    ' Get the application and temp directories.
    m_AppPath = App.Path & "\"
    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.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.