Stream.CopyTo not copying any stream data

Herdo picture Herdo · Aug 4, 2014 · Viewed 12.8k times · Source

I'm having an issue with copying data from a MemoryStream into a Stream inside a ZipArchive. The following is NOT working - it returns only 114 bytes:

GetDataAsByteArray(IDataSource dataSource)
{
    using (var zipStream = new MemoryStream())
    {
        using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
        {
            var file = archive.CreateEntry("compressed.file");
            using (var targetStream = file.Open())
            {
                using (var sourceStream = new MemoryStream())
                {
                    await dataSource.LoadIntoStream(sourceStream);
                    sourceStream.CopyTo(targetStream);
                }
            }
        }
        var result = zipStream.ToArray();
        zipStream.Close();
        return result;
    }
}

However, using the implementation below for the "copy"-process, all 1103 bytes are written to the array/memory stream:

await targetStream.WriteAsync(sourceStream.ToArray(), 0, (int) sourceStream.Length);

I'm wondering why the CopyTo yields less bytes. Also I'm feeling unsecure with the cast to Int32 in the second implementation.

FYI: Comparing the byte array: It looks like only the header and footer of the zip file were written by the first implementation.

Answer

Hans Passant picture Hans Passant · Aug 4, 2014

Stream.CopyTo() starts copying from the stream's current Position. Which probably isn't 0 after that LoadIntoStream() call. Since it is a MemoryStream, you can simply fix it like this:

    await dataSource.LoadIntoStream(sourceStream);
    sourceStream.Position = 0;
    sourceStream.CopyTo(targetStream);