I have a directory with a couple of files which I want to send to a client upon request. In order do this I have to somehow combine the files in a single file but I don't want to store this file (even temporarily) in a file system or entirely in memory. In other words I need to create an archive and stream it directly to the client.
A simple candidate for this seems to be a tarball. If I understand correctly a tarball doesn't contain a global file header but just a header for the individual files in the archive:
HeaderFile1-File1-glue-HeaderFile2-File2-finalizer
Assuming this is correct, it would be possible to generate a tarball without having to store it completely in memory or otherwise but I've looked into the libraries SharpCompress and SharpZipLib but neither seem to support something like this.
Before I try and implement it entirely myself two questions:
Is there an easier way to stream directory content to a client.
Does any commonly used library support the streaming of a tarball like I described?
FYI, I'm using ASP.net core 6
As an example using SharpCompress that first loads the tarball entirely in memory:
DirectoryInfo directoryOfFilesToBeTarred = new DirectoryInfo(dir);
FileInfo[] filesInDirectory = directoryOfFilesToBeTarred.GetFiles();
MemoryStream memStream = new MemoryStream();
var options = new TarWriterOptions(SharpCompress.Common.CompressionType.None, finalizeArchiveOnClose: true)
{
LeaveStreamOpen = true
};
using (TarWriter writer = new TarWriter(memStream, options: options))
{
foreach (FileInfo fileToBeTarred in filesInDirectory)
{
writer.Write(fileToBeTarred.Name, fileToBeTarred);
}
}
memStream.Seek(0, SeekOrigin.Begin);
return memStream;