Recognise empty tar archives.

Currently, when ArchiveFactory.Open is called on an empty tar archive, it throws due to being unable to determine the stream type. This fix allows it to recognise empty tar files by checking for whether the filename is empty, the size is empty and the entry type is defined. Add a test to try opening an empty archive.
This commit is contained in:
Elliot Prior 2018-08-16 09:44:08 +01:00 committed by Dominic Nancekievill
parent 9e96dec8c9
commit 83f6690576
3 changed files with 15 additions and 3 deletions

View file

@ -75,9 +75,10 @@ namespace SharpCompress.Archives.Tar
{
try
{
TarHeader tar = new TarHeader(new ArchiveEncoding());
tar.Read(new BinaryReader(stream));
return tar.Name.Length > 0 && Enum.IsDefined(typeof(EntryType), tar.EntryType);
TarHeader tarHeader = new TarHeader(new ArchiveEncoding());
bool readSucceeded = tarHeader.Read(new BinaryReader(stream));
bool isEmptyArchive = tarHeader.Name.Length == 0 && tarHeader.Size == 0 && Enum.IsDefined(typeof(EntryType), tarHeader.EntryType);
return readSucceeded || isEmptyArchive;
}
catch
{

View file

@ -155,5 +155,16 @@ namespace SharpCompress.Test.Tar
Assert.True(archive.Type == ArchiveType.Tar);
}
}
[Fact]
public void Tar_Empty_Archive()
{
string archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.Empty.tar");
using (Stream stream = File.OpenRead(archiveFullPath))
using (IArchive archive = ArchiveFactory.Open(stream))
{
Assert.True(archive.Type == ArchiveType.Tar);
}
}
}
}

Binary file not shown.