【问题标题】:Unzip a mail attachment解压缩邮件附件
【发布时间】:2013-07-08 07:31:28
【问题描述】:

下面这行给出了问题

content = new StreamReader(new GZipStream(new MemoryStream(a.RawBytes), CompressionMode.Decompress)).ReadToEnd();

InvalidDataException 发生:GZip 标头中的幻数不是 正确的。确保您在 GZip 流中。

我不能将附件转换为字节数组还是我做错了什么?

Attachment a = (from x in mail.Attachments.OfType<Attachment>()
   where !string.IsNullOrEmpty(x.Body) || x.RawBytes != null
   select x).FirstOrDefault();

AttachmentName = a.Name;
string AttachmentType = a.Name.Substring(a.Name.Length - 3, 3).ToUpper();

switch (AttachmentType)
{
   case "ZIP":
      content = new StreamReader(new GZipStream(new MemoryStream(a.RawBytes), CompressionMode.Decompress)).ReadToEnd();
      break;
   default:
      content = new StreamReader(new MemoryStream(a.RawBytes)).ReadToEnd();
      break;
}

【问题讨论】:

  • Gzip 标头包含一个幻数(文件格式签名),这似乎不是 Gzip 文件,您尝试解压缩...
  • 压缩时是否在实际字节数组中添加了额外的字节?

标签: c# gzipstream


【解决方案1】:

GZip 文件与 Zip 文件不同。您需要 System.IO.Compression.ZipFile 或 ZipArchive。

【讨论】:

  • 谢谢。看起来这就是解决方案。不幸的是,这给我留下了另一个问题,因为该程序位于 .NET 3.5 中,而 System.IO.Compression.ZipFile 仅是 .NET 4.5。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
相关资源
最近更新 更多