天天看點

vb6使用WinRAR壓縮和解壓檔案

'[先引用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

vb6使用WinRAR壓縮和解壓檔案
參考資料:
  • VB 利用WINRAR 壓縮檔案及解壓
  • WinRAR的指令行模式用法介紹