Zip File Remains Open After Extracting Contents

Nov 7, 2010 at 4:22 AM

I am trying to use the wonderful DotNetZip library to read a small compressed file. The file contains just one entry, and I am reading that entry (file) into a string. Works great, but I get a consistent issue: After the function executes, the original zip file remains open (it cannot be deleted since it is in use).
Is there any close() type of function to use after extracting, to ensure that the original file is closed / its handle released?
Thank in advance. (The code I use is below)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static bool DecompressZipToString(string fileURISource, ref string strDataDest, ref string errMsg)
        {
            try
            {
                ZipFile zip = ZipFile.Read(fileURISource);
                ZipEntry ze = zip.Entries.ElementAt(0);
                MemoryStream ms = new MemoryStream();
                ze.Extract(ms);
                ms.Flush();
                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                strDataDest = sr.ReadToEnd();
                ms.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                errMsg = "Error decompressing file: " + ex.Message;
                return false;
            }
            return true;
        }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Coordinator
Nov 7, 2010 at 2:30 PM

ZipFile is IDisposable.  You must use a using clause.

It would be good form to do that also for the MemoryStream, and the StreamReader, which are also IDisposable. That would be 3 distinct using clauses.

Nov 7, 2010 at 5:55 PM

Thank you, that did it.