通过VB.NET使用Rar.exe压缩文件夹吗?

时间:2018-07-01 08:27:22

标签: vb.net winrar

我已经使用Unrar.exe从RAR存档中提取了一个包含文件的文件夹,然后我想在提取的文件夹中编辑文件,然后将该文件夹重新rar为受密码保护的RAR存档。或者,或者附加现有的RAR存档。无论哪种方式,主要目标都是更新受密码保护的存档中的文件。但是我似乎无法弄清楚如何再次压缩文件夹。我的代码如下:

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

还应该提到我有Unrar.exe和Rar.exe 应用程序文件夹中的文件。在Winrar中找到相同的文件 目录。我将它们添加到项目文件夹中以使用它们。

enter image description here

有关预期内容的简要说明:用户将填写3个文本框(用户名,密码,网站url),然后单击一个按钮。该按钮将每个文本框中的文本作为值,然后创建这些值的字符串。然后,它将这些字符串值传递到UNARRS RAR归档文件所在的子目录中,然后,在提取文件夹之后,它要么覆盖该文件夹中的任何现有文件,要么创建一个新文件。然后,在那之后,它应该使用与以前相同的密码将这个新编辑的文件夹重新打包到RAR存档中,然后删除提取的文件夹和旧的RAR存档(除非我可以将它们重新添加回原始存档中,否则我不会必须制作一个“更新的”存档。)

更新:

 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()

这似乎可行,但似乎不仅添加了“ Credentials”文件夹,而且还添加了从“ Projects”开始的每个文件夹。

1 个答案:

答案 0 :(得分:1)

我终于明白了。万一其他人需要答案,就在这里。

 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()
  

字符串示例

 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)
  

哪个被读为'tostring'

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

要不使用密码,只需删除-ppassyword部分。

相关问题