如何使用 C# .NET 3.5 解压缩 7z 存档?

How to decompress a 7z archive with C# .NET 3.5?

我希望我的应用使用 .7z 文件中的文件。

我找到了how to decompress a 7z file的一些解释。然而,它们都是关于一个我在野外还没有遇到过的案例:一个解压成单个文件的档案。我希望 .7z 存档可以解压缩到任意数量的文件,可能位于任意子文件夹中。

including the whole 7zip application and then running that in a process. However, that seems like unnecessary extra steps, considering there's an official LZMA SDK 似乎可以做到这一点。此外,它还会导致有关交叉兼容性的问题。

那么,如何在 C# (.NET 3.5) 中使用将 .7z 存档解压缩到给定文件夹中?

来自评论的额外信息

"请显示无效的代码。"
好吧,目前我在另一个问题上使用 this answer 的后半部分。但是,我 期望 不做我想做的事 - output(就像 input)是 FileStream,所以很明显这只会创建一个文件。
它表明 Decoder 是 class 进行解压缩,但它没有适用于例如一个 DirectoryInfo、一个 string 路径或类似的任何东西 - 都是 一个文件输入,一个文件输出 。我发现的关于使用 SDK 的所有信息都做同样的事情;我还没有找到一个将存档提取到多个文件的工具。

我得出以下结论:

将 .7z 存档解压到文件夹中不是 C# LZMA SDK 提供的功能。

恐怕我还没有找到任何确凿的证据,就像博客post直接说的那样。

不过,我找到了a description of the .7z file format。此描述没有提供足够的技术细节来制作可行的解决方案,但它包含有关格式的某些事实:

In this section I will explain how the previous example is stored in the data Structured[...]

First up is the ​Header ​this contains just two variables: ​FileInfo​ and ​StreamsInfo.

FileInfo​ is probably the easiest structure to understand. It contains the name of each file in the 7z and a couple of other flags defining if the file is actually a directory or if the file is an empty file.

据我所知,此结构并未反映在 C# SDK 的代码中。结合 Decoder.Code() 方法不适用于目录路径或多个输出 Stream 的信息,这是一个强有力的指标。

因此,LZMA SDK 的 C# 部分似乎仅处理单个文件的加密和解密,或链接文档中所称的“压缩流”。 .7z 存档格式算作一种单独的格式,它最终是一种包含任意数量压缩流的结构。

这些流实际上可以以任意方式组合;例如一个存档可以包含用 LZMA 压缩的文件 A 和 B,然后将文件 C 添加到组合中并用另一种算法再次压缩它。这使得解压.7z压缩包变得更加复杂,这使得SDK中没有提供此功能更加遗憾。