繁体   English   中英

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

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

我使用SevenZipSharp库生成了一个多卷7z文件。

我的问题是,当我尝试提取文件时,出现有关无效转换的异常:

无法投射物体

类型'SevenZip.InMultiStreamWrapper'更改为'SevenZip.InStreamWrapper'。

引发异常的方法是SevenZipExtractor.Check()

这是用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

如果我忽略具有设置了密码的多卷文件的完整性检查,那么我将无法提取它,因为发生了另一个异常...

Probablly是其源代码中的错误,但我想确定一下,因为该库不支持提取多卷文件是很奇怪的...

可能是他们源代码中的错误

确实是这样。

查看SevenZipExtractor.cs源代码,我们看到以下几行(在finally块内部,因此它总是执行):

((InStreamWrapper)_archiveStream).Dispose();

其中_archiveStream是类型的类字段IInStream (注意I ),这是在不脱离导出接口类型IDisposable ,因此具有没有Dispose方法。

更深入地讲,我们可以看到它是使用InStreamWrapperInMultiStreamWrapper类的实例初始化的。 虽然它们都共享公共基类StreamWrapper ,但是后者不继承自前者,因此强制转换异常。

如果您愿意修改源代码,则修复它非常容易。 只需将上面的行替换为:

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

然而

如果我忽略具有设置了密码的多卷文件的完整性检查,那么我将无法提取它,因为发生了另一个异常...

他们没有内部调用Check方法,并且在调用ExtractArchive之前是否调用Check是否应该没有任何关系。 因此,我怀疑修复上述错误是否会阻止您正在谈论的另一个异常。

暂无
暂无

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

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