简体   繁体   English

如何使用SevenZipSharp提取多卷7z文件?

[英]How to extract a multi volume 7z file using SevenZipSharp?

I generated a multi volume 7z file using SevenZipSharp library. 我使用SevenZipSharp库生成了一个多卷7z文件。

The problem I have is that when I try to extract the file, I get a exception about an invalid casting: 我的问题是,当我尝试提取文件时,出现有关无效转换的异常:

Unable to cast object 无法投射物体

of type 'SevenZip.InMultiStreamWrapper' to type 'SevenZip.InStreamWrapper'. 类型'SevenZip.InMultiStreamWrapper'更改为'SevenZip.InStreamWrapper'。

The method that throws the exception is SevenZipExtractor.Check() . 引发异常的方法是SevenZipExtractor.Check()

This is a sample code written in Vb.Net to reproduce the extraction problem, but I also can accept a C# solution: 这是用Vb.Net编写的示例代码,用于重现提取问题,但我也可以接受C#解决方案:

Public Overridable Function Extract(ByVal sourceFilePath As String,
                                    ByVal outputDirectorypath As String,
                                    ByVal password As String) As String

    If String.IsNullOrEmpty(password) Then
        Me.extractor = New SevenZipExtractor(sourceFilePath)
    Else
        Me.extractor = New SevenZipExtractor(sourceFilePath, password)
    End If

    ' Check for password matches doing an integrity check.
    If Me.extractor.Check() Then
        ' Start the extraction.
        Me.extractor.ExtractArchive(outputDirectorypath)

    Else
        Throw New Exception(
              "Failed to extract, maybe the provided password does not match?.")

    End If

    Return outputDirectorypath

End Function

If I ignore the integrity check, with a multi volume file that has a password set, then I cannot extract it because another exception occurs... 如果我忽略具有设置了密码的多卷文件的完整性检查,那么我将无法提取它,因为发生了另一个异常...

Probablly is a bug in their source-code, but I ask to be sure, because it's very strange that the library does not support extracting multi volume files... Probablly是其源代码中的错误,但我想确定一下,因为该库不支持提取多卷文件是很奇怪的...

Probablly is a bug in their source-code 可能是他们源代码中的错误

This is indeed the case. 确实是这样。

Looking at the SevenZipExtractor.cs source code, we see the following line (inside the method finally block, so it always executes): 查看SevenZipExtractor.cs源代码,我们看到以下几行(在finally块内部,因此它总是执行):

((InStreamWrapper)_archiveStream).Dispose();

where the _archiveStream is a class field of type IInStream (note the I ) which is an interface type that does not derive from IDisposable , hence has no Dispose method. 其中_archiveStream是类型的类字段IInStream (注意I ),这是在不脱离导出接口类型IDisposable ,因此具有没有Dispose方法。

Going deeper, we can see that it is initialized with instance of either InStreamWrapper or InMultiStreamWrapper class. 更深入地讲,我们可以看到它是使用InStreamWrapperInMultiStreamWrapper类的实例初始化的。 While they both share common base class StreamWrapper , the later does not inherit from the former, hence the cast exception. 虽然它们都共享公共基类StreamWrapper ,但是后者不继承自前者,因此强制转换异常。

Fixing it is quite easy if you are willing to modify the source code. 如果您愿意修改源代码,则修复它非常容易。 Just replace the above line with: 只需将上面的行替换为:

if (_archiveStream is IDisposable)
    ((IDisposable)_archiveStream).Dispose();

However 然而

If I ignore the integrity check, with a multi volume file that has a password set, then I cannot extract it because another exception occurs... 如果我忽略具有设置了密码的多卷文件的完整性检查,那么我将无法提取它,因为发生了另一个异常...

They do not call the Check method internally, and there should not be any relation of whether you call Check or not before calling ExtractArchive . 他们没有内部调用Check方法,并且在调用ExtractArchive之前是否调用Check是否应该没有任何关系。 So I doubt that fixing the above bug will prevent the another exception you are talking about. 因此,我怀疑修复上述错误是否会阻止您正在谈论的另一个异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM