Search code examples
c#asp.netasp.net-mvc-3dotnetzip

Cannot read zip file from HttpInputStream using DotNetZip 1.9


I am trying to use DotNetZip 1.9 to read an uploaded zip file in Asp.Net MVC 3. I already verified that the HttpPostedFileBase object I receive is fine. I can save it to disk and then unzip it. However, saving to disk first seemed wasteful since I should be able to unzip from memory directly.

From MSDN, the HttpPostedFileBase.InputStream Property "gets a Stream object that points to an uploaded file to prepare for reading the contents of the file".

According to DetNetZip references, ZipFile.Read() can accept a Stream object. So I tried it and DotNetZip throws a BadReadException. I have attached screen shots showing the problem.

Problem unzipping from HttpInputStream Problem unzipping from HttpInputStream

Value of the InputStream, Length matches that of the uploaded zip file Value of the InputStream, Length matches that of the uploaded zip file

Help anyone? Thx


Solution

  • I suspect that the ZipFile.IsZipFile method call has advanced your InputStream position and when you try to read it later it is no longer a valid zip file since the stream position has moved. Try sticking a

    fileData.InputStream.Position = 0;
    

    just after verifying that the stream is a valid zip file and just before the using clause in which you attempt to read it. This will reset the stream position at the beginning.