How to use the .NET ZipArchive and ZipArchiveEntry classes to extract a file with a PASSWORD

99封情书 提交于 2019-12-12 04:43:18

问题


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");
    }
}

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).

Thanks in advance.


回答1:


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);
  }


来源:https://stackoverflow.com/questions/12899325/how-to-use-the-net-ziparchive-and-ziparchiveentry-classes-to-extract-a-file-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!