Using ZipInputStream with ZipEntry.OpenReader() gives a "Bad Signature" error


// class ZipInputStream
    public ZipEntry GetNextEntry()
        if (_findRequired)
        _inputStream.Seek(_endOfEntry, SeekOrigin.Begin); // fix code here
        _currentEntry = ZipEntry.ReadEntry(_container, !_firstEntry);
        _endOfEntry = _inputStream.Position;
        _firstEntry = true;
        _needSetup = true;
        _findRequired= false;
        return _currentEntry;
Closed Jun 18, 2011 at 5:40 AM by Cheeso
fixed in changeset 79368. This fix will first be available in v1.9.1.6.


Cheeso wrote May 26, 2010 at 2:47 PM

what do you mean "Fix the "bad signature" problem" . Do you have code that reproduces a problem? IS the code above a suggested fix?

Nanalich wrote May 31, 2010 at 10:51 AM

Poor English, sorry.
While using ZipInputStream with HttpPostedFileBase.InputStream, you will get "Bad Signature" Exception when calling GetNextEntry 2nd time. See code below.
    Stream stm = Request.Files["uploadfile"].InputStream;

    using (var zip = new ZipInputStream(stm, true))
      ZipEntry entity;
      while ((entity = zip.GetNextEntry()) != null)
        using (Stream file = entity.OpenReader())
        } // using (Stream file = entity.OpenReader())
      } // while ((entity = zip.GetNextEntry()) != null)
    } // using (var zip = new ZipInputStream(stm, true))

And the code I've posted before is a suggested fix.

TorstenR wrote Jun 17, 2010 at 4:50 PM

Think, Nanalich means the line "_inputStream.Seek(_endOfEntry, SeekOrigin.Begin); // fix code here" IS THE FIX.

wrote Jun 17, 2010 at 4:51 PM

wrote Jun 18, 2011 at 5:33 AM

Cheeso wrote Jun 18, 2011 at 5:37 AM

Ok, I've looked at this further and decided that the error is poor. However, I do think there should be an exception thrown by DotNetZip. You should not use OpenReader() to read a zip entry from a ZipInputStream. The ZipInputStream() programming model does not require it, and is actually simpler without OpenReader(). Check the doc for full details.

I will change DotNetZip to throw a more descriptive, appropriate exception in the future.

wrote Jun 18, 2011 at 5:40 AM

Nanalich wrote Jul 18, 2011 at 1:32 PM

The ZipInputStream uses ZipEntry, and ZipEntry has OpenReader.
Now you tell us we should use ZipInputStream.Read instead of the OpenReader way...
Sure this why is much simpler, but this model is so weird.

Nanalich wrote Jul 18, 2011 at 1:33 PM

I mean, this "way"...

wrote Feb 22, 2013 at 2:43 AM

wrote May 16, 2013 at 1:31 PM