C# 使用 .NET 4.5 解压缩受密码保护的 ZIP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13160490/
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-08-10 01:49:24  来源:igfitidea点击:

Decompressing password-protected ZIP files with .NET 4.5

c#.netzip.net-4.5

提问by So Many Goblins

Microsoft introduces improvements for ZIP file handling in .NET 4.5 in the System.IO.Compressionnamespace. Namely the classes ZipArchiveand ZipFile. However, I have not yet seen a way to use native .NET ZIP file handling for password protected files. Is there a way to achieve this? (I am aware that there are pretty good 3rd party zip file libraries, that is not the question.)

Microsoft 在System.IO.Compression命名空间中对 .NET 4.5 中的 ZIP 文件处理进行了改进。即类ZipArchiveZipFile。但是,我还没有看到对受密码保护的文件使用本机 .NET ZIP 文件处理的方法。有没有办法实现这一目标?(我知道有非常好的 3rd 方 zip 文件库,这不是问题。)

采纳答案by Oliver

Unfortunately not. There is no support within the .Net Framework 4.5 for password protected zip files. In this case you have to switch to one of the well known 3rd party libraries.

不幸的是没有。.Net Framework 4.5 不支持受密码保护的 zip 文件。在这种情况下,您必须切换到著名的 3rd 方库之一。

回答by Micah Armantrout

In looking at the methodsprovided by the 4.5 framework there is not a method that allows passwords with zip files. As mentioned in your question 3rd party is going to be your best bet.

在看的方法由4.5框架提供没有允许与ZIP文件的密码的方法。正如您在问题中提到的,第 3 方将是您最好的选择。

回答by Jonesome Reinstate Monica

The ionic method is awesome. I tried three other approaches, and it is by far the best. Don't waste time, just use it.

离子方法很棒。我尝试了其他三种方法,这是迄今为止最好的。不要浪费时间,只需使用它。

https://dotnetzip.codeplex.com/wikipage?title=PS-Examples

https://dotnetzip.codeplex.com/wikipage?title=PS-Examples

Supports password encrypted, and other zip options.

支持密码加密和其他 zip 选项。

回答by Nicholas Carey

As pointed out, DotNetZipis your friend. Unpacking your zip file is as easy as

正如所指出的,DotNetZip是您的朋友。解压您的 zip 文件就像

using ( ZipFile archive = new ZipFile( @"c:\path\to\your\password\protected\archive.zip",) )
{
  archive.Password = "your-pass-word-here" ;
  archive.Encryption = EncryptionAlgorithm.PkzipWeak ; // the default: you might need to select the proper value here
  archive.StatusMessageTextWriter = Console.Out;

  archive.ExtractAll( @"c:\path\to\unzip\directory\", ExtractExistingFileAction.Throw ) ;
}

In my experience, DotNetZip runs about as fast as Info-Zip'sopen source unziputility and uses roughly the same amount of memory.

根据我的经验,DotNetZip 的运行速度与Info-Zip 的开源解压缩实用程序一样快,并且使用的内存量大致相同。



Edited To Note:DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still available at Codeplex. It looks like the code has migrated to Github:

编辑为注意:DotNetZip 曾经住在 Codeplex。Codeplex 已关闭。旧档案仍可在 Codeplex 获得。看起来代码已经迁移到 Github:



回答by user1253917

There does not appear to be any support for password protected zip files in the native .net 4.5 library, similar to how there does not appear to be support in windows explorer, even with Windows 10! Some people have reported that they have zip corruption issues using the 3rd party DotNetLib, so make sure you extensively test if you do go down that path or try SharpZipLibinstead.

本机 .net 4.5 库中似乎不支持受密码保护的 zip 文件,类似于 Windows 资源管理器中似乎不支持的方式,即使在 Windows 10 中也是如此!有些人报告说他们在使用 3rd 方 DotNetLib 时遇到了 zip 损坏问题,因此请确保您进行了广泛的测试,如果您确实沿着这条路走下去,或者尝试使用SharpZipLib

回答by Alberto Silva

For those targeting .Net Standard 2.0, SharpZipLib does a great work, handling gracefully extraction of in memory password protected zip files to byte[].

对于那些以 .Net Standard 2.0 为目标的人来说,SharpZipLib 做得很好,可以优雅地将内存中受密码保护的 zip 文件提取到 byte[]。

https://github.com/icsharpcode/SharpZipLib

https://github.com/icsharpcode/SharpZipLib

Tried Ionic for the same scenario but was enable to extract files with the ZipInputStream, which generated corrupt extracted byte arrays.

为相同的场景尝试了 Ionic,但可以使用 ZipInputStream 提取文件,这会生成损坏的提取字节数组。