Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing to seek empty zip files #611

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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