I'm fairly new to System.IO streams, and are therefore not entirely sure when and how I should use the different streams.
Let me explain my use-case:
Currently, I have a MS SQL database with a FileStream installation, in which I store FileName
, Byte[]
and Section
for the files. i.e.
public partial class MyFiles {
public int Id { get; set; }
public int Section { get; set; }
public string FileName { get; set; }
public byte[] Data { get; set; }
}
At some point, I want to be able to download all the files that belongs to a specific section. I therefore want to:
- Query the files specific to a section
- Write to a ZipArchive
- Pass the zipped file as a
FileContentResult
I have decided to use a MemoryStream
to achieve this, because its fast and convenient in the sense that I don't have to use the filesystem on the server. The implementation looks as follows:
MemoryStream stream;
using (stream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
{
foreach (MyFiles file in fetchedFiles)
{
var fileEntry = zipArchive.CreateEntry(file.FileName);
using (var entryStream = fileEntry.Open())
{
entryStream.Write(file.Data, 0, file.Data.Length);
}
}
}
}
return new SuccessResult<MemoryStream>(stream);
Everything is working, and I can successfully retreive my zipped files..
However, now I'm starting to doubt this implementation, as this possibly could end up handling files that combined can add up to say, 512MB - 1GB..
The server is really powerful, but obviously I don't want to burn all the memory in this process.
Am I moving in a wrong direction with MemoryStream
and should I ideally consider something else?