C# 解压缩邮件附件

C# 解压缩邮件附件,c#,gzipstream,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

下面一行给出了一些问题

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;
}
附件a=(来自mail.Attachments.OfType()中的x)
其中!string.IsNullOrEmpty(x.Body)| | x.RawBytes!=null
选择x).FirstOrDefault();
AttachmentName=a.名称;
string AttachmentType=a.Name.Substring(a.Name.Length-3,3).ToUpper();
开关(附件类型)
{
案例“ZIP”:
content=newstreamreader(newgzipstream(newmemorystream(a.RawBytes),CompressionMode.Decompress)).ReadToEnd();
打破
违约:
content=newstreamreader(newmemoryStream(a.RawBytes)).ReadToEnd();
打破
}

GZip文件与Zip文件不是一回事。您需要System.IO.Compression.ZipFile或ZipArchive。

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

Gzip头包含一个幻数(文件格式签名),而这似乎不是一个Gzip文件,您正在尝试解压它……压缩时是否向实际的字节数组添加了任何额外的字节?Gzip头包含一个幻数(文件格式签名)这似乎不是一个Gzip文件,你正在尝试解压…你在压缩时是否在实际的字节数组中添加了额外的字节?谢谢。看来这就是解决办法。不幸的是,这给我留下了另一个问题,因为程序在.NET 3.5中,System.IO.Compression.ZipFile仅在.NET 4.5中。谢谢。看来这就是解决办法。不幸的是,这给我留下了另一个问题,因为程序在.NET3.5中,System.IO.Compression.ZipFile仅在.NET4.5中。