From 83f66905767ca50b82bd39efece602eb6d5e49cc Mon Sep 17 00:00:00 2001 From: Elliot Prior Date: Thu, 16 Aug 2018 09:44:08 +0100 Subject: [PATCH] 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. --- src/SharpCompress/Archives/Tar/TarArchive.cs | 7 ++++--- tests/SharpCompress.Test/Tar/TarArchiveTests.cs | 11 +++++++++++ tests/TestArchives/Archives/Tar.Empty.tar | Bin 0 -> 10240 bytes 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 tests/TestArchives/Archives/Tar.Empty.tar diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 961c1f6e..2ac7ce44 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -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 { diff --git a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs index 4e677abb..bf6946e9 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -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); + } + } } } diff --git a/tests/TestArchives/Archives/Tar.Empty.tar b/tests/TestArchives/Archives/Tar.Empty.tar new file mode 100644 index 0000000000000000000000000000000000000000..9df64990f7be3c1f7194a0c22852a1ab3a09f3c5 GIT binary patch literal 10240 zcmeIu0Sy2E0K%a6Pi+o2h(KY$fB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM P7%*VKfB^#r47?2tC;$Kf literal 0 HcmV?d00001