0

I have to create a ZipArchive and return it. I can add the Entries but when I return it, it comes empty and I don't know why.

Could you help me please?

Here is my code

public static ZipArchive CreateZip(Dictionary<string, Stream> nameAndContent)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Update, true))
        {
            foreach (var i in nameAndContent)
            {
                var file = archive.CreateEntry(i.Key);
                using (var entryStream = file.Open())
                using (var streamWriter = new StreamWriter(entryStream))
                {
                    streamWriter.Write(i.Value);
                }
            }

            return archive;
        }
    }
}
mouldycurryness
  • 69
  • 1
  • 11
jnoguerm
  • 147
  • 1
  • 1
  • 15
  • See [this](https://stackoverflow.com/questions/12347775/ziparchive-creates-invalid-zip-file/12350106#12350106) – huMpty duMpty Jul 12 '19 at 12:02
  • Possible duplicate of [ZipArchive creates invalid ZIP file](https://stackoverflow.com/questions/12347775/ziparchive-creates-invalid-zip-file) – ESG Jul 12 '19 at 12:20
  • `nameAndContent` contains streams already. You need to *copy* data from them to the archive entry, not write them to the stream as if they were strings or integers. Use` Stream.CopyTo()` instead of a StreamWriter. Make sure you set each stream's position to 0 too as `CopyTo` copies from the stream's current location – Panagiotis Kanavos Jul 12 '19 at 12:34

0 Answers0