using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace Valg.EVA.Skanning.Tools
{
    public class Zip
    {
        public static byte[] Create(Dictionary<string, byte[]> files)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    foreach (var file in files)
                    {
                        WriteFileToZip(
                            archive,
                            file.Value,
                            file.Key);
                    }
                }

                return memoryStream.ToArray();
            }
        }


        private static void WriteFileToZip(ZipArchive archive, byte[] token, string filename)
        {
            var demoFile = archive.CreateEntry(filename);

            using (var entryStream = demoFile.Open())
            {
                using (var streamWriter = new BinaryWriter(entryStream))
                {
                    streamWriter.Write(token);
                }
            }
        }
    }
}
