# VBForums CodeBank > CodeBank - Visual Basic .NET >  [2008] [VB.NET] Winrar DLL Simple Usage

## youngbucks

Here is a a code snippet of how you can use the Winrar DLL. This is only a simple example for those who dont understand and need a barebone. You may download the source code from below. I took out most of the junk that you would get in the examples included in installation so you can understand it easier.

This allows you to open an RAR file and extract it to a specified directory of your choice. Download WinRARDLL here.



```
Imports RARNET
Public Class Form1
    Dim d As Decompressor
    Dim m_dll As String = Application.StartupPath & "\unrar.dll"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            d.UnPackAll(GetPath())
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub Unpacked(ByVal r As Decompressor.RAREntry)
        StatusBar1.Panels.Item(0).Text = r.FileName & " Unpacked"
    End Sub

    Public Sub InProgress(ByVal TotalFileSize As Long, ByVal CurrentFileSize As Long)
        If TotalFileSize = 0 Then Exit Sub
        StatusBar1.Panels.Item(1).Text = Format(CurrentFileSize / TotalFileSize, "percent")
    End Sub

    Private Function GetFile() As String
        Dim f As New OpenFileDialog
        f.Title = "Unpack RAR Archive"
        f.CheckFileExists = True
        f.DefaultExt = "RAR"
        f.Filter = "RAR files (*.rar)|*.rar|All files (*.*)|*.*"
        f.RestoreDirectory = True
        If f.ShowDialog() = DialogResult.OK Then
            Return f.FileName
        End If
        Return ""
    End Function

    Private Function GetPath() As String
        Dim f As New FolderBrowserDialog
        f.Description = "Unpack RAR Archive"
        f.ShowNewFolderButton = True
        If f.ShowDialog() = DialogResult.OK Then
            Return f.SelectedPath
        End If
        Return ""
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim szFile As String = GetFile()
        d = New Decompressor(szFile, "khogarth")
        AddHandler d.OnUnpack, AddressOf Unpacked
        'AddHandler d.Unpacking, AddressOf InProgress

        For Each r As Decompressor.RAREntry In d.RARFiles
            Dim lvitem As New ListViewItem(r.FileName)
            ' Place a check mark next to the item.
            lvitem.SubItems.Add(r.FileAttr)
            lvitem.SubItems.Add(r.PackSize)
            lvitem.SubItems.Add(r.UnpSize)
            lvitem.SubItems.Add(r.FileCRC)
            Lv.Items.Add(lvitem)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If System.IO.File.Exists(m_dll) Then
        Else
            MsgBox(m_dll & " cannot be found.")
            Application.Exit()
        End If
        Lv.View = View.Details
        Lv.GridLines = True
        ' Create some columns 
        Lv.Columns.Add("File", 200, HorizontalAlignment.Left)
        Lv.Columns.Add("Attr", 30, HorizontalAlignment.Left)
        Lv.Columns.Add("Packed Size", 100, HorizontalAlignment.Left)
        Lv.Columns.Add("Unpacked Size", 100, HorizontalAlignment.Center)
        Lv.Columns.Add("CRC", 120, HorizontalAlignment.Center)
    End Sub

End Class
```

----------


## Abbas Haider

Hi

I installed your dll but it says that it got no library files error when I reference it with Visual Studio. I checked from the folder IT DID got a library file howver I dont know why VS didnt pick it.

----------


## ahostbr

hello when converting the example project to vb.net 2008 there was only one error


vb Code:
AddHandler d.Unpacking, AddressOf InProgress

could you please help me with this,

d.Unpacking was underlined green... and InProgress was undelined blue and says "does not have a signature compatible with delegate"

----------


## youngbucks

It should be :


vb Code:
Public Sub InProgress(ByVal fTotalFileSize As Long, ByVal fUnpacked As Long, ByRef Disposition As RARNET.Decompressor.RarDisposition)  End Sub

Did that fix ur problem?

----------


## ahostbr

thank you but no it didnt,

heres the old Inprogress sub,  notice
"ByVal TotalFileSize As Long, ByVal CurrentFileSize As Long"


vb.net Code:
Public Sub InProgress(ByVal TotalFileSize As Long, ByVal CurrentFileSize As Long)        If TotalFileSize = 0 Then Exit Sub        StatusBar1.Panels.Item(1).Text = Format(CurrentFileSize / TotalFileSize, "percent")    End Sub


heres the one i tried to add, but it gave me errors with
"CurrentFileSize / TotalFileSize"


vb Code:
Public Sub InProgress(ByVal fTotalFileSize As Long, ByVal fUnpacked As Long, ByRef Disposition As RARNET.Decompressor.RarDisposition)    If TotalFileSize = 0 Then Exit Sub    StatusBar1.Panels.Item(1).Text = Format(CurrentFileSize / TotalFileSize, "percent")    End Sub


where should i add the progress bar part?

vb.net Code:
Progressbar1.value = CInt(CDbl(Format(fUnpacked / fTotalFileSize)) * 100)

----------


## youngbucks

Your supposed to change those since u just changed the event names. Change that to  Format(fUnpacked / fTotalFileSize, "percent") . As for the progress, that also goes into the Inprogress section and along with the update of the text it will give progress. You need to add a progress bar to it dnt forget lol

So this should be the final thing u have for that part:

vb.net Code:
Public Sub InProgress(ByVal fTotalFileSize As Long, ByVal fUnpacked As Long, ByRef Disposition As RARNET.Decompressor.RarDisposition)If fTotalFileSize = 0 Then Exit Sub    StatusBar1.Panels.Item(1).Text = Format(fUnpacked / fTotalFileSize, "percent")    Progressbar1.value = CInt(CDbl(Format(fUnpacked / fTotalFileSize)) * 100)End Sub

----------


## youngbucks

2 Updated samples added. One that works regularly and another that uses multithreading (this one is for if your planning to decompress big files).

----------


## blacksea999

Thnksss..

----------


## schalk

Hello. If I include this winrar extractor in one of my programs and I distribute it with the Rar DLL, does the user have to actually have winrar installed, or do they only need the RarDLL? Also is it legal to distribute the RarDLL with a program?

----------


## youngbucks

Yes it is legal for you to redistribute as far as im concerned. Also no the user doesnt have to have Winrar installed as it has its own library using.

----------


## schalk

OK thanks a lot!

----------


## tourist

Hello,

First I would like to thank you for this great example. That's exactly what I needed  :Smilie: 

Unfortunately I have a small issue with the Unrar.dll.

I don't want for the user to have an interraction with the program. Let's say my rar is corrupted or  if it's a file which has been splitted into 10 .rar files and one is missing, I don't want to get a msgbox :

UnRAR
Load Volume C:\Win\myrar.r02
OK - Cancel


Is there a way to get rid of these msgbox ?  I would like to get an error code if  there is a problem and let the program run.



Another quick question : When opening the .rar file ( which is part of a split archive , r01,r02,r03, etc )  is there a way to get by code a list of all the rar files ( to be able to delete them )

Something  like


            Dim d As New Decompressor(TextBox1.Text)
            AddHandler d.OnUnpack, AddressOf Unpacked
            AddHandler d.Unpacking, AddressOf InProgress
            d.UnPackAll("C:\Win\myrar.rar")

            Dim mytab() as string
            And get mytab(0) = "myrar.r00"
            mytab(1) = "myrar.r01"

etc ...





Thanks for your help !

----------


## MarkBayhon

Dear Guys,

I have a question, will this work on Windows 2003 server R2 Standard edition x64  with service pack 1? 

My colleauge developed a simple application that downloads  .rar files from our FTP and extracts it. but when we installed it on a computer that has an OS i have mentioned above,  it gives us a message in our log file the "extraction failed"

In other computers it is working aside from this OS, does it has something to do with the .net framework or with the OS or maybe in the code it self??

Many thanks,
--Mark

----------


## dkittell

Do you have a way to incorporate a password into this example?

I'd like to on the fly unrar files with passwords without needing user interaction.

----------


## youngbucks

Yes it is possible, If the passwords for the rar are static you can hard code it and it will automatically process them. If they are dynamic you can use the password control to input it manually.

----------


## dkittell

You wouldn't happen to have an example would you?

----------


## youngbucks

Yes i do, bare with me for a couple minutes while i re-install Visual basic express.

----------


## dkittell

not a problem, thank your for your assistance

----------


## youngbucks

The decompressor class given with the solution has the control for the password already in it so for dynamic passwords you simply need to create a form to pass the password through (via String). Add a textbox and a button to it. 

Name the textbox: txtPW
Name the button: btnOK

Add this code to the form:

vb.net Code:
Public pw As String    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click        pw = Me.txtPW.Text        Me.Hide()    End Sub

Note: If you do not name the form "pwDLG" you are going to have to change the code in the decompressor class. Find (Ctrl+f) "Case UCM_NEEDPASSWORD" and change "Dim f As New *pwDlg*" accordingly.

----------


## dkittell

thank you for your help

----------


## RoyDJ

thanks for your class(winrar.dll)
i have several rar file in same dir.
some have pass "aaa" some others "bbb", we can assume 50% each
if i put static pass 50% of file are not been extracted, if i use it dynamically i got pupup for password for every rarfile.

is possible to make it try both pass for each file?
is possible to eliminate your pwDlg form from class?
can you share your class as source code and not as dll?

thanks.

----------


## RoyDJ

tried to do some FOR before set the pass , i got only crc32 error .. deleting try catch ... all goes good.

but if you share the class souce, would be nice

----------


## youngbucks

There is a value that is returned when you send the password, use that  and Create an array for your passwords and loop through it.

----------


## RoyDJ

what value? i not found it

----------


## youngbucks

In the UnrarNET.dll or Decompression class it checks if a password is needed through:



```
If mPassword <> "" Then RARSetPassword(mRARHandle, mPassword)
Dim hr As New RARCallBack(AddressOf RARCB)
Call RARSetCallback(mRARHandle, hr, 0)
```

It fires off Case UCM_NEEDPASSWORD in RARCB which uses the pwDLG to get the password:



```
                'I need a password
                Dim f As New pwDlg
                f.ShowDialog()
                Dim pwB As Byte() = Encoding.ASCII.GetBytes(f.pw & Chr(0))
                P2 = f.pw.Length
                f.Close()
                Marshal.Copy(pwB, 0, P1, pwB.Length)
                Return +1 '??
```

You can use this method and test the result or you can use a try method and catch the error and then go back and tell it to try and but with the new password.

----------


## RoyDJ

yes, i did a 
vb Code:
For Each File In files              For Each pass As String In passlist.Items                d = New Decompressor(File, pass)                AddHandler d.OnUnpack, AddressOf Unpacked                AddHandler d.Unpacking, AddressOf InProgress                Try                    d.UnPackAll(tot.Text)                    Exit For                Catch ex As Exception                    'MsgBox(ex.Message)                End Try        Next Next

but for large file it takes long time only to test 1 pass  :Frown:  i have 10 pass to test ...

----------


## RoyDJ

dupe post.

----------


## d_a_r_k

Where can I find the x64 version of the UnRARNET.dll?

----------


## youngbucks

> Where can I find the x64 version of the UnRARNET.dll?


Download the WinRAR dll package from my first post and extract it to a folder. In it will be a folder called "x64" and it a 64 bit version of the dll.

----------


## d_a_r_k

Thanks for your reply in such short notice, but I only find folder "dlls" which contains "unrar.dll" and "UnRARNET.dll" I have the 64bit version of unrar.dll though. I need 64bit ver of the UnRARNET.dll not the unrar.dll. This package only contains the x86 version of UnRARNET.dll  :Frown:

----------


## MrTree

Is it possible to add to your library to unzip .zip files? since Winrar is capable of unzipping zip files. Thanks

----------


## obi1kenobi

> Is it possible to add to your library to unzip .zip files? since Winrar is capable of unzipping zip files. Thanks


Try looking for a project named DotNetZipLib on CodePlex. I've used it once, it's written in .Net and it's very easy to use.

----------


## MrTree

But does it include RAR and ZIP?

----------


## techgnome

the WinRAR APPLICATION can... because it has a ZIP library in it... but the RAR library itself is probably incapable of unzippin ZIP files. If you need your app to handle both, you'll need to get both a RAR library and a ZIP library and include them both in your app. 

-tg

----------


## obi1kenobi

> But does it include RAR and ZIP?


It includes Zip only, but since you already got a Rar solution within this thread...

----------


## MrTree

No but it would be a lot easier to have Rar and zip in one library.

----------


## youngbucks

> No but it would be a lot easier to have Rar and zip in one library.


If you want to extract rar and zip you can use the Unrar command line and package it with your application.

http://www.rarlab.com/rar_add.htm

----------


## tanStaafl

Better late than never..

For those of you getting the warning (and it is only a warning) on the AddHandler d.Unpacking, AddressOf InProgress line, here is the fix

AddHandler Decompressor.Unpacking.AddressOf InProgress

That's all there is to it.

----------


## CatchItBaby

can we rar folder and its subfolder using winrar dll ?

Thanks

----------


## gxry

hi, I've tried unrar.dll, but whether unrar.dll can read bytes from a file inside a *.rar?
or whether there are other techniques for reading bytes from a file that is in the *.rar?

tx..

----------


## moti barski

1 where does the path string of the rar file go in the code ?
2 does btn 1 or 2 unpack the rar file ?
3 in case the rar is passworded where(in the code or the form) does the user enter the pass ?
4 is post #1 edited or will it raise some of the glitches raised in the following posts ?

----------


## swab

> The decompressor class given with the solution has the control for the password already in it so for dynamic passwords you simply need to create a form to pass the password through (via String). Add a textbox and a button to it. 
> 
> Name the textbox: txtPW
> Name the button: btnOK
> 
> Add this code to the form:
> 
> vb.net Code:
> Public pw As String
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        pw = Me.txtPW.Text
        Me.Hide()
    End Sub
> ...


how do i enter a static password

----------

