TAR file not working with stream - .NET7

Amit Singh Rawat 731 Reputation points
2023-07-10T09:48:34.2966667+00:00

Hi,

I am following below link from Microsoft about new TAR assembly added in .NET7.

https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-4/#added-new-tar-apis

I try to replicate example specified against "We also offer variants that allow extracting from a stream or archiving into a stream:"

But it is throwing error "System.IO.EndOfStreamException: 'Unable to read beyond the end of the stream.'"

Below is my code.

string sourceDirectoryName = "D:\TarFileDemo\FolderContainingFiles";

string destinationDirectoryName = "D:\TarFileDemo\ExtractedFiles";

MemoryStream archiveStream = new MemoryStream();

TarFile.CreateFromDirectory(sourceDirectoryName, archiveStream, true);

TarFile.ExtractToDirectory(archiveStream, destinationDirectoryName, true);

Please confirm what's wrong with this code.

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-07-10T13:36:24.8666667+00:00

    Try adding archiveStream.Position = 0 before extracting the files. (You can also use archiveStream.Seek).

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.