简体   繁体   English

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

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

I want my app to use files from a .7z file.我希望我的应用程序使用 .7z 文件中的文件。

I have found some explanations of how to decompress a 7z file .我找到了一些关于如何解压缩 7z 文件的解释 However, they are all about a case I have not yet encountered in the wild: An archive that decompresses to a single file.然而,它们都是关于一个我在野外还没有遇到过的案例:一个解压为单个文件的存档。 I would expect a .7z archive to decompress to an arbitrary amount of files, possibly in arbitrary subfolders.我希望 .7z 存档解压缩为任意数量的文件,可能位于任意子文件夹中。

It seems like it would be possible to do this by including the whole 7zip application and then running that in a process .似乎可以通过包含整个 7zip 应用程序然后在进程中运行它来做到这一点。 However, that seems like unnecessary extra steps, considering there's an official LZMA SDK .但是,考虑到有官方 LZMA SDK ,这似乎是不必要的额外步骤。 Also, it leads to problems concerning cross compatibility.此外,它会导致有关交叉兼容性的问题。

So, how can I unpack a .7z archive into a given folder, using in C# (.NET 3.5)?那么,如何使用 C# (.NET 3.5) 将 .7z 存档解压缩到给定文件夹中?

Additional info from comments来自评论的附加信息

" Please show the code that isn't working. " 请出示无效的代码。
Well, at the moment I'm using the second half of this answer on another question.好吧,目前我正在另一个问题上使用这个答案的后半部分。 However, I'm expecting that to not do what I want - output (just like input ) is a FileStream , so it's clear that this only creates one file.但是,我希望不要做我想做的事情 - output (就像input一样)是一个FileStream ,所以很明显这只会创建一个文件。
It shows that Decoder is the class doing the decompressing, but it doesn't have a method that works with eg a DirectoryInfo , a string path or anything like that - it's all one file in, one file out .它表明Decoder是进行解压缩的类,但它没有一个方法可以与例如DirectoryInfostring路径或类似的东西一起使用 - 它都是一个文件输入,一个文件输出 Every info I found on using the SDK does this very same thing;我在使用 SDK 时发现的每个信息都做同样的事情; I haven't found a single one extracting an archive to several files.我还没有找到一个将存档提取到多个文件的方法。

I have come to the following conclusion:我得出以下结论:

Decompressing a .7z archive into a folder is not a feature provided by the C# LZMA SDK.将 .7z 存档解压缩到文件夹中不是 C# LZMA SDK 提供的功能。

I'm afraid I have not found any hard evidence, like a blog post directly stating it.恐怕我还没有找到任何确凿的证据,就像博客文章直接陈述的那样。

However, I have found a description of the .7z file format .但是,我找到.7z文件格式的说明 This description did not provide enough technical detail to craft a working solution, but it contained certain facts about the 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.首先是 Header 它只包含两个变量: FileInfo 和 StreamsInfo。

FileInfo​ is probably the easiest structure to understand. FileInfo 可能是最容易理解的结构。 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.它包含 7z 中每个文件的名称和一些其他标志,用于定义文件实际上是目录还是文件是空文件。

This structure is not reflected in the code of the C# SDK as far as I can see.据我所知,这种结构并没有反映在 C# SDK 的代码中。 That, combined with the information that the Decoder.Code() methods do not work with a directory path or more than one output Stream is a strong indicator.结合Decoder.Code()方法不适用于目录路径或多个输出Stream是一个强有力的指标。

So it seems that the C# part of the LZMA SDK only handles en- and decryption of a single file, or "Compressed Stream" as it is called in the linked document.因此,LZMA SDK 的 C# 部分似乎只处理单个文件的加密和解密,或在链接文档中称为“压缩流”。 The .7z archive format then counts as a separate format, which is ultimately a structure to contain an arbitrary number of compressed streams. .7z存档格式然后算作一个单独的格式,它最终是一个包含任意数量压缩流的结构。

These streams can actually be combined in, again, arbitrary ways;这些流实际上可以以任意方式组合; eg an archive can contain files A and B compressed with LZMA, then add file C to the mix and compress it again with another algorithm.例如,存档可以包含用 LZMA 压缩的文件 A 和 B,然后将文件 C 添加到混合中并使用另一种算法再次压缩它。 That makes decompressing a .7z archive more complex, which makes it even more of a pity that this functionality is not provided in the SDK.这使得解压缩.7z存档变得更加复杂,这使得 SDK 中没有提供此功能更加遗憾。

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

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