vb.net 解压一个 rar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18522605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:51:48  来源:igfitidea点击:

Unpack a rar file

vb.netunpackunrar

提问by codegenius

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.

好的,所以我搜索了可以让我解压文件的 dll 文件,我找到了很多,例如 unrar.dll、chilkat、sharpcompress 等等,但我想使用 Rar 自己提供的那个。

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

所以我在我的项目中引用了DLL文件并导入了它。我使用的是 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.

但是我找不到任何最新的代码来让我测试和尝试。我发现的所有示例要么不是最新的,要么不适用于 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.

我只想将特定位置的 rar 文件解压缩到我的程序的根目录,所以如果我的程序在桌面上,我希望它解压缩我的文档中的文件并将文件解压缩到我的桌面。

回答by Michael B.

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

如果你只是想解压文件,我可以用SharpCompress做到这一点

First I created a new VB.Net App and added a reference to SharpCompress.dll before using this code to extract all the files from a Rar file.

首先,我创建了一个新的 VB.Net 应用程序并在使用此代码从 Rar 文件中提取所有文件之前添加了对 SharpCompress.dll 的引用。

'Imports 
Imports SharpCompress.Archives
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.Key)
        entry.WriteToDirectory("C:\unrar", New ExtractionOptions With  
                              {.ExtractFullPath = True, .Overwrite = True})
    End If
Next

More code samples

更多代码示例

回答by Wolf Billaros

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

对于那些将在 vb.net 中尝试的人,提取选项被重命名并用作

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