DEV Community

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

Posted on • Originally published at rmauro.dev on

4 1

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more