适当压缩利用GZIP流和内存流的CSV [英] Properly Compressing a CSV utilizing GZIP Stream and Memory Stream

查看:896
本文介绍了适当压缩利用GZIP流和内存流的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我利用压缩和GZIPStream一个的MemoryStream CSV文件,并注意到一些奇怪的结果文件。这似乎是CSV不能正确识别。这示出了当该文件被附加到电子邮件,但是当保存了windows桌面上正常工作



下面是当前片段处理gzip的部分:



  GZipStream gStream = NULL; 
MemoryStream的Mstream工具= NULL;
MemoryStream的mStream2 = NULL;

{
如果(attachment.Length大于0)
{
Mstream工具=新的MemoryStream();

gStream =新GZipStream(Mstream工具,CompressionMode.Compress);
字节[]字节= System.Text.Encoding.UTF8.GetBytes(attachment.ToString());
gStream.Write(字节,0,bytes.Length);
gStream.Close();

mStream2 =新的MemoryStream(mStream.ToArray());
扣押emailAttachement =新的附件(mStream2,myGzip.csv.gz,应用程序/ x-Gzip已);
mailMessage.Attachments.Add(emailAttachement);
}

}


解决方案

所有参考答案没有工作。找到答案在这里:




其中的一个限制是,你不能到您在存档放置文件给出一个名称




http://msdn.microsoft.com/en-us/magazine/cc163727.aspx


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:

One of the limitations is that you cannot give a name to the file that you place in the archive.

http://msdn.microsoft.com/en-us/magazine/cc163727.aspx

这篇关于适当压缩利用GZIP流和内存流的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆