首页 > 解决方案 > 通过 VB.NET 使用 Rar.exe 压缩文件夹?

问题描述

我已经使用 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 目录中找到相同的文件。我将它们添加到我的项目文件夹中以使用它们。

在此处输入图像描述

预期内容的简要说明:用户将填写 3 个文本框(用户名、密码、网站 url),然后他们将单击一个按钮。该按钮将每个文本框中的文本作为值,然后创建这些值的字符串。然后,它将这些字符串值传递到 UNRARS 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()

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

标签: vb.netwinrar

解决方案


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

 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部分。


推荐阅读