/// <summary>
        /// Delegates <c>WriteEndElement</c> calls when the element's prefix does not
        /// match a zip command.
        /// Otherwise, close the archive or flush the delegate writer.
        /// </summary>
        public override void WriteEndElement()
        {
            // check for pending pzip instructions
            if (_currentPzipElement != null && PZIP.Namespace.Equals(_currentPzipElement.Namespace))
            {
                processPackageInstructions(_currentPzipElement);
                _currentPzipElement = null;
            }

            Node elt = _elements.Pop();

            if (!elt.Namespace.Equals(PZIP.Namespace))
            {
                Debug.WriteLine("delegate - </" + elt.Name + ">");
                if (_delegateWriter != null)
                {
                    _delegateWriter.WriteEndElement();
                }
            }
            else
            {
                switch (elt.Name)
                {
                case PZIP.ArchiveElement:
                    if (_processingState == ProcessingState.EntryWaiting)
                    {
                        if (_zipOutputStream != null)
                        {
                            // Copy binaries before closing the archive
                            copyBinaries();
                            //Added by Sonata - Copy Audio files before closing archive
                            extractFiles();
                            importFiles();
                            Debug.WriteLine("[closing archive]");
                            _zipOutputStream.Close();
                            _zipOutputStream = null;
                        }
                        _processingState = ProcessingState.None;
                    }
                    break;

                case PZIP.EntryElement:
                    if (_processingState == ProcessingState.EntryStarted)
                    {
                        if (_delegateWriter != null)
                        {
                            Debug.WriteLine("[end part]");
                            _delegateWriter.WriteEndDocument();
                            _delegateWriter.Flush();
                            _delegateWriter.Close();
                            _delegateWriter = null;
                        }
                        _processingState = ProcessingState.EntryWaiting;
                    }
                    break;

                case PZIP.CopyElement:
                    break;

                case PZIP.ExtractElement:
                    break;

                case PZIP.ImportElement:
                    break;
                }
            }
        }
        private void processPackageInstructions(Element element)
        {
            switch (_currentPzipElement.Name)
            {
            case PZIP.ArchiveElement:
                // Prevent nested archive creation
                if (_processingState == ProcessingState.None)
                {
                    if (_currentPzipElement.Attributes.ContainsKey(PZIP.TargetAttr))
                    {
                        Debug.WriteLine("Creating archive : " + _currentPzipElement.Attributes[PZIP.TargetAttr].Value);
                        _zipOutputStream = ZipFactory.CreateArchive(_currentPzipElement.Attributes[PZIP.TargetAttr].Value);
                        _processingState = ProcessingState.EntryWaiting;

                        _binaries        = new Dictionary <string, string>();
                        _extractFileList = new Dictionary <string, string>();
                        _importFileList  = new Dictionary <string, string>();
                    }
                    else
                    {
                        //TODO throw exception
                    }
                }
                break;

            case PZIP.EntryElement:
                // Prevent nested entry creation
                if (_processingState == ProcessingState.EntryWaiting)
                {
                    if (_currentPzipElement.Attributes.ContainsKey(PZIP.TargetAttr))
                    {
                        Debug.WriteLine("creating new part : " + _currentPzipElement.Attributes[PZIP.TargetAttr].Value);
                        CompressionMethod compressionMethod = CompressionMethod.Deflated;
                        if (_currentPzipElement.Attributes.ContainsKey(PZIP.CompressionAttr) && _currentPzipElement.Attributes[PZIP.CompressionAttr].Value.Equals("none"))
                        {
                            compressionMethod = CompressionMethod.Stored;
                        }
                        _zipOutputStream.AddEntry(_currentPzipElement.Attributes["target"].Value, compressionMethod);

                        if (_currentPzipElement.Attributes.ContainsKey(PZIP.ContentTypeAttr) && _currentPzipElement.Attributes[PZIP.ContentTypeAttr].Value.Equals("text/plain") &&
                            _currentPzipElement.Attributes.ContainsKey(PZIP.ContentAttr))
                        {
                            Encoding enc    = new System.Text.ASCIIEncoding();
                            byte[]   buffer = enc.GetBytes(_currentPzipElement.Attributes[PZIP.ContentAttr].Value);
                            _zipOutputStream.Write(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            _delegateWriter = XmlWriter.Create(_zipOutputStream, _delegateSettings);
                            _delegateWriter.WriteStartDocument();

                            _processingState = ProcessingState.EntryStarted;
                        }
                    }
                }
                break;

            case PZIP.CopyElement:
                if (_processingState != ProcessingState.None)
                {
                    if (_currentPzipElement.Attributes.ContainsKey(PZIP.SourceAttr) &&
                        _currentPzipElement.Attributes.ContainsKey(PZIP.TargetAttr))
                    {
                        string source = _currentPzipElement.Attributes[PZIP.SourceAttr].Value;
                        string target = _currentPzipElement.Attributes[PZIP.TargetAttr].Value;

                        Debug.WriteLine(string.Format("copy source={0} target={1}", source, target));

                        if (_binaries != null && !_binaries.ContainsKey(target))
                        {
                            //target is the key because there are cases where one file has to be
                            //copied twice to different locations
                            _binaries.Add(target, source);
                        }
                    }
                }
                break;

            case PZIP.ExtractElement:
                if (_processingState != ProcessingState.None)
                {
                    if (_currentPzipElement.Attributes.ContainsKey(PZIP.SourceAttr) &&
                        _currentPzipElement.Attributes.ContainsKey(PZIP.TargetAttr))
                    {
                        string source = _currentPzipElement.Attributes[PZIP.SourceAttr].Value;
                        string target = _currentPzipElement.Attributes[PZIP.TargetAttr].Value;

                        Debug.WriteLine(string.Format("copy source={0} target={1}", source, target));

                        if (_extractFileList != null && !_extractFileList.ContainsKey(target))
                        {
                            //target is the key because there are cases where one file has to be
                            //copied twice to different locations
                            _extractFileList.Add(target, source);
                        }
                    }
                }
                break;

            case PZIP.ImportElement:
                if (_processingState != ProcessingState.None)
                {
                    if (_currentPzipElement.Attributes.ContainsKey(PZIP.SourceAttr) &&
                        _currentPzipElement.Attributes.ContainsKey(PZIP.TargetAttr))
                    {
                        string source = _currentPzipElement.Attributes[PZIP.SourceAttr].Value;
                        string target = _currentPzipElement.Attributes[PZIP.TargetAttr].Value;

                        Debug.WriteLine(string.Format("copy source={0} target={1}", source, target));

                        if (_importFileList != null && !_importFileList.ContainsKey(target))
                        {
                            //target is the key because there are cases where one file has to be
                            //copied twice to different locations
                            _importFileList.Add(target, source);
                        }
                    }
                }
                break;
            }
        }