private async Task ImportingStaticMediaFiles()
        {
            PackageImportStateChangeEvent?.Invoke("Importing static media...");
            foreach (var entry in numToName)
            {
                try
                {
                    string file = entry.Value;
                    string c    = entry.Key;
                    if (!file.StartsWith("_") && !file.StartsWith("latex-"))
                    {
                        continue;
                    }

                    StorageFile path = await destCol.Media.MediaFolder.TryGetItemAsync(file) as StorageFile;

                    if (path != null)
                    {
                        await path.DeleteAsync();
                    }

                    Utils.UnZipNotFolderEntries(archive, destCol.Media.MediaFolder.Path, new string[] { c }, numToName);
                }
                catch (IOException)
                {
                    Debug.WriteLine("Failed to extract static media file. Ignoring.");
                }
            }
        }
        public async override Task Run()
        {
            tempDir = await destCol.Folder.CreateFolderAsync(tempDirName, CreationCollisionOption.ReplaceExisting);

            string collectionName     = "collection.anki2";
            string mapFileName        = "media";
            string mapAnkiU           = "mediaAnkiU";
            AnkiImportFinishCode code = AnkiImportFinishCode.UnableToUnzip;

            try
            {
                packageFile = await packageFile.CopyAsync(tempDir, packageFile.Name, NameCollisionOption.ReplaceExisting);

                using (var fileStream = (await packageFile.OpenReadAsync()).AsStream())
                    using (archive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        // We extract the zip contents into a temporary directory and do a little more
                        // validation than the python client to ensure the extracted collection is an apkg.
                        try
                        {
                            PackageImportStateChangeEvent?.Invoke("Extracting package...");
                            Utils.UnZipNotFolderEntries(archive, tempDir.Path, new string[] { collectionName, mapFileName, mapAnkiU }, null);

                            //Point the sourcefolder and relative path to extracted files
                            sourceFolder       = tempDir;
                            relativePathToFile = collectionName;
                        }
                        catch (IOException e)
                        {
                            code = AnkiImportFinishCode.UnableToUnzip;
                            ThrowDebugException(AnkiImportFinishCode.UnableToUnzip, e.Message);
                            return;
                        }
                        string      colpath = "collection.anki2";
                        StorageFile colFile = await tempDir.TryGetItemAsync("collection.anki2") as StorageFile;

                        if (colFile == null)
                        {
                            code = AnkiImportFinishCode.NotFoundCollection;
                            ThrowDebugException(AnkiImportFinishCode.NotFoundCollection);
                            return;
                        }
                        // we need the media dict in advance, and we'll need a map of fname ->
                        // number to use during the import
                        PackageImportStateChangeEvent?.Invoke("Create mapping file...");
                        numToName = new Dictionary <string, string>();
                        using (Collection col = await Storage.OpenOrCreateCollection(tempDir, colpath))
                        {
                            try
                            {
                                GetAnkiMediaMapping(numToName);
                                await GetAnkiUMediaMappingIfNeeded();
                            }
                            catch (FileNotFoundException)
                            {
                                code = AnkiImportFinishCode.NotFoundMediaFile;
                                ThrowDebugException(AnkiImportFinishCode.NotFoundMediaFile);
                                return;
                            }
                            catch (IOException)
                            {
                                code = AnkiImportFinishCode.MediaFileIsCorrupted;
                                ThrowDebugException(AnkiImportFinishCode.MediaFileIsCorrupted);
                                return;
                            }
                            catch (Exception)
                            {
                                code = AnkiImportFinishCode.UnknownExpception;
                                return;
                            }
                        }
                        try
                        {
                            // run anki2 importer
                            await base.Run();

                            //WARNING: AnkiU does not support static media and Latex
                            //import static media
                            await ImportingStaticMediaFiles();

                            destCol.Database.Commit();
                            code = AnkiImportFinishCode.Success;

                            PackageImportStateChangeEvent?.Invoke("Importing media...");

                            //Only in AnkiU we perform this step to move all mediafiles into DeckIdFolder
                            await ExtractSourceMediaFileToAllDeckIdFolderAsync();
                        }
                        catch (AnkiImportException e)
                        {
                            code = e.Error;
                        }
                        catch (Exception)
                        {
                            code = AnkiImportFinishCode.UnknownExpception;
                        }
                    }
            }
            finally
            {
                if (tempDir == null)
                {
                    // Clean up our temporary files
                    tempDir = await destCol.Folder.TryGetItemAsync(tempDirName) as StorageFolder;
                }

                if (tempDir != null)
                {
                    if (sourceCol != null)
                    {
                        sourceCol.Close(false);
                        sourceCol = null;
                    }
                    sourceFolder = null;
                    await tempDir.DeleteAsync();

                    tempDir = null;
                }
                AnkiPackageImporterFinishedEvent?.Invoke(code, ImportedNoteId.Count.ToString());
            }
        }
 private void ImporterStateChangeEventHandler(string message)
 {
     PackageImportStateChangeEvent?.Invoke(message);
 }