Curiosus.Archiver
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 aTempFileStream;ZipFilesToStreamAsync— zip a list of files into aTempFileStream(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
- Curiosus.Archiver.SharpZip —
IArchiverimplementation based on SharpZipLib - Curiosus.Tools — temp files (
TempFileStream,TempFileOptions) - Curiosus.Utils — all packages