-
Notifications
You must be signed in to change notification settings - Fork 2
/
zip.bas
50 lines (33 loc) · 1.77 KB
/
zip.bas
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
Option Explicit
Private Const ZIP_LOCATION = "C:\...\7za"
Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STILL_ACTIVE = &H103
Public Sub unzip(ByVal destination As String, zipFileName As String)
'x = extract with full paths
'o = output directory
'ao = overwrite
shellAndWait path:=ZIP_LOCATION & " x " & zipFileName & " -o" & destination & " -ao", windowsState:=vbHide
End Sub
Public Sub zip(ByVal files As String, ByVal zipFileName As String)
'a = add files to archive
'mx5 = compression level 5
shellAndWait path:=ZIP_LOCATION & " a " & zipFileName & " " & files & " -mx5", windowsState:=vbHide
End Sub
Private Sub shellAndWait(ByVal path As String, Optional windowsState)
' Dim wsh As Object
' Set wsh = VBA.CreateObject("WScript.Shell")
' Dim waitOnReturn As Boolean: waitOnReturn = True
' Dim windowStyle As Integer: windowStyle = 1
'
' wsh.Run path, windowStyle, waitOnReturn
Dim hProg As Long, hProcess As LongPtr, ExitCode As Long
If IsMissing(windowsState) Then windowsState = 1 'fill in the missing parameter
hProg = Shell(path, windowsState) 'execute the program
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hProg) 'hProg is a "process ID under Win32. used to get the process handle
Do
GetExitCodeProcess hProcess, ExitCode 'populate Exitcode variable
DoEvents
Loop While ExitCode = STILL_ACTIVE
End Sub