File Locking Issue

Jul 5, 2011 at 7:22 PM
I'm keep getting Compressed (zipped) Folders Error. 
Windows Cannot open the folder. Access to the Compresses(zipped) folder 'C:\x\xx\x123.zip' is denied.

I can open the zip folder if I exit out the application. How can I release the .zip file lock so that I don't have to exit out the application?

Any help!



using
 (var zipFile = new ZipFile()) {
zipFile.AddEntry(Path.Combine(zipPath, fileName), stream);
zipFile.Save(saveFileDialog.OpenFile());

}

Coordinator
Jul 5, 2011 at 8:43 PM
Edited Jul 5, 2011 at 8:44 PM

saveFileDialog.OpenFile() opens a stream.  Apparently your app does not call .Close() on that stream, and this is why the file cannot be opened by Windows.   If you modify your app to call .Close(), you'll be able to open the file externally, without exiting your app. 

You can (should) do it implicitly in a using clause. Something like this:

   SaveFileDialog saveFileDialog1 = new SaveFileDialog();
   saveFileDialog1.Filter = "ZIP File|*.zip";
   saveFileDialog1.Title = "Save a ZIP File";
   saveFileDialog1.ShowDialog();

   if(saveFileDialog1.FileName != "")
   {
      using (var stream2 = saveFileDialog1.OpenFile())
      {
          using (var zip = new ZipFile()) 
          {
             zip.AddEntry(Path.Combine(zipPath, fileName), stream1);
             zip.Save(stream2);
          }
      }
   }

An alternative which may be simpler for you is to NOT open the filestream using OpenFile(), and instead just reference the filename itself in the call to ZipFile.Save(). like this:

   SaveFileDialog saveFileDialog1 = new SaveFileDialog();
   saveFileDialog1.Filter = "ZIP file|*.zip";
   saveFileDialog1.Title = "Save a ZIP File";
   saveFileDialog1.ShowDialog();

   if(saveFileDialog1.FileName != "")
   {
          using (var zip = new ZipFile()) 
          {
             zip.AddEntry(Path.Combine(zipPath, fileName), stream1);
             zip.Save(saveFileDialog1.FileName);
          }      
   }


Jul 6, 2011 at 5:13 PM

Cheeso,

Thank you for your help. Your code snippet helped to fix the problem.