/// <summary> /// Retrieve a Package for usage. This method will create the Package if it does not exist yet or open it if it exists /// </summary> /// <param name="packagePath">Path to the Package to initialize</param> private async Task <ZipArchive> RetrievePackageAsync(string packagePath) { var semaphore = LockingDictionary.GetOrAdd(packagePath, new SemaphoreSlim(1)); try { await semaphore.WaitAsync(); if (PackageDictionary.ContainsKey(packagePath)) { return(PackageDictionary[packagePath]); } if (!File.Exists(packagePath)) { var createdZip = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Create); createdZip.Dispose(); } var tmpPackage = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Update); PackageDictionary.Add(packagePath, tmpPackage); return(tmpPackage); } finally { semaphore.Release(); } }
/// <summary> /// Flush (Dispose) the <see cref="ZipArchive"/> in <see cref="PackageDictionary"/> and reconnect to it /// to force data to be persisted to disk after an operation. /// Flush will only occur if the pacakge is in the <see cref="PackageDictionary"/> and <see cref="AutoFlush"/> is set to true. /// <remarks> /// This method does not lock use the Semaphore from the <see cref="LockingDictionary"/> so it must be called when the archive is already locked /// </remarks> /// </summary> /// <param name="packagePath">Package path used to retrieve it from the Dictionary</param> private void AutoFlushPackage(string packagePath) { if (!AutoFlush || !PackageDictionary.ContainsKey(packagePath)) { return; } PackageDictionary[packagePath].Dispose(); PackageDictionary[packagePath] = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Update); }