本文介绍了如何使用.NET ZipArchive和ZipArchiveEntry类提取带有密码的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码提取zip文件的内容:

I am extracting the contents of a zip file with the following code:

using(ZipArchive zipArchive = new ZipArchive(memoryStream))
{
    foreach (ZipArchiveEntry entry in zipArchive.Entries)
    {
        entry.ExtractToFile("extract.txt");
    }
}

这对于那些没有密码保护的zip文件非常有效,但是,我需要它也能为那些有密码保护的密码工作.

This works perfectly for those zip files which are not password protected, however, I need it to also work for those passwords which are password protected.

我看过其他示例可以使用其他类或其他代码来实现我想要的功能,但是我发现这种方式非常干净,我希望有一个可以设置密码的属性(它不需要比这更困难).

I have seen other samples which can achieve what I want using other classes or other code but I find this way to be very clean and I hope that there is a property where I can set the password (it shouldn't need to be any more difficult than that).

谢谢.

推荐答案

正如Oded所说,内置类不支持密码.您应该尝试使用 DotNetZip 之类的外部库.它是免费,强大的,并且几乎支持您需要的所有内容.

As Oded said, the built-in classes don't support passwords. You should try an external library like DotNetZip. It's free, powerful, and supports just about everything you'd need.

在这种情况下,适合您的示例是:

In this case, the example for you is:

 using (ZipFile zip = ZipFile.Read(ExistingZipFile))
  {
    ZipEntry e = zip["TaxInformation-2008.xls"];
    e.ExtractWithPassword(BaseDirectory, Password);
  }

这篇关于如何使用.NET ZipArchive和ZipArchiveEntry类提取带有密码的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:12