2012-07-23 65 views
3

我想压缩XML树并将其用作电子邮件附件。带附件的电子邮件发送成功,但创建的zip文件总是损坏 - 它不是有效的zip文件,但包含二进制数据。为电子邮件附件压缩MemoryStream

问题是重建如下,具体参见BuildAttachment()

static void Main(string[] args) 
{ 
    try 
    { 
     var report = new XElement("Report", 
      new XElement("Product", 
       new XElement("ID", "10000001"), 
       new XElement("Name", "abcdefghijklm"), 
       new XElement("Group", "nopqrstuvwxyz") 
      ) 
     ); 
     var mailMessage = BuildMessage(report); 
     EmailMessage(mailMessage); 
     Thread.Sleep(10000); 
    } 
    catch (Exception e) { Console.WriteLine(e.Message); } 
} 
static MailMessage BuildMessage(XElement report) 
{ 
    string from = "[email protected]"; 
    string to = "[email protected]"; 
    var message = new MailMessage(from, to, "Subject text", "Body text"); 

    var attachment = BuildAttachment(report); 
    message.Attachments.Add(attachment); 

    return message; 
} 
static Attachment BuildAttachment(XElement report) 
{ 
    var inStream = new MemoryStream(); 
    report.Save(inStream); 
    inStream.Position = 0; 

    var outStream = new MemoryStream(); 
    var compress = new DeflateStream(outStream, CompressionMode.Compress); 
    inStream.CopyTo(compress); 

    outStream.Position = 0; 
    return new Attachment(outStream, "report.zip", "application/zip"); 
} 
static void EmailMessage(MailMessage message) 
{ 
    var smtpClient = new SmtpClient("127.0.0.1"); 
    smtpClient.SendCompleted += SendCompletedCallback; 
    smtpClient.SendAsync(message, null); 
} 
static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
{ 
    if (e.Error != null) 
     Console.WriteLine(e.Error.ToString()); 
} 

为了把问题情境:这是一个Windows服务应用程序的一部分,所以我不想在磁盘上创建文件,以及电子邮件消息还包含xslt转换的xml树的替代视图,所以我不想要一个完全不同的解决方案。

任何建议为什么压缩文件损坏?

+0

'DeflateStream =邮编File'文件(.zip可以有多种类型的压缩)的 – user7116 2012-07-23 14:20:24

+0

可能重复[如何使用System.IO.Compression读/写ZIP文件?(HTTP:/ /stackoverflow.com/questions/184309/how-to-use-system-io-compression-to-read-write-zip-files) – user7116 2012-07-23 14:24:46

回答

4

您并未创建有效的zip文件,只是创建一个压缩流并将其写入具有*.zip扩展名的文件。您应该使用.NET zip库而不是DeflateStream

您可以使用类似DotNetZip Library

DotNetZip是一个易于使用,快速,免费类库和工具集 操作zip文件或文件夹。 Zip和Unzip很简单:使用DotNetZip,用VB编写的.NET应用程序,C# - 任何.NET语言 - 都可以轻松创建,读取,提取或更新zip文件。适用于单声道或MS .NET。

如果你有限制,不能使用外部库,你可以尝试使用GZipStream并与*.gz延长其将通过常见的压缩工具,支持添加附件。

作为将来参考的有用信息,.NET 4.5将终于通过ZipArchive类引入了对zip压缩归档的本地支持。

4

为了将来的参考,在接受的答案中建议的替代GZipStream方法来生成.gz存档是通过如下更改上述代码来实现的。

static Attachment BuildAttachment(XElement report) 
{ 
    var inStream = new MemoryStream(); 
    report.Save(inStream); 
    inStream.Position = 0; 

    var outStream = new MemoryStream(); 
    using (var compress = new GZipStream(outStream, CompressionMode.Compress)) 
    { 
     inStream.CopyTo(compress); 
     compress.Close(); 
    } 

    var ms = new MemoryStream(outStream.ToArray()); 
    Attachment attachment = new Attachment(ms, "report.xml.gz", "application/gzip"); 
    return attachment; 
} 

using块和ToArray()呼叫是必需的。

7

我知道这是一个古老的问题,但是当我寻找相同的东西时它就出现了。

这是我的溶液添加用System.IO.Compression.ZipArchive (requires .NET 4.5 or higher)压缩(ZIP)附着[基于的acraig5075答案]:

byte[] report = GetSomeReportAsByteArray(); 
string fileName = "file.txt"; 

using (MemoryStream memoryStream = new MemoryStream()) 
{ 
    using (ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Update)) 
    { 
     ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry(fileName); 
     using (StreamWriter streamWriter = new StreamWriter(zipArchiveEntry.Open())) 
     { 
      streamWriter.Write(Encoding.Default.GetString(report)); 
     } 
    } 

    MemoryStream attachmentStream = new MemoryStream(memoryStream.ToArray()); 

    Attachment attachment = new Attachment(attachmentStream, fileName + ".zip", MediaTypeNames.Application.Zip); 
    mail.Attachments.Add(attachment); 
} 
0

感谢“尼尔斯R”的回答,我已经创建了一个静态C#函数用于管理附件,并在大于1Mb的情况下将其压缩。希望这有助于任何人。

public static Attachment CreateAttachment(string fileNameAndPath, bool zipIfTooLarge = true, int bytes = 1 << 20) 
{ 
    if (!zipIfTooLarge) 
    { 
     return new Attachment(fileNameAndPath); 
    } 

    var fileInfo = new FileInfo(fileNameAndPath); 
    // Less than 1Mb just attach as is. 
    if (fileInfo.Length < bytes) 
    { 
     var attachment = new Attachment(fileNameAndPath); 

     return attachment; 
    } 

    byte[] fileBytes = File.ReadAllBytes(fileNameAndPath); 

    using (var memoryStream = new MemoryStream()) 
    { 
     string fileName = Path.GetFileName(fileNameAndPath); 

     using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create)) 
     { 
      ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry(fileName, CompressionLevel.Optimal); 

      using (var streamWriter = new StreamWriter(zipArchiveEntry.Open())) 
      { 
       streamWriter.Write(Encoding.Default.GetString(fileBytes)); 
      } 
     } 

     var attachmentStream = new MemoryStream(memoryStream.ToArray()); 
     string zipname = $"{Path.GetFileNameWithoutExtension(fileName)}.zip"; 
     var attachment = new Attachment(attachmentStream, zipname, MediaTypeNames.Application.Zip); 

     return attachment; 
    } 
}