You must call ZipFile.Save before calling any Extract method

Dec 2, 2009 at 6:07 PM

I'm just migrating some code from 1.6 to 1.8.4.28 and I'm mystified by the BadStateException "You must call ZipFile.Save before calling any Extract method".  So it is not possible to open a zip file readonly to extract a file?

 

Coordinator
Dec 2, 2009 at 6:42 PM

no, it is possible to open, read, and then extract from a zip file.

Normally it is done like this:

using (var zip = ZipFile.Read("myarchive.zip")) 
{
    zip["entry1.txt"].Extract("c:\\extractdir");
}

If you are asking about the meaning of the exception, not because you have encountered it, but because you are reading the source code and want to understand what it means, be aware that the exception in question is thrown only when the zipfile has never been saved, in other words when it is being created in memory.

Dec 2, 2009 at 8:12 PM
Edited Dec 2, 2009 at 8:13 PM

Thanks.  My root problem was reading the file, calling ZipFile.UpdateFile() on contained files (believing it was a no-op in this instance), neglecting to Save, and then trying to Extract, which results in the exception.

In conclusion, call Save before calling any Extract method on a new or updated ZipFile, as proven above.