Unpack a rar file

2019-09-20 10:41发布

问题:

Okay, so I have searched for dll files that will allow me to unrar files and I was able to find quite a few such as unrar.dll, chilkat, sharpcompress and some more but I wanted to use the one provided by Rar themselves.

So I referenced the DLL file in my project and imported it. I was using unrar.dll.

But I wasn't able to find any up to date code to allow me to test and try things out. All the examples I found were either not up to date or not for Vb.net.

I also tried the official example, which came in the installation but that didn't work even after I fixed it and when I tried to use the code I always got an error for

object reference not set to an instance of an object

I just want to unrar a rar file from a specific location to the root directory of my program so if my program was on the desktop I want it to unrar a file in My documents and extract the files to my desktop.

回答1:

If you just want to unrar files, I Was able to do that with SharpCompress

I created new VB.Net App and added reference to SharpCompress.dll and used this code

'Imports 
Imports SharpCompress.Archive
Imports SharpCompress.Common

'Unrar code
Dim archive As IArchive = ArchiveFactory.Open("c:\file.rar")
For Each entry In archive.Entries
    If Not entry.IsDirectory Then
        Console.WriteLine(entry.FilePath)
        entry.WriteToDirectory("c:\unrar", 
          ExtractOptions.ExtractFullPath Or ExtractOptions.Overwrite)
    End If
Next

More code samples



回答2:

for those who will try in vb.net the extract options are renamed and used as

Dim options As New ExtractionOptions With {
    .ExtractFullPath = True,
    .Overwrite = True
}
entry.WriteToDirectory(Application.StartupPath, options)