Event Handler not Fired after Save

Sep 14, 2010 at 6:33 PM
Edited Sep 14, 2010 at 9:07 PM

Hi,

I have a small problem, which might me a stupid mistake on my side.

Here is my code to create a zipfile when needed and the method to add a file to the archive.

Adding a file works without problem but for some reason the Event is not fired after saving.

I set a breakpoint at the zipFile_SaveProgress, the event is not fired.

        
class CoverArchive
    {
        private ZipFile zipFile;
        private String coverArchivePath;

        public CoverArchive()
        {        
            coverArchivePath = "Archive\\Covers";

            if (!File.Exists("Archive\\Covers"))
            {
                CreateZipFile();
            }
            using (zipFile = ZipFile.Read(coverArchivePath))
            {
                //zipFile.AddProgress += zipFile_AddProgress;
                //zipFile.ExtractProgress += zipFile_ExtractProgress;
                //zipFile.ZipError += zipFile_ZipError;
                zipFile.SaveProgress += zipFile_SaveProgress;
            }
        }

        private void CreateZipFile()
        {
            zipFile = new ZipFile();
            zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zipFile.UseUnicodeAsNecessary = true;
            if (!Directory.Exists("Archive"))
                Directory.CreateDirectory("Archive");
            zipFile.Save(coverArchivePath);
        }

        public void AddCover(List<String> directories, String coverName, Stream fileStream)
        {
            try
            {
                using (zipFile = ZipFile.Read(coverArchivePath))
                {
                    String filePath = createPath(directories, coverName);
                    ZipEntry entry = zipFile.AddEntry(filePath, fileStream);
                    zipFile.Save();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error adding File" + ex);
            }
        }
        private void zipFile_SaveProgress(Object sender, SaveProgressEventArgs e)
        {
            if (e.EventType == ZipProgressEventType.Saving_Completed)
                Console.WriteLine("Save Done");
        }
}


Thank a lot in advance :)

EDIT:

Problem solved, I deleted the using statement and it worked :)
Admin can delete this entry ;)

 

 

Coordinator
Sep 19, 2010 at 5:38 PM

The problem is with the using statement, but the solution is NOT to remove the using statement. 

The problem is you have called zipfile.Save(), which happens in your CreateZipFile() method,  before attaching the progress event to the zipfile object, which happens later in the CoverArchive() method. 

I'd suggest refactoring your code to put the using() statement outside all references to the zipfile object.  Do not remove the using() statement; that will result in other problems.