You have to rewind memory stream before you can read something (after write operation file pointer is that its end):
ms.Seek(0, SeekOrigin.Begin)
ms.Read(buffer, 0, buffer.Length);
That said a MemoryStream
is nothing more than a byte array so you don't even need to allocate and read a new one then this code:
buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
entry.Size = ms.Length;
ms.Close();
zipOutputStream.Write(buffer, 0, buffer.Length);
Can be simply replaced with:
entry.Size = ms.Length;
zipOutputStream.Write(ms.GetBuffer(), 0, ms.Length);
ms.Close();
Final note: if you don't want to use internal MemoryStream
buffer (for any reason) and you want a trimmed copy of it (as you're doing manually) then simply use ToArray()
method like this:
var buffer = ms.ToArray();
ms.Close();
entry.Size = buffer.Length;
zipOutputStream.Write(buffer, 0, buffer.Length);