I'm trying to download a tar file via an HTTP GET request, where the file is assembled in memory from data stored in SQL Server (Filestream) as varbinary(max) columns. My goal is to construct the tar file by reading these binary files and streaming them to the client.
I am currently using .NET 8 and wondering if I can leverage the native TarWriter/TarEntry classes (from System.Formats.Tar) to accomplish this, or if I must rely on third-party libraries like SharpZipLib.
Here's my pseudocode:
// Create a MemoryStream to store the .tar file in memory
using var tarStream = new MemoryStream();
await using var tarWriter = new TarWriter(tarStream);
// Open a connection to SQL Server
await using var connection = new SqlConnection("YourConnectionString");
await connection.OpenAsync();
var command = new SqlCommand("SELECT FileName, FileData FROM FilesTable", connection);
await using var reader = command.ExecuteReader();
while (reader.Read())
{
var fileName = reader.GetString(0);
var fileDataStream = reader.GetStream(1);
// How to construct TarEntry from a stream?
tarWriter.WriteEntry(fileName, fileDataStream);
}
// Then write the tarStream to response stream.
If anyone has experience or insights on handling streams with TarEntry in this scenario, particularly when stream-based file data is coming directly from SQL Server, your feedback would be much appreciated.