Search code examples
c#filestreamzipmemorystream

How to go from byte[], to MemoryStream, Unzip, then write to FileStream


I am unsure what I am doing wrong. The files that I create after grabbing a byte[] (which is emailAttachment.Body) and passing it to the method ExtractZipFile, converting it to MemoryStream and then unzipping it, returning it as a KeyValuePair and then Writing to a file using FileStream.

However when I go to open the new created files there is an error in opening them. They are not able to be opened.

The below are in the same class

using Ionic.Zip;

var extractedFiles = ExtractZipFile(emailAttachment.Body);

foreach (KeyValuePair<string, MemoryStream> extractedFile in extractedFiles)
{                               
    string FileName = extractedFile.Key;
    using (FileStream file = new FileStream(CurrentFileSystem + 
    FileName.FileFullPath(),FileMode.Create, System.IO.FileAccess.Write))
    {
        byte[] bytes = new byte[extractedFile.Value.Length];
        extractedFile.Value.Read(bytes, 0, (int) xtractedFile.Value.Length);
        file.Write(bytes,0,bytes.Length);
        extractedFile.Value.Close();
     }
}


private Dictionary<string, MemoryStream> ExtractZipFile(byte[] messagePart)
{
    Dictionary<string, MemoryStream> result = new Dictionary<string,MemoryStream>();
    MemoryStream data = new MemoryStream(messagePart);
    using (ZipFile zip = ZipFile.Read(data))
    {
        foreach (ZipEntry ent in zip)
        {
            MemoryStream memoryStream = new MemoryStream();
            ent.Extract(memoryStream);
            result.Add(ent.FileName,memoryStream);
        }   
    }
    return result;
}

Is there something I am missing? I do not want to save the original zip file just the extracted Files from MemoryStream. What am I doing wrong?


Solution

  • After writing to your MemoryStream, you're not setting the position back to 0:

    MemoryStream memoryStream = new MemoryStream();
    ent.Extract(memoryStream);
    result.Add(ent.FileName,memoryStream);
    

    Because of this, the stream position will be at the end when you try to read from it, and you'll read nothing. Make sure to rewind it:

    memoryStream.Position = 0;
    

    Also, you don't have to handle the copy manually. Just let the CopyTo method take care of it:

    extractedFile.Value.CopyTo(file);