From c10bd840c5a96b327cc197c996829f2cce1fed8f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 25 Apr 2021 09:25:51 +0100 Subject: [PATCH] =?UTF-8?q?ReadFully=20used=20by=20pkware=20encryption=20d?= =?UTF-8?q?idn=E2=80=99t=20like=20spans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SharpCompress/Utility.cs | 17 ++++++++++- .../SharpCompress.Test/Zip/ZipReaderTests.cs | 28 ++++++++++++++++++ tests/TestArchives/Archives/zipcrypto.zip | Bin 0 -> 1176 bytes 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/TestArchives/Archives/zipcrypto.zip diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index ffebb1526..6972854d5 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -280,7 +280,22 @@ private static byte[] GetTransferByteArray() { return ArrayPool.Shared.Rent(81920); } - + + public static bool ReadFully(this Stream stream, byte[] buffer) + { + int total = 0; + int read; + while ((read = stream.Read(buffer, total, buffer.Length - total)) > 0) + { + total += read; + if (total >= buffer.Length) + { + return true; + } + } + return (total >= buffer.Length); + } + public static bool ReadFully(this Stream stream, Span buffer) { int total = 0; diff --git a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs index ba8b070e2..24fbe0024 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -268,6 +268,34 @@ public void Zip_Deflate_WinzipAES_Read() } VerifyFiles(); } + + [Fact] + public void Zip_Deflate_ZipCrypto_Read() + { + int count = 0; + using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip"))) + using (var reader = ZipReader.Open(stream, new ReaderOptions() + { + Password = "test" + })) + { + while (reader.MoveToNextEntry()) + { + if (!reader.Entry.IsDirectory) + { + Assert.Equal(CompressionType.None, reader.Entry.CompressionType); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, + new ExtractionOptions() + { + ExtractFullPath = true, + Overwrite = true + }); + count++; + } + } + } + Assert.Equal(8, count); + } [Fact] public void TestSharpCompressWithEmptyStream() diff --git a/tests/TestArchives/Archives/zipcrypto.zip b/tests/TestArchives/Archives/zipcrypto.zip new file mode 100644 index 0000000000000000000000000000000000000000..23d065d89fab699baba7bb710ed6da1d96eb9bb9 GIT binary patch literal 1176 zcmWIWW@Zs#U}Rumm{~eA=vCLAI3XaL4Tw2`IH@SVJWsErqU7$Ig2*oyLLQ%U?{eHX z>(YZM%L3pUHv=_lt3Q$v0BT?XVs;=-$xjDqdmi!Umgl^%=_kUAGwp15wIDPt0BW*( zxUm{pQ(ArnNRxH@l)0PixczUJ%sFIt%H9~EX$eqMYueKQWG7~o<`xu#w3!sHH02Fk zVmp2Hf?0<*A9vjH9HDU|P@{mgNt__iX{AvGXYa57t{v$OgOIy9wqa z8;qGckPTi8G#H*%kd1y4rQ8lO`jI{)FkpOSqcPJDve9>dM#EAQvdJ$j_e}$te2fEP z@_8W$AK7HgB!+CVFfe1n5*)J0k614DfJ{~vPM;2TJCu)XGG