DEV Community

Cover image for Compress files in memory (.zip) using C#
Ricardo
Ricardo

Posted on • Originally published at rmauro.dev on

Compress files in memory (.zip) using C#

Compress files in memory (.zip) using C#

Here is a code snippet of how to compress one or many files to a zip archive in memory using C#.

It works in .Net Core and .Net Full Framework

public static byte[] GetZipArchive(params InMemoryFile[] files)
{
    byte[] archiveFile;
    using (var archiveStream = new MemoryStream())
    {
        using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
        {
            foreach (var file in files)
            {
                var zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest);

                using var zipStream = zipArchiveEntry.Open();
                zipStream.Write(file.Content, 0, file.Content.Length);
            }
        }

        archiveFile = archiveStream.ToArray();
    }

    return archiveFile;
}
Enter fullscreen mode Exit fullscreen mode

Very simple, right?

Here is a sample Console Application in C# .Net Core.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApp1
{
    class InMemoryFile
    {
        public string FileName { get; set; }

        public byte[] Content { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using var fs = File.OpenRead(@"C:\Users\rmauro\Pictures\cool-computer.jpg");

            var files = new[]
            {
                LoadFromFile(@"C:\Users\rmauro\Pictures\cool-computer.jpg"),
                LoadFromFile(@"C:\Users\rmauro\Pictures\34778.jpg"),
            };
            var result = GetZipArchive(files);

            using var fw = File.OpenWrite(@"C:\Users\rmauro\Pictures\out.zip");

            using var memZip = new MemoryStream(result);
            memZip.CopyTo(fw);

            fw.Close();

            Console.ReadKey();
        }

        static InMemoryFile LoadFromFile(string path)
        {
            using var fs = File.OpenRead(path);
            using var memFile = new MemoryStream();
            fs.CopyTo(memFile);

            memFile.Seek(0, SeekOrigin.Begin);

            return new InMemoryFile() { Content = memFile.ToArray(), FileName = Path.GetFileName(path) };
        }

        public static byte[] GetZipArchive(params InMemoryFile[] files)
        {
            byte[] archiveFile;
            using (var archiveStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
                {
                    foreach (var file in files)
                    {
                        var zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest);

                        using var zipStream = zipArchiveEntry.Open();
                        zipStream.Write(file.Content, 0, file.Content.Length);
                    }
                }

                archiveFile = archiveStream.ToArray();
            }

            return archiveFile;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Hope it helps. Leave a comment.

Top comments (0)