I am compressing a CSV file utilizing GZIPStream and MemoryStream, and noticing something weird with the result file. It seems like the CSV is not properly recognized. This shows when the file is attached to an email, but works fine when saved on a windows desktop.
Here is the current snippet handling the gzip portion:
GZipStream gStream = null;
MemoryStream mStream = null;
MemoryStream mStream2 = null;
try
{
if (attachment.Length > 0)
{
mStream = new MemoryStream();
gStream = new GZipStream(mStream, CompressionMode.Compress);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(attachment.ToString());
gStream.Write(bytes, 0, bytes.Length);
gStream.Close();
mStream2 = new MemoryStream(mStream.ToArray());
Attachment emailAttachement = new Attachment(mStream2, "myGzip.csv.gz", "application/x-Gzip");
mailMessage.Attachments.Add(emailAttachement);
}
}
All the suggested answers did not work. Found the answer here:
http://msdn.microsoft.com/en-us/magazine/cc163727.aspx
GZipStream does NOT create a zip archive; it simply implements the compression algorithm.
See this MSDN sample for creating a zip file: http://msdn.microsoft.com/en-us/library/ywf6dxhx.aspx
I was able to gzip compress and send a csv using the code below. GZipStream doesn't complete writing until its Close() method is called. This happens when the using block that creates gzipStream is completed. Even though the stream output is also closed once that using block is completed, the data can still be retrieved from the output stream using the ToArray() or GetBuffer() methods. Please see this blog entry for more information.