Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create ZipFile with password without external libraries C#

As the title says, I need to create a zip file secured with a given password but I have the constraint of don’t use any external library. I have to use only .NET Framework 4.6.1

Currently, I’m creating the file using System.IO.Compression and the code is this:

using (MemoryStream zipStream = new MemoryStream())
{
     using (ZipArchive zipFile = new ZipArchive(zipStream, ZipArchiveMode.Create, false))
     {
          foreach (InlineContent report in reports)
          {
               ZipArchiveEntry zipEntry = zipFile.CreateEntry(report.FileName, CompressionLevel.Optimal);
               using (StreamWriter writer = new StreamWriter(zipEntry.Open()))
               {
                    new MemoryStream(report.Content.ToArray()).WriteTo(writer.BaseStream);
               }
          }
     }
}

This is how I’m generating the file, not how I save it. I understand that it’s not relevant for this put that part of the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If someone can help me with this I would be very thankful.

>Solution :

You can’t. There is a issue asking to add the password support into standard ZipArchive class and the issue is still open: https://github.com/dotnet/runtime/issues/1545

So, the only way to support passwords without using the 3rd-party libraries will be to implement zip format with password support from scratch manually and use this implementation instead of ZipArchive. I don’t think it is possible to elaborate the all details of such an implementation in the SO answer, but you can definitely check out the sources of the existing 3rd-party libraries for reference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading