Exemple #1
0
        public void GZipAccess_WriteRomResourceToGZipFile_ProducesExpectedResult(CompressedArchiveAccessImplementation implementation)
        {
            var gzipFileName = TemporaryFile.GenerateUniqueFilePath("tagalong", ".luigi.gz");

            using (TemporaryFile.CreateTemporaryFileWithPath(gzipFileName, createEmptyFile: false))
            {
                // Create on-disk GZIP
                var inputLength = 0L;
                var fileStream  = new FileStream(gzipFileName, FileMode.Create, FileAccess.Write);
                using (var gzip = CompressedArchiveAccess.Open(fileStream, CompressedArchiveFormat.GZip, CompressedArchiveAccessMode.Create, implementation))
                {
                    var testResourceName = "INTV.TestHelpers.Core.Resources.tagalong.luigi";
                    var newGZipEntryName = "tagalong.luigi";
                    var entry            = gzip.CreateEntry(newGZipEntryName);
                    using (var gzipStream = gzip.Open(entry.Name))
                        using (var sourceStream = typeof(TestRomResources).Assembly.GetManifestResourceStream(testResourceName))
                        {
                            sourceStream.CopyTo(gzipStream);
                            inputLength = sourceStream.Length;
                        }
                }

                // Now, see if we can extract it!
                var extractedRomPath = Path.Combine(Path.GetDirectoryName(gzipFileName), Path.GetFileNameWithoutExtension(gzipFileName));
                using (TemporaryFile.CreateTemporaryFileWithPath(extractedRomPath, createEmptyFile: false))
                {
                    var fileInfo = new FileInfo(gzipFileName);
                    Assert.True(fileInfo.Exists);
                    Assert.True(inputLength > fileInfo.Length); // Compressed we must be! On this, all depends.
                    using (var gzip = CompressedArchiveAccess.Open(gzipFileName, CompressedArchiveAccessMode.Read, implementation))
                    {
                        Assert.True(gzip.Entries.Any());
                        var entry = gzip.Entries.Single();
                        Assert.False(string.IsNullOrEmpty(entry.Name));
                        using (var outputFileStream = new FileStream(extractedRomPath, FileMode.Create, FileAccess.Write))
                            using (var gzipStream = gzip.OpenEntry(entry))
                            {
                                gzipStream.CopyTo(outputFileStream);
                            }
                    }

                    // Verify we have a valid LUIGI and it's got what we expect inside. Trust, but verify!
                    LuigiFileHeader header = null;
                    using (var outputFileStream = new FileStream(extractedRomPath, FileMode.Open, FileAccess.Read))
                    {
                        header = LuigiFileHeader.Inflate(outputFileStream);
                    }

                    Assert.NotNull(header);
                    Assert.Equal(RomFormat.Bin, header.OriginalRomFormat);
                    Assert.Equal(TestRomResources.TestBinCrc, header.OriginalRomCrc32);
                }
            }
        }
        public void ResourceHelpers_ExtractResourcesToFile_ProducesExpectedFile()
        {
            var testResource = TestResource.TextEmbeddedResourceFile;
            var type         = testResource.GetType();

            var expectedFilePath = Path.Combine(Path.GetTempPath(), "embedded resource file.txt");

            using (var temporaryFile = TemporaryFile.CreateTemporaryFileWithPath(expectedFilePath, createEmptyFile: false))
            {
                var extractedResourceFile = type.ExtractResourcesToFiles(new[] { testResource.Name }, TestResource.ResourcePrefix, Path.GetTempPath());

                Assert.True(File.Exists(expectedFilePath));
            }
        }
        private static void VerifyTestZipContents(string testZipFilePath, string zipTestEntryName, string zipTestEntryContent, CompressedArchiveAccessImplementation implementation)
        {
            using (var tempFile = TemporaryFile.CreateTemporaryFileWithPath(testZipFilePath, createEmptyFile: false))
                using (var zipArchive = CompressedArchiveAccess.Open(testZipFilePath, CompressedArchiveAccessMode.Read, implementation))
                {
                    var entries = zipArchive.Entries;
                    Assert.True(entries.Any(e => e.IsDirectory));

                    var fileEntryStream = zipArchive.Open(zipTestEntryName);
                    using (var streamReader = new StreamReader(fileEntryStream, Encoding.UTF8))
                    {
                        var entryContent = streamReader.ReadToEnd();
                        Assert.Equal(zipTestEntryContent, entryContent);
                    }
                }
        }
        public void ResourceHelpers_ExtractResourcesToExistingFilesWithCustomFileNames_ProducesExpectedFilePathssAndDoesNotChangeContents()
        {
            var testResources     = new[] { TestResource.TagalongZip, TestResource.TagalongDirZip };
            var type              = testResources[0].GetType();
            var expectedFilePaths = testResources.Select(r => Path.Combine(Path.GetTempPath(), GetFileNameForResource(r.Name, null))).ToArray();

            using (var firstFile = TemporaryFile.CreateTemporaryFileWithPath(expectedFilePaths[0], createEmptyFile: true))
                using (var secondFile = TemporaryFile.CreateTemporaryFileWithPath(expectedFilePaths[1], createEmptyFile: true))
                {
                    var extractedResourceFiles = type.ExtractResourcesToFiles(testResources.Select(r => r.Name), TestResource.ResourcePrefix, Path.GetTempPath(), GetFileNameForResource);

                    Assert.Equal(expectedFilePaths, extractedResourceFiles);
                    Assert.All(extractedResourceFiles, f => Assert.True(File.Exists(f)));
                    Assert.All(extractedResourceFiles, f => Assert.Equal(0L, new FileInfo(f).Length));
                }
        }
        public void ZipArchiveAccess_DeleteAnEntryFromExistingZipArchive_RemovesEntry(CompressedArchiveAccessImplementation implementation)
        {
            var numberOfEntriesToCreate   = 4;
            var zipTestEntryNameFormat    = "testEntry_{0}";
            var zipTestEntryContentFormat = "Here is some text to write for {0}!";
            var testZipFilePath           = TemporaryFile.GenerateUniqueFilePath("ZipTest", ".zip");
            var zipStream = new FileStream(testZipFilePath, FileMode.Create, FileAccess.ReadWrite); // SharpZipLib requires ReadWrite, .NET works with Write only.

            using (var zipArchive = CompressedArchiveAccess.Open(zipStream, CompressedArchiveFormat.Zip, CompressedArchiveAccessMode.Create, implementation))
            {
                for (var i = 0; i < numberOfEntriesToCreate; ++i)
                {
                    var entryName = string.Format(CultureInfo.CurrentCulture, zipTestEntryNameFormat, i);
                    var entry     = zipArchive.CreateEntry(entryName);
                    using (var zipEntryStream = zipArchive.OpenEntry(entry))
                    {
                        var zipTestEntryContent = string.Format(CultureInfo.CurrentCulture, zipTestEntryContentFormat, entryName);
                        var textToWrite         = Encoding.UTF8.GetBytes(zipTestEntryContent);
                        zipEntryStream.Write(textToWrite, 0, textToWrite.Length);
                    }
                }
            }
            VerifyNumberOfEntries(testZipFilePath, numberOfEntriesToCreate, implementation);

            var numberOfEntriesToDelete = 2;

            using (var zipArchive = CompressedArchiveAccess.Open(testZipFilePath, CompressedArchiveAccessMode.Update, implementation))
            {
                var entriesToDelete = zipArchive.Entries.Take(numberOfEntriesToDelete).Select(e => e.Name).ToList();
                foreach (var entry in entriesToDelete)
                {
                    zipArchive.DeleteEntry(entry);
                }
            }

            using (var tempFile = TemporaryFile.CreateTemporaryFileWithPath(testZipFilePath, createEmptyFile: false))
            {
                VerifyNumberOfEntries(testZipFilePath, numberOfEntriesToCreate - numberOfEntriesToDelete, implementation);
            }
        }