Skip to content

Commit

Permalink
Merge pull request #593 from adamhathcock/fix-pkware-encryption
Browse files Browse the repository at this point in the history
ReadFully used by pkware encryption didn’t like spans
  • Loading branch information
adamhathcock committed Apr 25, 2021
2 parents 8a022c4 + c10bd84 commit 6474741
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/SharpCompress/Utility.cs
Expand Up @@ -280,7 +280,22 @@ private static byte[] GetTransferByteArray()
{
return ArrayPool<byte>.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<byte> buffer)
{
int total = 0;
Expand Down
28 changes: 28 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipReaderTests.cs
Expand Up @@ -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()
Expand Down
Binary file added tests/TestArchives/Archives/zipcrypto.zip
Binary file not shown.

0 comments on commit 6474741

Please sign in to comment.