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

namespace Valg.EVA.Skanning.Tools
{
    public static class ZipBuilder
    {

        public struct FileDefinition
        {
            public string FileName { get; set; }

            public byte[] FileContent { get; set; }

            public FileDefinition(string fileName, byte[] fileContent)
            {
                FileName = fileName;
                FileContent = fileContent;
            }

            public FileDefinition(string fileName, string fileContent)
            {
                FileName = fileName;
                FileContent = System.Text.Encoding.ASCII.GetBytes(fileContent);
            }
        }

        public static byte[] CreateZipFile(List<FileDefinition> filesToZip)
        {
            byte[] zipFile;

            using (var outputMemStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(outputMemStream, ZipArchiveMode.Create, true))
                {
                    foreach (var file in filesToZip)
                    {
                        var fileEntry = archive.CreateEntry(file.FileName);
                        using (var writer = new BinaryWriter(fileEntry.Open()))
                        {
                            writer.Write(file.FileContent);
                        }
                    }
                }
                zipFile = outputMemStream.ToArray();
            }
            return zipFile;
        }
    }
}
