Problems updating ZIP file

Nov 14, 2011 at 2:13 PM

Was using 1.9.1.5, kept getting a message indicating "file in use by another process".  Upgraded to 1.9.1.8, now getting FileNotFoundException.  Basically given a file \\UNCPath\filename.xml and a file \\UNCPath\filename.xml.zip containg a previous version of \\UNCPath\filename.xml entered with the code I will provide, when I produce an new \\UNCPath\filename.xml, I want to put it into the \\UNCPath\filename.xml.zip.

Here's the code.  The bold area is where I'm doing the Add/Update, and the "Save" is where I am running into the exceptions.  Interestingly, the filename showing up in my FileNotFoundException is VisualStudioDebugPath\filename.xml

        public static FileInfo WriteToZipFile(IEnumerable<FileInfo> filesToZip, FileInfo zipFileInfo, bool moveToZip)
        {
           
            if (!zipFileInfo.Directory.Exists)
                Directory.CreateDirectory(zipFileInfo.DirectoryName);

            if (zipFileInfo.Exists && !ZipFile.IsZipFile(zipFileInfo.FullName))
                throw new Exception(String.Format("File '{0}' exists, but is not a Zip File.", zipFileInfo.FullName));

            using (ZipFile zipFile = (zipFileInfo.Exists) ? ZipFile.Read(zipFileInfo.FullName) : new ZipFile(zipFileInfo.FullName))
            {
                zipFile.TempFileFolder = zipFileInfo.DirectoryName;
                foreach (FileInfo fileToZip in filesToZip)
                {
                    if (fileToZip.Exists)
                    {
                        string zipPathEntryName = "";

                        if (fileToZip.DirectoryName != zipFileInfo.DirectoryName)
                        {
                            if (fileToZip.DirectoryName.Contains(zipFileInfo.DirectoryName))  // file is in a subdirectory of the zip file
                                zipPathEntryName = fileToZip.DirectoryName.Replace(zipFileInfo.DirectoryName, @".");
                            else if (fileToZip.Directory.Root != zipFileInfo.Directory.Root) // file is not even in the same root as the zip file
                                zipPathEntryName = fileToZip.DirectoryName.Replace(fileToZip.Directory.Root.Name, "");
                            else
                            {
                                DirectoryInfo zipFileDirectory = zipFileInfo.Directory;
                                bool matchFound = false;
                                string zipPathBackTrace = "";
                                while (!matchFound && zipFileDirectory != zipFileInfo.Directory.Root)
                                {
                                    zipFileDirectory = zipFileDirectory.Parent;
                                    zipPathBackTrace += @"..\";
                                    if (fileToZip.DirectoryName.Contains(zipFileDirectory.FullName))
                                        matchFound = true;
                                }

                                if (!matchFound)
                                    zipPathEntryName = zipPathBackTrace + fileToZip.DirectoryName.Remove(0, fileToZip.Directory.Root.Name.Length);
                                else
                                    zipPathEntryName = zipPathBackTrace + fileToZip.DirectoryName.Remove(0, zipFileDirectory.FullName.Length);
                            }
                        }
                        else
                            zipPathEntryName = fileToZip.DirectoryName;

                        if (zipFile.ContainsEntry(fileToZip.Name))
                            zipFile.UpdateFile(fileToZip.Name, zipPathEntryName);
                        else if (zipFile.ContainsEntry(fileToZip.FullName))
                            zipFile.UpdateFile(fileToZip.FullName, zipPathEntryName);
                        else
                            zipFile.AddFile(fileToZip.FullName, zipPathEntryName);

                        zipFile.Save();

                        if (moveToZip)
                            File.Delete(fileToZip.FullName);
                    }
                }
                zipFile.Dispose();
            }

            return new FileInfo(zipFileInfo.FullName);
        }

Any insight would be appreciated.