From 780be3fcb3975f6d175b4a68af892de2971b1c20 Mon Sep 17 00:00:00 2001 From: doug24 <17227248+doug24@users.noreply.github.com> Date: Fri, 29 Apr 2022 16:29:36 -0700 Subject: [PATCH 1/3] add libs to run unit tests in VS --- SevenZip.Tests/SevenZip.Tests.csproj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/SevenZip.Tests/SevenZip.Tests.csproj b/SevenZip.Tests/SevenZip.Tests.csproj index f8ace8a..84f15d9 100644 --- a/SevenZip.Tests/SevenZip.Tests.csproj +++ b/SevenZip.Tests/SevenZip.Tests.csproj @@ -18,15 +18,16 @@ true - - + + + - + From 6f257f4d9cfb3c279d70368c8f5a0b98113d6d24 Mon Sep 17 00:00:00 2001 From: doug24 <17227248+doug24@users.noreply.github.com> Date: Wed, 4 May 2022 17:58:41 -0700 Subject: [PATCH 2/3] Updates for solid archives: fix initialization for _isSolid remove index initialization to extract all preceding files in a solid archive remove check for solid archive in extract by callback add tests for modified Extract methods using solid and non-solid archives --- SevenZip.Tests/SevenZip.Tests.csproj | 18 +++ SevenZip.Tests/SevenZipExtractorTests.cs | 142 ++++++++++++++++-- SevenZip.Tests/TestData_solid/testFiles.7z | Bin 0 -> 225 bytes SevenZip.Tests/TestData_solid/testFiles.rar | Bin 0 -> 287 bytes .../TestData_solid/testFiles.solid.7z | Bin 0 -> 186 bytes .../TestData_solid/testFiles.solid.rar | Bin 0 -> 283 bytes SevenZip.Tests/TestData_solid/testFiles.tar | Bin 0 -> 6144 bytes SevenZip.Tests/TestData_solid/testFiles.zip | Bin 0 -> 537 bytes .../EventArguments/ExtractFileCallbackArgs.cs | 2 +- SevenZip/SevenZipExtractor.cs | 48 +----- 10 files changed, 150 insertions(+), 60 deletions(-) create mode 100644 SevenZip.Tests/TestData_solid/testFiles.7z create mode 100644 SevenZip.Tests/TestData_solid/testFiles.rar create mode 100644 SevenZip.Tests/TestData_solid/testFiles.solid.7z create mode 100644 SevenZip.Tests/TestData_solid/testFiles.solid.rar create mode 100644 SevenZip.Tests/TestData_solid/testFiles.tar create mode 100644 SevenZip.Tests/TestData_solid/testFiles.zip diff --git a/SevenZip.Tests/SevenZip.Tests.csproj b/SevenZip.Tests/SevenZip.Tests.csproj index 84f15d9..01d4d11 100644 --- a/SevenZip.Tests/SevenZip.Tests.csproj +++ b/SevenZip.Tests/SevenZip.Tests.csproj @@ -76,6 +76,24 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + diff --git a/SevenZip.Tests/SevenZipExtractorTests.cs b/SevenZip.Tests/SevenZipExtractorTests.cs index 94b7d78..6645a2e 100644 --- a/SevenZip.Tests/SevenZipExtractorTests.cs +++ b/SevenZip.Tests/SevenZipExtractorTests.cs @@ -32,6 +32,22 @@ public static List TestFiles return result; } } + /// + /// TestCaseSource for ReadSolidArchive test + /// + public static List SolidArchivesFiles + { + get + { + var result = new List(); + foreach (var file in Directory.GetFiles(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData_solid"))) + { + result.Add(new TestFile(file)); + } + + return result; + } + } [Test] public void ExtractFilesTest() @@ -85,7 +101,7 @@ public void ExtractionWithCancellationTest() e.Cancel = true; } }; - + tmp.ExtractArchive(OutputDirectory); Assert.AreEqual(2, Directory.GetFiles(OutputDirectory).Length); @@ -160,10 +176,10 @@ public void DetectMultiVolumeIndexTest() [Test] public void ThreadedExtractionTest() { - var destination1 = Path.Combine(OutputDirectory, "t1"); - var destination2 = Path.Combine(OutputDirectory, "t2"); + var destination1 = Path.Combine(OutputDirectory, "t1"); + var destination2 = Path.Combine(OutputDirectory, "t2"); - var t1 = new Thread(() => + var t1 = new Thread(() => { using (var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z")) { @@ -172,8 +188,8 @@ public void ThreadedExtractionTest() }); var t2 = new Thread(() => { - using (var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z")) - { + using (var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z")) + { tmp.ExtractArchive(destination2); } }); @@ -183,11 +199,11 @@ public void ThreadedExtractionTest() t1.Join(); t2.Join(); - Assert.IsTrue(Directory.Exists(destination1)); - Assert.IsTrue(Directory.Exists(destination2)); - Assert.AreEqual(3, Directory.GetFiles(destination1).Length); - Assert.AreEqual(3, Directory.GetFiles(destination2).Length); - } + Assert.IsTrue(Directory.Exists(destination1)); + Assert.IsTrue(Directory.Exists(destination2)); + Assert.AreEqual(3, Directory.GetFiles(destination1).Length); + Assert.AreEqual(3, Directory.GetFiles(destination2).Length); + } [Test] public void ExtractArchiveWithLongPath() @@ -211,7 +227,7 @@ public void ReadArchivedFileNames() Assert.AreEqual("file3.txt", fileNames[2]); } } - + [Test] public void ReadArchivedFileData() { @@ -235,6 +251,108 @@ public void ExtractDifferentFormatsTest(TestFile file) Assert.AreEqual(1, Directory.GetFiles(OutputDirectory).Length); } } + + [Test, TestCaseSource(nameof(SolidArchivesFiles))] + public void ReadStreamFromSolidArchiveTest(TestFile file) + { + using (var extractor = new SevenZipExtractor(file.FilePath)) + { + bool isSolid = Path.GetFileName(file.FilePath).ToLowerInvariant().Contains("solid"); + + var fileData = extractor.ArchiveFileData; + Assert.AreEqual(isSolid, extractor.IsSolid); + Assert.AreEqual(5, fileData.Count); + + // extract files in non-sequential order + foreach (int index in new int[] { 2, 4, 1, 3, 0 }) + { + string content = string.Empty; + using (var stream = new MemoryStream()) + { + extractor.ExtractFile(4, stream); + stream.Seek(0, SeekOrigin.Begin); + + using (TextReader reader = new StreamReader(stream)) + { + content = reader.ReadToEnd(); + } + } + Assert.AreEqual("test file", content); + } + } + } + + [Test, TestCaseSource(nameof(SolidArchivesFiles))] + public void ExtractSpecificFilesFromSolidArchiveTest(TestFile file) + { + using (var extractor = new SevenZipExtractor(file.FilePath)) + { + extractor.ExtractFiles(OutputDirectory, 2, 4); + Assert.AreEqual(2, Directory.GetFiles(OutputDirectory).Length); + } + + Assert.AreEqual(2, Directory.GetFiles(OutputDirectory).Length); + Assert.Contains(Path.Combine(OutputDirectory, "test3.txt"), Directory.GetFiles(OutputDirectory)); + Assert.Contains(Path.Combine(OutputDirectory, "test5.txt"), Directory.GetFiles(OutputDirectory)); + } + + [Test, TestCaseSource(nameof(SolidArchivesFiles))] + public void ExtractFileCallbackFromSolidArchiveTest(TestFile file) + { + using (var extractor = new SevenZipExtractor(file.FilePath)) + { + extractor.ExtractFiles(args => + { + if (args.Reason == ExtractFileCallbackReason.Start && + (args.ArchiveFileInfo.Index == 2 || args.ArchiveFileInfo.Index == 4)) + { + args.ExtractToFile = Path.Combine(OutputDirectory, args.ArchiveFileInfo.FileName); + } + }); + } + + Assert.AreEqual(2, Directory.GetFiles(OutputDirectory).Length); + Assert.Contains(Path.Combine(OutputDirectory, "test3.txt"), Directory.GetFiles(OutputDirectory)); + Assert.Contains(Path.Combine(OutputDirectory, "test5.txt"), Directory.GetFiles(OutputDirectory)); + } + + [Test, TestCaseSource(nameof(SolidArchivesFiles))] + public void ReadStreamCallbackFromSolidArchiveTest(TestFile file) + { + using (var extractor = new SevenZipExtractor(file.FilePath)) + { + extractor.ExtractFiles(args => + { + if (args.Reason == ExtractFileCallbackReason.Start) + { + if (args.ArchiveFileInfo.Index == 2 || args.ArchiveFileInfo.Index == 4) + { + args.ExtractToStream = new MemoryStream(); + } + } + else if (args.Reason == ExtractFileCallbackReason.Done) + { + if (args.ExtractToStream != null) + { + string content = string.Empty; + args.ExtractToStream.Seek(0, SeekOrigin.Begin); + using (TextReader reader = new StreamReader(args.ExtractToStream)) + { + content = reader.ReadToEnd(); + } + Assert.AreEqual("test file", content); + + args.ExtractToStream.Dispose(); + args.ExtractToStream = null; + } + } + else + { + Assert.Fail("Extract file failed " + args.Exception?.Message); + } + }); + } + } } /// diff --git a/SevenZip.Tests/TestData_solid/testFiles.7z b/SevenZip.Tests/TestData_solid/testFiles.7z new file mode 100644 index 0000000000000000000000000000000000000000..d2fe417dc4e8ac64db42dabcb52b5e96f613a523 GIT binary patch literal 225 zcmXr7+Ou9=hJhtQ&3eNE1_)4u(!ObPwHX;WN>YnU6w)$tQW?nMFfcS4v#;x$G<{xV z?{4mkkEZFoeE#uV;iJdDt0pe!dh~dr!2!+e>CX!eaB{MV#&~VY*8Alr;J!`7 literal 0 HcmV?d00001 diff --git a/SevenZip.Tests/TestData_solid/testFiles.rar b/SevenZip.Tests/TestData_solid/testFiles.rar new file mode 100644 index 0000000000000000000000000000000000000000..7aded5a49c998040829dd4c5ab264b403205ae44 GIT binary patch literal 287 zcmWGaEK-zWXJq*Nu<127BP%-t8zW;wLj!}Agrl1(6Ek-w14}1^!olp6PYnzVoF%En zC5Cz>6(wBEOn;^vKH(XEgApXAkd~Q~dSXpyK6WiexU@vtK0JtBi!m-OI!7gJuxl~F YrDbnO!6fWjOmS(EEe{K1W@TXj09BG<5C8xG literal 0 HcmV?d00001 diff --git a/SevenZip.Tests/TestData_solid/testFiles.solid.7z b/SevenZip.Tests/TestData_solid/testFiles.solid.7z new file mode 100644 index 0000000000000000000000000000000000000000..d3c78c495319acbe13066b3875db50f3ff752729 GIT binary patch literal 186 zcmXr7+Ou9=hJoeW;)d;&3=p6QrDq!yHa}p{VGxLAu#)sTC-dzD!+N`Shm6*Ma45*RU8kO>`CWKlvsw>}ER{9ZS&VI>tHs$Q7&#Le f*tr=Q7?qhB8Chc)1Q-~28eVZRo);4EV_*OPf$Kqh literal 0 HcmV?d00001 diff --git a/SevenZip.Tests/TestData_solid/testFiles.solid.rar b/SevenZip.Tests/TestData_solid/testFiles.solid.rar new file mode 100644 index 0000000000000000000000000000000000000000..a3820c5ae110839328fce23e7a04373da70cc84a GIT binary patch literal 283 zcmWGaEK-zWXJp`fe<3TndandP`}`>@y+7Z+Pm*Km>Ntj2N}@< zH{t-)2qQEjT;v%QmMr{z6T50-EUGU?GO1u!ZGuI$!-=de?5a(%sFp1c3uIB{xR!zXJ?XmnyV~<}l)QFswX!4s;q_kTTm2fJJqdXJHKYFx z{+}83oZ){@y7}++{98u<1^!2L5B!gyoVSMOFB$zG;eSNe!2bx!d24w7V@Cfc_#e?V Q@IQhwYJdO;fIvP1JA_AI*8l(j literal 0 HcmV?d00001 diff --git a/SevenZip.Tests/TestData_solid/testFiles.zip b/SevenZip.Tests/TestData_solid/testFiles.zip new file mode 100644 index 0000000000000000000000000000000000000000..7c3802ef915ac6a0ef7d04c3be1309f1812b7ebb GIT binary patch literal 537 zcmWIWW@Zs#0D-jj#UTf?Q$BG5*)Uv^T3lkNS5i>|Vko3#=A;G?RBuG0dSepRn~@IkwF1O0Ri0q0p7%FGk|IX2Ltii5}?|^p+UU12~cg|Kp|e+0jM@` X*aUd9vH|%FtUxHt$iVOc#9;sc7$R*t literal 0 HcmV?d00001 diff --git a/SevenZip/EventArguments/ExtractFileCallbackArgs.cs b/SevenZip/EventArguments/ExtractFileCallbackArgs.cs index 212c811..ace2fa9 100644 --- a/SevenZip/EventArguments/ExtractFileCallbackArgs.cs +++ b/SevenZip/EventArguments/ExtractFileCallbackArgs.cs @@ -89,7 +89,7 @@ public Stream ExtractToStream get => _extractToStream; set { - if (_extractToStream != null && !_extractToStream.CanWrite) + if (value != null && !value.CanWrite) { throw new ExtractionFailedException("The specified stream is not writable!"); } diff --git a/SevenZip/SevenZipExtractor.cs b/SevenZip/SevenZipExtractor.cs index 9b32f6e..2bf66d5 100644 --- a/SevenZip/SevenZipExtractor.cs +++ b/SevenZip/SevenZipExtractor.cs @@ -574,14 +574,9 @@ private void GetArchiveInfo(bool disposeStream) _archiveProperties = new ReadOnlyCollection(archProps); - if (!_isSolid.HasValue && _format == InArchiveFormat.Zip) - { - _isSolid = false; - } - if (!_isSolid.HasValue) { - _isSolid = true; + _isSolid = false; } #endregion @@ -618,31 +613,6 @@ private void InitArchiveFileData(bool disposeStream) } } - /// - /// Produces an array of indexes from 0 to the maximum value in the specified array - /// - /// The source array - /// The array of indexes from 0 to the maximum value in the specified array - private static uint[] SolidIndexes(uint[] indexes) - { - var max = indexes.Aggregate(0, (current, i) => Math.Max(current, (int) i)); - - if (max > 0) - { - max++; - var res = new uint[max]; - - for (var i = 0; i < max; i++) - { - res[i] = (uint)i; - } - - return res; - } - - return indexes; - } - /// /// Checks whether all the indexes are valid. /// @@ -1085,12 +1055,6 @@ public void ExtractFile(int index, Stream stream) try { var indexes = new[] { (uint)index }; - var entry = _archiveFileData[index]; - - if (_isSolid.Value && !entry.Method.Equals("Copy", StringComparison.InvariantCultureIgnoreCase)) - { - indexes = SolidIndexes(indexes); - } using (var aec = GetArchiveExtractCallback(stream, (uint) index, indexes.Length)) { @@ -1163,11 +1127,6 @@ public void ExtractFiles(string directory, params int[] indexes) origIndexes.Sort(); uindexes = origIndexes.ToArray(); - if (_isSolid.Value) - { - uindexes = SolidIndexes(uindexes); - } - #endregion try @@ -1272,11 +1231,6 @@ public void ExtractFiles(ExtractFileCallback extractFileCallback) DisposedCheck(); InitArchiveFileData(false); - if (IsSolid) - { - throw new SevenZipExtractionFailedException("Solid archives are not supported."); - } - foreach (var archiveFileInfo in ArchiveFileData) { var extractFileCallbackArgs = new ExtractFileCallbackArgs(archiveFileInfo); From c151288bb99f29d9917ca7cb4fd052ae1479caf2 Mon Sep 17 00:00:00 2001 From: doug24 <17227248+doug24@users.noreply.github.com> Date: Sat, 7 May 2022 17:27:07 -0700 Subject: [PATCH 3/3] fix index on extract test --- SevenZip.Tests/SevenZipExtractorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SevenZip.Tests/SevenZipExtractorTests.cs b/SevenZip.Tests/SevenZipExtractorTests.cs index 6645a2e..0a4ae07 100644 --- a/SevenZip.Tests/SevenZipExtractorTests.cs +++ b/SevenZip.Tests/SevenZipExtractorTests.cs @@ -269,7 +269,7 @@ public void ReadStreamFromSolidArchiveTest(TestFile file) string content = string.Empty; using (var stream = new MemoryStream()) { - extractor.ExtractFile(4, stream); + extractor.ExtractFile(index, stream); stream.Seek(0, SeekOrigin.Begin); using (TextReader reader = new StreamReader(stream))