views:

1241

answers:

4

I'm creating a zip file using the class FastZip from SharpZipLib and once I after I close the program, I cannot delete the file because:

"Cannot delete zip.zip: It is being used by another person or program. Close any programs that might be using the file and try again."

The code that is generating the file is simply this:

fZip.CreateEmptyDirectories = true;
fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);

I tried using:

            using (FastZip fZip = new FastZip())
            {
                try
                {
                    fZip.CreateEmptyDirectories = true;
                    fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);
                }
                catch (Exception)
                {
                }
            }

But it doesn't convert to iDisposable

+1  A: 

Perhaps antivirus is busy checking the file? If not, then get a program that can tell you which programs have files open.

You can look at:

Lasse V. Karlsen
Nope, turned off the antivirus and it still doesn't want me to delete it. The unlocker didn't help too.
LuRsT
SysInternals Process Explorer should also be able to tell you who owns the file. http://www.sysinternals.com
GalacticCowboy
Unlocker did it. I didn't see the delete select box at first.
LuRsT
A: 

I'm not familier with the FastZip API (so this is just a punt) but do you need to close the archive with something like fZip.Close();

I would have thought that closing the program would have done the same thing but just a guess.

DilbertDave
I though of that too, but the fastZip class doesn't have it :(
LuRsT
Does FastZip implement IDisposable? If so, did you use "using" or ".Dispose();"?
GalacticCowboy
A: 

Make sure you CLOSE the SharpZipLib stream after you're finished with it, you can do this by wrapping the construction of it inside a "using" statement...

using(Stream s = new SharpZipLibStream())
{
   /*...do stuff, zip stuff.../
}
Thomas Hansen
But, I'm not using a ShrapZipLibStream. I'm using fastzip.
LuRsT
+1  A: 

It looks like it may be a bug with SharpZipLib. The FastZip.CreateZip method has three overloads and two of them create use File.Create such as:

CreateZip(File.Create(zipFileName), // ...

It seems it does not close the stream, so you may be better off using:

string zipFileName = System.IO.Path.Combine(filesPath, this.zipName);
using (Stream stream = File.Create(zipFileName))
{
    fZip.CreateZip(stream, filesPath, false, this.zipFilter, null);
    stream.Close();
}

Technically you shouldn't need to call Close in the using block, but I like to be explicit about it.

Ryan
I used the code, but it gives the same error...I'm starting to think that the problem is the pc...
LuRsT