vb6使用WinRAR壓縮和解壓檔案
阿新 • • 發佈:2019-01-04
'[先引用Registry Access Functions library(RegObj.dll)]:
Function GetWINRARPath() As String
Dim myReg As New Registry, KeyFound As Boolean
KeyFound = myReg.GetKeyValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe", "Path", GetWINRARPath)
If KeyFound = False Then
'WINRAR.EXE 可以單獨執行,所以可以拷貝到專案目錄下使用
GetWINRARPath = "WinRAR"
End If
If KeyFound = True Then
GetWINRARPath = GetWINRARPath & "/WinRAR"
End If
End Function
Sub compress(ByVal SOURCE As String, ByVal TARGET As String)
Dim WINRARPath As String
WINRARPath = GetWINRARPath
If Dir(SOURCE) > "" Then
On Error Resume Next
Shell WINRARPath & " a -r " & TARGET & " " & SOURCE, vbHide
If Err <> 0 Then
MsgBox "系統未安裝WINRAR.EXE!"
End If
End If
End Sub
Sub decompress(ByVal SOURCE As String, ByVal TARGET As String)
Dim WINRARPath As String
WINRARPath = GetWINRARPath
If Dir(SOURCE) > "" Then
On Error Resume Next
Shell WINRARPath & " x -r " & SOURCE & " " & TARGET, vbHide
If Err <> 0 Then
MsgBox "系統未安裝WINRAR.EXE!"
End If
End If
End Sub
Private Sub Command1_Click()
'壓縮Lock資料夾
compress "Lock/", "Lock.rar"
End Sub
Private Sub Command2_Click()
'解壓到a資料夾
decompress "Lock.rar", "a/"
End Sub
使用前匯入登錄檔的引用Registry Access Functions
參考資料: