Search code examples
vb.netwinrar

Compressing a Folder Using Rar.exe via VB.NET?


I already extracted a folder with files from a RAR archive using the Unrar.exe I then want to edit the files within the extracted folder and then Re-rar that folder back into a password protected RAR archive. Either that, or append the existing RAR archive. Either way, the main goal is to update the files within the password protected archive. But I can't seem to figure out how to compress the folder again. My code as follows:

Imports System.IO

Public Class Form1
    'establish the application directory and set it as a string to plugin later when needed
    Dim MAINDIR As String = AppDomain.CurrentDomain.BaseDirectory



    Private Sub UNRAR()
        'if extracted folder does NOT exist then
        If Not (System.IO.Directory.Exists(MAINDIR & "Credentials\")) Then
            'set variables
            Dim SourceFile As String = MAINDIR & "Credentials.rar"
            Dim PassWord As String = "locker101"
            Dim DestinationFolder As String = MAINDIR
            'if extracted folder does not exist then
            If Not IO.Directory.Exists(DestinationFolder) Then IO.Directory.CreateDirectory(DestinationFolder)
            'unrar it and create extracted folder with the files
            Dim p As New Process
            p.StartInfo.FileName = MAINDIR & "UnRAR.exe"
            p.StartInfo.Arguments = "-p" & PassWord & " x " & Chr(34) & SourceFile & Chr(34) & " " & Chr(34) & DestinationFolder & Chr(34)
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            p.Start()

        End If

    End Sub



    Private Sub EDIT(ByVal ACCOUNT, ByVal USER, ByVal PASS, ByVal URL)
        Do Until (System.IO.Directory.Exists(MAINDIR & "Credentials\"))
            'does nothing on loop and keeps checking for the folder to exist
        Loop
        'confirms that the folder exists then begins to write to the file(s) inside
        If (System.IO.Directory.Exists(MAINDIR & "Credentials\")) Then

            Dim file As System.IO.StreamWriter
            file = My.Computer.FileSystem.OpenTextFileWriter(MAINDIR & "Credentials\" & ACCOUNT & ".txt", False)
            file.WriteLine(USER)
            file.WriteLine(PASS)
            file.WriteLine(URL)
            file.Close()
            MsgBox("DONE")
        Else
            MsgBox("FAILED")

            MsgBox("END existing")

            MsgBox("END LOOP")

        End If
        APPENDRAR()
    End Sub

    Private Sub APPENDRAR()


    End Sub



    Private Sub DELETE()
        'deletes the extracted folder, leaving behind only the password protected rar archive
        My.Computer.FileSystem.DeleteDirectory(MAINDIR & "Credentials", False, False)
    End Sub



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        UNRAR()
        'sets textboxes' texts as strings then sends those values to the EDIT sub
        Dim btn As Button = DirectCast(sender, Button)
        Dim ACCOUNT As String = TB_account.Text
        Dim USER As String = TB_user.Text
        Dim PASS As String = TB_pass.Text
        Dim URL As String = TB_url.Text
        EDIT(ACCOUNT, USER, PASS, URL)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub
End Class

It should also be mentioned that I have the Unrar.exe and Rar.exe files in the application folder. The same files found in the Winrar directory. I added them to my project folder to use them.

enter image description here

A brief explanation of what is expected: The user will fill out 3 textboxes (username, password, website url) then they will click a button. The button takes the text in each textbox as values then creates strings of those values. It then passes these string values onto the sub which UNRARS the RAR archive, then once the folder is extracted, it either overwrites any existing file in that folder or creates a new one. Then after that, it should repack this newly edited folder back into a RAR archive with the same password as before, then deletes the extracted folder and the old RAR archive (UNLESS I can just append them back into the original archive, then I would not have to make an "updated" archive.)

UPDATE:

 Dim SourceFile As String = MAINDIR & "Credentials\"
Dim PassWord As String = "locker101"
Dim DestinationFolder As String = MAINDIR
'if extracted folder does not exist then
'If Not IO.Directory.Exists(DestinationFolder) Then IO.Directory.CreateDirectory(DestinationFolder)
'unrar it and create extracted folder with the files
Dim p As New Process
Dim bbs As String = "-p" & PassWord & " u " & Chr(34) & MAINDIR & "ass.rar" & Chr(34) & " " & Chr(34) & SourceFile & Chr(34)
p.StartInfo.FileName = MAINDIR & "Rar.exe"
p.StartInfo.Arguments = bbs
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()

This seems to work but it seems to not just add the "Credentials" folder but every folder leading up to it starting from "Projects".


Solution

  • I finally figured it out. Just in case anyone else needed the answer, here it is.

     Dim p0 As New Process
                Dim createo As String
                createo = "-p" & <PASSWORD> & " u -ep " & Chr(34) & <arhive.rar> & Chr(34) & " " & Chr(34) & <FILE TO REPLACE> & Chr(34)
                p0.StartInfo.FileName = "Rar.exe"
                p0.StartInfo.Arguments = createo
                p0.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                p0.Start()
    

    Example for the string

     createo = "-p" & TB_BIGKEY.Text & " u -ep " & Chr(34) & Form1.MAINDIR & "Users\" & TB_ID.Text & ".rar" & Chr(34) & " " & Chr(34) & Form1.MAINDIR & "Users\" & TB_ID.Text & ".txt" & Chr(34)
    

    Which is read out 'tostring' as

    -ppassyword u -ep "F:\Projects\PG\PG\bin\Debug\Users\Username.rar" "F:\Projects\PG\PG\bin\Debug\Users\Username.txt"
    or <PASSWORD> <UPDATE> <EXLUDE PATHS> <RAR TO UPDATE> <FILE TO UPDATE WITH>
    

    To not use a password, just remove the -ppassyword part.