C# ZipFile中的流为空

C# ZipFile中的流为空,c#,dotnetzip,C#,Dotnetzip,我正在尝试解压缩一个包含单个txt文件的zip文件。但我必须处理错误的流或其他东西,因为输出是一个空字符串 content = new StreamReader(ms).ReadToEnd(); // content is "" 我使用DotNetZip开源软件包。你知道这里少了什么吗 Attachment a = (from x in mail.Attachments.OfType<Attachment>() where !string.IsNullOrEmpty(x.Bo

我正在尝试解压缩一个包含单个txt文件的zip文件。但我必须处理错误的流或其他东西,因为输出是一个空字符串

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;
附件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”:
MemoryStream ms=新的MemoryStream();
使用(ZipFile zip=ZipFile.Read(a.RawBytes))
{
foreach(拉链式拉链)
e、 提取物(ms);
}
content=new StreamReader(ms).ReadToEnd();//内容为“”
打破
违约:
content=newstreamreader(newmemoryStream(a.RawBytes)).ReadToEnd();
打破

我过去遇到过一些类似的问题,通过设置属性解决了这些问题

Position = 0;

在使用流内容之前。

我过去遇到过一些类似的问题,通过设置属性解决了这些问题

Position = 0;

在使用流内容之前。

添加ms.Position=0;就在ReadToEnd()之前。p位置位于调用e.Extract之后的最后一个字节。通过将位置设置回0,您可以读取流开头的所有字节。添加ms.Position=0;就在ReadToEnd()之前。位置位于调用e.Extract后的最后一个字节。通过将位置设置回0,您可以读取流开头的所有字节。谢谢。就是这样。谢谢。就是这样。