解压缩邮件附件

时间:2013-07-08 07:31:28

标签: c# gzipstream

以下行提出了问题

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;
}

1 个答案:

答案 0 :(得分:4)

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

相关问题