MemoryStream output to a file c#

I have below code:

string fileName = GetFileName();
using (var stream = new MemoryStream())
{
    using (var sw = new StreamWriter(stream))
    {            
        for (int i = 0; i < 20; i++)
        {
            sw.WriteLine(String.Format("{0};{1};{2};{3};{4};{5}", i.ToString(), (i*2).ToString(), "-_-_-", "-_" + i.ToString() + "_-", "3", "15"));
        }

        // Check if compression needed.
        if (stream.Length > limit)
        {
            sw.Flush();
            stream.Position = 0;

            using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
            {
                absoluteFileName = Path.GetFileName(fileName);
                zipFile.AddEntry(absoluteFileName, stream);

                zipFileName = Path.Combine(Path.GetDirectoryName(fileName), Path.ChangeExtension(absoluteFileName, ZipExtension));
                zipFile.Save(zipFileName);
            }
        }
        else
        {
            // no compression needed
            using (FileStream file = new FileStream(fileName, FileMode.Create, System.IO.FileAccess.Write))
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);
                file.Write(bytes, 0, bytes.Length);
                //stream.Close();
            }
        }
    }

I am trying to generate a plain text file in case memorystream length (in bytes) is no greater than a concrete amount of bytes (limit (see conditional).

If memorystream length is greater than a concrete amount of bytes then I create a zipped file.

My problem is in the else body when I try to write all the content of memory stream into a plain text file when no need to compress (stream.Length <= limit). I get a file of 39bytes but when I open it, it is empty, only new lines within it.

I do this because I do not want to create the file on disk directly in case I need to compress it. So if at the end no need to compress, I write all the streammemory to a file and that is the problem, file is empty, only newlines.

What am I doing wrong?

UPDATE2 I have put below line just before conditional:

 sw.Flush();
 stream.Position = 0;

And now the content is written to file.

Jon Skeet
people
quotationmark

My problem is in the else body

Well you're handling that in a different way to in the first if body.

In the first if body, you're explicitly flushing the writer and rewinding the stream:

sw.Flush();
stream.Position = 0;

You're not doing either of those in the else body, so you won't get any data that's already been written to the stream (no rewind) and there could still be data buffered in the StreamWriter (no flush). That's one problem.

Next, you're checking your "limit" without flushing the StreamWriter, which means there could be a bunch more data meaning you should compress, but you're not. So I suggest you call sw.Flush() before your if statement occurs at all.

Finally, I'd strongly recommend against creating another byte array for no reason - the body of your final using statement can just be:

stream.CopyTo(file);

people

See more on this question at Stackoverflow