Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple files in one MemoryStream?

Is it possible to save a list of files into one MemoryStream and save the files later back to the disc?

like image 832
jwillmer Avatar asked Nov 29 '11 14:11

jwillmer


1 Answers

Well yeah, there are a few ways of doing this but one would be to do something like this:

class MyFile
{
    public byte[] Data;
    public string FileName;
}

List<MyFile> files = GetFiles();
using (MemoryStream stream = new MemoryStream())
{
    // Serialise
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, files);

    // Deserailise
    stream.Position = 0;
    List<MyFile> deserialisedFiles = (List<MyFile>)formatter.Deserialize(stream);
    SaveFiles(deserialisedFiles);
}

Where you should be able to figure out roughly the implementation of SaveFiles and GetFiles. I'm not entirely clear why you want to do this though.

like image 195
Justin Avatar answered Oct 17 '22 18:10

Justin