public void TestGetCompressed() { string realPath = Path.Combine(TestConstants.TestAssetPath, "TestZip.ZiP"); string fakePath = Path.Combine(TestConstants.TestAssetPath, "TestZip.lol"); var compressed = CompressedHelper.GetCompressed(new FileInfo(realPath)); Assert.IsTrue(compressed is ZipCompressed); compressed = CompressedHelper.GetCompressed(new FileInfo(fakePath)); Assert.IsNull(compressed); compressed = CompressedHelper.GetCompressed(new FileInfo(fakePath), true); Assert.IsTrue(compressed is DefaultCompressed); }
public async Task <T> Import(FileInfo archive, bool deleteOnImport = true, TaskListener <T> listener = null) { if (archive == null) { throw new ArgumentNullException(nameof(archive)); } if (!archive.Exists) { throw new FileNotFoundException($"File at ({archive.FullName}) does not exist!"); } // Retrieve the compressed file representation of the archive. var compressed = CompressedHelper.GetCompressed(archive); if (compressed == null) { throw new NotImportableException(archive, GetType()); } // Start extraction of archive. var extractedDir = await compressed.Uncompress(GetTempExtractDir(archive), listener?.CreateSubListener <DirectoryInfo>()); if (!extractedDir.Exists) { throw new NotImportableException(archive, GetType()); } // Parse the data at temporary extraction destination. var data = ParseData(extractedDir); // Failed to parse. if (data == null) { listener?.SetFinished(); return(default(T)); } // Calculate hash code. data.CalculateHash(); // Check whether this data already exists using hash check. bool isNewData = false; if (ContainsHash(data.HashCode, out T existingData)) { // Replace existing data. PostProcessData(data, existingData.Id); } else { // Allocate a new Id. PostProcessData(data, Guid.NewGuid()); isNewData = true; } // Move the extracted data under management of the storage. storage.Move(data.Id.ToString(), extractedDir); // Replace or add the data to database. database.Edit().Write(data).Commit(); // Delete archive if (deleteOnImport) { archive.Delete(); } // Report finished. listener?.SetValue(data); listener?.SetFinished(); if (isNewData) { OnNewData?.Invoke(data); } return(data); }