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.
If someone can help me with this I would be very thankful.
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.