From d506c555c928b21c5e1a8fd04f47c7a2c995eab6 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Fri, 19 Jun 2020 22:20:35 +0100 Subject: [PATCH] Merge PR #435: Add unit test for ZipFile.Add(string fileName, string entryName) --- .../Zip/ZipFileHandling.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index c79137130..996b09213 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -1569,5 +1569,47 @@ public void AddingAnAESEncryptedEntryShouldThrow() Assert.That(exception.Message, Is.EqualTo("Creation of AES encrypted entries is not supported")); } } + + /// + /// Test that we can add a file entry and set the name to sometihng other than the name of the file. + /// + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void AddFileWithAlternateName() + { + // Create a unique name that will be different from the file name + string fileName = Guid.NewGuid().ToString(); + + using (var sourceFile = Utils.GetDummyFile()) + using (var outputFile = Utils.GetDummyFile(0)) + { + var inputContent = File.ReadAllText(sourceFile.Filename); + using (ZipFile f = ZipFile.Create(outputFile.Filename)) + { + f.BeginUpdate(); + + // Add a file with the unique display name + f.Add(sourceFile.Filename, fileName); + + f.CommitUpdate(); + f.Close(); + } + + using (ZipFile zipFile = new ZipFile(outputFile.Filename)) + { + Assert.That(zipFile.Count, Is.EqualTo(1)); + + var fileEntry = zipFile.GetEntry(fileName); + Assert.That(fileEntry, Is.Not.Null); + + using (var sr = new StreamReader(zipFile.GetInputStream(fileEntry))) + { + var outputContent = sr.ReadToEnd(); + Assert.AreEqual(inputContent, outputContent, "extracted content does not match source content"); + } + } + } + } } }