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.
Code:
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
Last edited by youngbucks; Oct 24th, 2008 at 05:52 PM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
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.
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)
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?
First I would like to thank you for this great example. That's exactly what I needed
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 !
Last edited by tourist; Jul 14th, 2009 at 03:58 AM.
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??
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.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
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.
Last edited by youngbucks; Sep 30th, 2009 at 03:41 PM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
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?
In the UnrarNET.dll or Decompression class it checks if a password is needed through:
Code:
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:
Code:
'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.
Last edited by youngbucks; Mar 23rd, 2010 at 05:00 PM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
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
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.
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?