Frage

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;
War es hilfreich?

Lösung

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

Position = 0;

before using the stream content.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top