Microsoft introduces improvements for ZIP file handling in .NET 4.5 in the System.IO.Compression namespace. Namely the classes ZipArchive and 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.)
-
1Have you looked at DotNetZip Library..? here is a link and it has lots of examples http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples – MethodMan Oct 31 '12 at 14:46
-
Good Question very direct for what your looking for! – Micah Armantrout Oct 31 '12 at 14:47
7 Answers
As pointed out, DotNetZip is your friend. Unpacking your zip file is as easy as
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's open source unzip utility and uses roughly the same amount of memory.
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:
- https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
- https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/

- 71,308
- 16
- 93
- 135
-
3Heads-up to the readers that SharpZipLib is more active/popular as of 2019: https://github.com/icsharpcode/SharpZipLib – Eric Eskildsen Sep 11 '19 at 15:01
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
Supports password encrypted, and other zip options.

- 6,618
- 11
- 65
- 112
In looking at the methods provided 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.

- 6,781
- 4
- 40
- 66
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 SharpZipLib instead.

- 41
- 1
For those targeting .Net Standard 2.0, SharpZipLib does a great work, handling gracefully extraction of in memory password protected zip files to byte[].
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.

- 382
- 3
- 12
I found the way is very simple to unzip in c#
Install PM (you can find new version if available!)
Install-Package Ionic.Zip -Version 1.9.1.8
Code C#
string zipFile = @"C:\Users\Fesslersoft\Desktop\ZipTest\Test.zip";
string targetDirectory = @"C:\Users\Fesslersoft\Desktop\ZipTest\";
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(zipFile))
{
zip.Password = "1234";
zip.ExtractAll(targetDirectory, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}
Console.WriteLine("Zip file has been successfully extracted.");
Console.Read();
Source: https://codesnippets.fesslersoft.de/extract-a-password-protected-zip-file-using-dotnetzip/

- 426
- 5
- 12