Files locked after beeing added to ZIP file from FileStream

May 10, 2010 at 3:27 PM
Edited May 10, 2010 at 3:34 PM

Hello everybody,

I'm currently working on a little piece of software, written in C#, which automates some image manipulation tasks for me.

My work flow is like this:

First you use my GUI to open a folder, select all images you want to edit. After that my code puts all images in an array, does the image manipulation on each image, saves each image to a subfolder on my harddrive, loads all images from FileStream again(write and read is done on purpose) and adds them to a zip file. This zip file is then saved and uploaded to ftp. After all those tasks my program is supposed to delete the created subfolder including all edited images and the zip file in it.

Unfortunately whenever I try to delete the mentioned subfolder, I get an IOException because the images are still in use. After some tracing, I discovered that all files, which were added using the AddEntry(String entryName, Stream stream) command, were still locked and I couldn't delete them. Using AddFile(String path) didn't do the trick either. The files would still get locked.

Now I was wondering: How can I release those handles? It's really annoying, not being able to delete the directory the way I intend to. Maybe someone has an idea on how to solve this. Since I couldn't find a Dispose() command for the files, I had a little trouble so far, getting to my goal.

About my application itself: I'm using .NET 2.0. My program has a GUI which displays the status for the actions mentioned above. The tasks are grouped in three backgroundworker processes (image manipulation and resizing, zip file creation, ftp upload).

Alright, I guess that's about it. If you guys have any more questions, feel free to ask.

And last but not least, here is the part of my code, which works on the zip file.

//directory containing the unedited images
String path = "C:\\Pics\\";

//contains all edited images as FileInfo
ArrayList ResizedImages = new ArrayList();

//contains all unedited images as FileInfo
ArrayList Images = new ArrayList();

using (ZipFile zip = new ZipFile())
                {
                    Directory.SetCurrentDirectory(path);
                    zip.TempFileFolder = path;

                    //add images to zip
                    foreach (FileInfo f in ResizedImages)
                    {
                        FileStream fileStream = new FileStream(f.Name, FileMode.Open, FileAccess.Read);
                        zip.AddEntry(f.Name, fileStream);
                    }

                    //filename of zip file is created from filename of first image
                    String fileName = ((FileInfo)Images[0]).Name.Substring(0, ((FileInfo)Images[0]).Name.Length - 4);

                    //save zip, catch exception
try { zip.Save((fileName + ".zip")); } catch (Exception exc) { MessageBox.Show(exc.ToString()); } }

 

Coordinator
May 11, 2010 at 1:35 AM

Where does the delete occur? - the one that fails.  I don't see it.

Your situation sounds very straightforward.   Do you have a simple test case that reproduces the problem?

DotNetZip has an extensive test suite that includes a bunch of cases that are VERY similar to what you describe....except that the deletion of the directory works.
So... I'm gonna need a test case to illustrate this one.