Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Creating a ZIP File In Memory - .NET Framework

RSS
Modified on Tue, Dec 02, 2014, 1:02 PM by Administrator Categorized as ·Net Framework
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

public class ZipEngine
{
    public byte[] ZipInMemory(Dictionary<string, byte[]> files)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                foreach (var file in files)
                {
                    var entry = archive.CreateEntry(file.Key);

                    using (var entryStream = entry.Open())
                    {
                        entryStream.Write(file.Value, 0, file.Value.Length);
                    }
                }
            }

            return memoryStream.ToArray();
        }
    }

}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.