0

I am trying to unzip a zip-file that contains a single txt-file. But I must be handling the streams wrong or something because the output is an empty string.

content = new StreamReader(ms).ReadToEnd(); // content is ""

I use the DotNetZip open source package. Any idea what is missing here?

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;
Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
  • 3
    Add ms.Postion = 0 ; just before you ReadToEnd(). Thep osition is at the last byte just after you called e.Extract. By setting the Position back to 0 you are able to Read all bytes from the start of the stream. – rene Jul 08 '13 at 09:27

1 Answers1

3

I had some similar issues in the past, solved by setting the property

Position = 0;

before using the stream content.

Oscar
  • 13,594
  • 8
  • 47
  • 75