TarArchive fails to find subsequent entries if you open an entry stream during entry iteration #402

Open
opened 2026-01-29 22:11:19 +00:00 by claunia · 0 comments
Owner

Originally created by @DannyBoyk on GitHub (Jul 1, 2020).

We usually use TarReader, not TarArchive, so we hadn't seen this before. When using TarArchive (directly or from ArchiveFactory), if you open an entry stream while iterating over the entries, TarArchive fails to find the next header and silently indicates there are no more entries. The follow code will demonstrate this with any TAR archive of more than one file.

using (var streamReader = File.OpenRead(args[0]))
using (var archiveFactory = ArchiveFactory.Open(streamReader))
{
    foreach (var entry in archiveFactory.Entries)
    {
        if (entry.IsDirectory)
        {
            continue;
        }

        var outputPath = Path.Combine(@"D:\temp\tar", entry.Key);

        Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

        using (var tarEntryStream = entry.OpenEntryStream())
        using (var outputStream = File.OpenWrite(outputPath))
        {
            tarEntryStream.CopyTo(outputStream);
        }
    }
}

I stepped through the source and TarHeaderFactory.ReadHeader fails silently for the second entry in the archive. Without debugging further, it appears the archive is someone losing its position due to opening the entry stream. If you don't open the stream, all the entries are iterated over just fine.

Originally created by @DannyBoyk on GitHub (Jul 1, 2020). We usually use TarReader, not TarArchive, so we hadn't seen this before. When using TarArchive (directly or from ArchiveFactory), if you open an entry stream while iterating over the entries, TarArchive fails to find the next header and silently indicates there are no more entries. The follow code will demonstrate this with any TAR archive of more than one file. ``` using (var streamReader = File.OpenRead(args[0])) using (var archiveFactory = ArchiveFactory.Open(streamReader)) { foreach (var entry in archiveFactory.Entries) { if (entry.IsDirectory) { continue; } var outputPath = Path.Combine(@"D:\temp\tar", entry.Key); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); using (var tarEntryStream = entry.OpenEntryStream()) using (var outputStream = File.OpenWrite(outputPath)) { tarEntryStream.CopyTo(outputStream); } } } ``` I stepped through the source and `TarHeaderFactory.ReadHeader` fails silently for the second entry in the archive. Without debugging further, it appears the archive is someone losing its position due to opening the entry stream. If you don't open the stream, all the entries are iterated over just fine.
claunia added the bug label 2026-01-29 22:11:19 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#402