Skip to content

Commit

Permalink
Merge pull request #611 from Thunderstr1k3/fix-zipheader-seeking
Browse files Browse the repository at this point in the history
Allowing to seek empty zip files
  • Loading branch information
adamhathcock committed Sep 12, 2021
2 parents 54fc26b + 3e32e3d commit b425659
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs
Expand Up @@ -117,7 +117,8 @@ private static void SeekBackToHeader(Stream stream, BinaryReader reader)
// Search in reverse
Array.Reverse(seek);

var max_search_area = len - MINIMUM_EOCD_LENGTH;
// don't exclude the minimum eocd region, otherwise you fail to locate the header in empty zip files
var max_search_area = len; // - MINIMUM_EOCD_LENGTH;

for( int pos_from_end = 0; pos_from_end < max_search_area; ++pos_from_end)
{
Expand Down
22 changes: 22 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Expand Up @@ -290,6 +290,28 @@ public void Zip_Create_New()
Directory.Delete(SCRATCH_FILES_PATH, true);
}

/// <summary>
/// Creates an empty zip file and attempts to read it right afterwards.
/// Ensures that parsing file headers works even in that case
/// </summary>
[Fact]
public void Zip_Create_Empty_And_Read()
{
var archive = ZipArchive.Create();

var archiveStream = new MemoryStream();

archive.SaveTo(archiveStream, CompressionType.LZMA);

archiveStream.Position = 0;

var readArchive = ArchiveFactory.Open(archiveStream);

var count = readArchive.Entries.Count();

Assert.Equal(0, count);
}

[Fact]
public void Zip_Create_New_Add_Remove()
{
Expand Down

0 comments on commit b425659

Please sign in to comment.