-1

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".

Nick.McDermaid
  • 15,079
  • 5
  • 40
  • 67
BuddyRoach
  • 139
  • 1
  • 16
  • You have an _UNRAR_ subroutine. Whats the difficulty with writing a _RAR_ subroutine? – Nick.McDermaid Jul 01 '18 at 08:49
  • I am not familiar with the switches. Also, I've read online that rar.exe isn't the one to use to compress things. But rather Winrar itself. But I do not want to use winrar. I want the function to be done without another application popping up. So is "x" the switch for unrarring? – BuddyRoach Jul 01 '18 at 08:51
  • How did you become familiar with the UNRAR switches? – Nick.McDermaid Jul 01 '18 at 08:58
  • 1
    Normally you go to a command prompt and type `rar /?` to get a guide, then you google, then you experiment. Does it have to be rar? Here's a guide to gzip directly in .net https://msdn.microsoft.com/en-us/library/ms404280(v=vs.100).aspx – Nick.McDermaid Jul 01 '18 at 09:01
  • I didn't. I found that code snippet online. I understand what is going on. I just don't understand the winrar arguments passed through it. I went to the winrar manual online to make sense of it but I just can't grasp the concept of it and I did not see anything about compressing it. – BuddyRoach Jul 01 '18 at 09:01
  • Please try the example here. https://superuser.com/questions/902663/winrar-command-line-zip-folder – Nick.McDermaid Jul 01 '18 at 09:10
  • That outputs a folder. I already have the folder. I am just trying to compress the folder into a RAR. – BuddyRoach Jul 01 '18 at 09:16
  • So you are saying that when using that syntax, no `.rar` file is created? – Nick.McDermaid Jul 01 '18 at 10:10
  • Yeah. I looked at my code and apparently the chr(34) is a quotation character which is needed in this syntax when describing directories. Since my archive is password protected, I created a dummy non protected one to test called "ass.rar". I have found the following code to show results: Since they are too long for this comment, I will add them to my question above. – BuddyRoach Jul 01 '18 at 10:22
  • I can't see your folder structure so your question edit doesn't make much sense to me. I think you're just going to take some time to look at what you have and troubleshoot it. You already worked out the `CHR(34)` thing. You might also want to consider a solution that doesn't require installing and running external EXE's. For example, this sample makes it look really quite trivial: https://www.codeproject.com/Articles/381661/Creating-Zip-Files-Easily-in-NET – Nick.McDermaid Jul 01 '18 at 11:24
  • First, `UnRAR.exe` is freeware and can be shipped with your application. But `Rar.exe` is shareware. So every user of your application needs to buy a license for WinRAR to use `Rar.exe` legally. Second, the program files directory of *WinRAR* contains the text file `Rar.txt`. This is the manual for console version `Rar.exe` which explains everything. It is best practice to read that text file from top to bottom and build the command line needed to create the RAR archive with command, switches, archive file name and folder to archive. – Mofi Jul 01 '18 at 11:30
  • Third, source code of `UnRAR.exe` is available on [WinRAR Extras](https://www.rarlab.com/rar_add.htm) download page as well as __UnRAR OCX control__ for use within a Visual Basic program. But the sources of RAR compression executables `Rar.exe` and `WinRAR.exe` are not available public because of being copyright protected by RAR/WinRAR author Alexander Roshal. The usage of those two applications requires buying a license. Start *WinRAR*, click in menu __Help__ on menu item __About__ and click in opened dialog window on button __License__ and read the END USER LICENSE AGREEMENT. – Mofi Jul 01 '18 at 11:37
  • See also [Simply compress 1 folder in batch with WinRAR command line?](https://stackoverflow.com/questions/24397921/) But really read the end user license agreement, especially point 3 of it. What you want to do clearly violates the end user license agreement of RAR/WinRAR. – Mofi Jul 01 '18 at 11:45
  • I am not selling this program or monetizing it in any way, nor am I even distributing it for free. The reason I am using Winrar is for the password protection feature. Unless there is another method which supports password protected archiving and also does not violate any license agreement, then I am all ears. – BuddyRoach Jul 02 '18 at 05:50
  • _The usage of those two applications requires buying a license_. Personally I find 7Zip a little easier to use. TBH I haven't seen anyone use WinRAR since about Windows 95. Password protected compression is not real encryption anymore. It's easily broken. Is the intention to protect and encrypt files or is the intention to compress files and make it a bit difficult to get into? – Nick.McDermaid Jul 02 '18 at 11:35
  • @Nick.McDermaid You information about WinRAR is obviously 20 years outdated. RAR is used by many applications and many people. Java archives (JAR) are RAR compressed Java files. Many installers use RAR in background. RAR/WinRAR makes it possible to just password protect an archive, or completely encrypt it with a strong AES encryption. A completely encrypted archive can't be opened without the passphrase to decrypt it. It is not even possible to get a list of files and folders of a totally encrypted RAR archive without knowing the passphrase. – Mofi Jul 02 '18 at 12:43
  • You're right - the statement I made about encryption is incorrect. But I must say that WinRAR is not my goto for compression. 7Zip is. I've just done some self education and it appears .7z format also does AES encryption and appears to not require a licence. Happy for you to correct or clarify on that. I seriously have not seen WinRAR for 20 years but then that's not my area. – Nick.McDermaid Jul 02 '18 at 13:25

1 Answers1

1

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.

BuddyRoach
  • 139
  • 1
  • 16