Compression

Overview

VersaTul.Compression provides a small abstraction over System.IO.Compression for zipping and unzipping in-memory files.

The package centers around ZipStream objects, which pair a file name, content type, and MemoryStream so higher-level workflows can archive generated files without dealing directly with zip entry wiring.

When To Use This Package

Use this package when you want to:

  1. Package one or many in-memory files into a zip archive.

  2. Extract zip archives back into named in-memory streams.

  3. Keep compression concerns separate from file generation logic.

  4. Support workflows that generate exports before saving, emailing, or uploading them.

Installation

Install the package with the .NET CLI:

dotnet add package VersaTul.Compression

Or with the Package Manager Console:

PM> NuGet\Install-Package VersaTul.Compression -Version latest

Core Types And Concepts

ZipStream

Carries the file name, content type, and in-memory content to archive.

IArchiver and Archiver

Write individual ZipStream instances into a ZipArchive.

IZipper and Zipper

Create zip archives from one or many ZipStream objects and can also unzip archive streams.

Key Capabilities

  1. Zip a single ZipStream or a collection of them.

  2. Choose a specific CompressionLevel when creating the archive.

  3. Unzip archive streams back into ZipStream instances.

  4. Filter extracted entries during unzip with a predicate.

Basic Example

using System.IO;
using VersaTul.Compression;

IArchiver archiver = new Archiver();
IZipper zipper = new Zipper(archiver);

var zipStream = new ZipStream
{
    ContentType = "text/csv",
    FileName = "people.csv",
    Stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Name,Age\nJane,42"))
};

using var archive = zipper.Zip(zipStream);

Unzip Example

using var archiveStream = File.OpenRead("exports.zip");

var files = zipper.Unzip(archiveStream, entryName => entryName.EndsWith(".csv"));

foreach (var item in files)
{
    Console.WriteLine(item.FileName);
}

Notes

  1. ZipStream.Stream is a MemoryStream, so this package is best suited to in-memory export workflows.

  2. The package does not decide where archives are stored or sent; pair it with Handler File or Collection Streamers depending on your workflow.