Create in memory zip from a file
<p>Is DeflateStream supposed to create archived stream that can be stored as standard .zip archive?</p>

<p>I'm trying to create in-memory zip (to be sent remotely) from a local file.
I used a DeflateStream to get a compressed byte array from the file on local disk:</p>

<pre><code>public static byte[] ZipFile(string csvFullPath)
    {
        using (FileStream csvStream = File.Open(csvFullPath, FileMode.Open, FileAccess.Read))
        {
            using (MemoryStream compressStream = new MemoryStream())
            {
                using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionLevel.Optimal))
                {
                    csvStream.CopyTo(deflateStream);
                    deflateStream.Close();
                    return compressStream.ToArray();
                }
            }
        }
    }
</code></pre>

<p>This works great.
However when I dump the resulting bytes to a zip file:</p>

<pre><code>byte[] zippedBytes = ZipFile(FileName);
File.WriteAllBytes("Sample.zip", zippedBytes);
</code></pre>

<p>I cannot open the resulting .zip archive with windows build-in .zip functionality (or with any other 3rd party archive tool).</p>

<p>An alternative I'm planning now is using ZipArchive - however that would require creating temporary files on disk (first copy the file into separate directory, then zip it, then read it into byte array and then delete it)</p>