Skip to main content

Curiosus.Archiver

NuGet Downloads Coverage

Abstractions for creating and extracting ZIP archives: the IArchiver interface and the FileNames model. Reference it from code that should not depend on a concrete ZIP library; the implementation is provided by Curiosus.Archiver.SharpZip.

Installation​

dotnet add package Curiosus.Archiver

Usage​

IArchiver can:

  • ZipDirAsync — zip a directory recursively into a temp file and return it as a TempFileStream;
  • ZipFilesToStreamAsync — zip a list of files into a TempFileStream (the stream is positioned at the start);
  • ZipFilesToFileAsync — zip a list of files into the temp directory and return the archive path;
  • UnzipFile — extract an archive into a directory (a new temp directory by default) and return its path.

FileNames pairs the path of a file on disk with the name it gets inside the archive (by default the file name from the path). Duplicate entry names get a numeric suffix.

using Curiosus.Archiver;

public class ReportExporter(IArchiver archiver)
{
public async Task<Stream> ExportAsync(string reportPath, string logPath, CancellationToken ct)
{
var files = new[]
{
new FileNames(reportPath, "report.xlsx"),
new FileNames(logPath),
};

// TempFileStream deletes the archive when it is disposed
return await archiver.ZipFilesToStreamAsync(files, zipFileName: "export.zip", cts: ct);
}
}

The IList<string> overloads of ZipFilesToStreamAsync and ZipFilesToFileAsync are obsolete; use the FileNames overloads.

See also​