timeout exception on memorystream

Jan 14, 2011 at 4:43 PM

Hi,

I am trying to unzip a file into a memorystream and I always get a timeout exception on memorystream.  What do I do wrong?

                TOTO NP = null;
                MemoryStream MemStream = new MemoryStream();

                using (ZipFile zip = ZipFile.Read(Path + ZipFileName))
                {
                    ZipEntry e = zip[FileName];
                    e.ExtractWithPassword(MemStream, "!password");
                    NP = LoadFromFile(MemStream);
                }

 

        public static TOTO LoadFromFile(MemoryStream fileName)
        {
            System.IO.StreamReader sr = null;
            try
            {
                sr = new System.IO.StreamReader(fileName, Encoding.GetEncoding(1252));

/// Here I can never read the buffer from sr, because it is always empty, which when I look at the object I see all the data in the buffer.  But when I get to the

/// Base class I get the timeout exception on reading and writing was thrown...


                string xmlString = sr.ReadToEnd();
                sr.Close();
                return Deserialize(xmlString);
            }
            finally
            {
                if ((sr != null))
                {
                    sr.Dispose();
                }
            }
        }

Coordinator
Jan 16, 2011 at 2:38 PM
Edited Jan 16, 2011 at 2:44 PM

I don't think the timeout you're seeing is related to extracting into the MemoryStream.  I don't know, but I would guess the timeout is related to reading from the MemoryStream.  This may be because you are trying to read from the end of the MemoryStream, where there is no data.  I'm not sure, but you may need to call Seek() on the MemoryStream in order to reset the cursor on it, before attempting to read it, but after having extracted into it. That's the first thing I would look into.

On the other hand I'd also use a different design than you have selected here, to eliminate the MemoryStream completely.  The way you have it, you read from the zip file and write into a temporary buffer (the MemoryStream), then later you read from the temporary buffer.  With the code you've shown, there's no need for the temporary buffer.   To eliminate it, obtain a readable stream directly from the zip file.  Instead of calling ZipEntry.ExtractWithPassword(), call ZipEntry.OpenReader() which gives you a readable stream.  You can then wrap a StreamReader around that stream.   

Also,

  • I suggest you rename your variables and methods to reflect their purpose.  A MemoryStream called "fileName" is just a recipe for confusion. A method that reads from a stream, but is named LoadFromFile() is also a cause of confusion.
  • why not employ a using{} clause instead of the try...finally?  It cleans up the code, which is the purpose of the using clause.

The resulting code would look something like this, I guess:

      public void Run()
      {
          TOTO NP = null;
          using (ZipFile zip = ZipFile.Read(Path + ZipFileName))
          {
              ZipEntry e = zip[FileName];
              NP = LoadFromStream(e.OpenReader("!password"));
          }
      }
 

      public static TOTO LoadFromStream(Stream s)
      {
          using (var sr = new System.IO.StreamReader(s, Encoding.GetEncoding(1252)))
          {
              return Deserialize(sr.ReadToEnd());                
          }
      }

Jan 17, 2011 at 1:50 PM

Thank you for you comments Cheeso.