0
我想解壓縮包含單個txt文件的zip文件。但是我必須處理錯誤的流,因爲輸出是空字符串。ZipFile中的流爲空
content = new StreamReader(ms).ReadToEnd(); // content is ""
我使用DotNetZip開源軟件包。有什麼想法在這裏失蹤?
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":
MemoryStream ms = new MemoryStream();
using (ZipFile zip = ZipFile.Read(a.RawBytes))
{
foreach (ZipEntry e in zip)
e.Extract(ms);
}
content = new StreamReader(ms).ReadToEnd(); // content is ""
break;
default:
content = new StreamReader(new MemoryStream(a.RawBytes)).ReadToEnd();
break;
添加ms.Postion = 0;就在你ReadToEnd()之前。該命令位於您稱爲e.Extract之後的最後一個字節。通過將Position設置回0,您可以從流的開頭讀取所有字節。 – rene
謝謝。有效。 –