2013-07-08 54 views
0

以下行给出的问题解压缩邮件附件

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

Gzip头部包含一个幻数(文件格式签名),这似乎不是一个Gzip文件,你试图解压缩... –

+0

您是否在压缩时将任何额外的字节添加到实际的字节数组? – Rezoan

回答

4

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

+0

谢谢。看起来就是这样的解决方案。不幸的是,由于该程序在.NET 3.5和System.IO.Compression.ZipFile中仅有.NET 4.5,所以给我带来了另一个问题。 –