diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs index e464b89bae9211..8a4ee54bac4f9f 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs @@ -439,11 +439,9 @@ internal async Task WriteAsGnuAsync(Stream archiveStream, Memory buffer, C private static MemoryStream GetLongMetadataStream(string text) { - MemoryStream data = new MemoryStream(); - data.Write(Encoding.UTF8.GetBytes(text)); - data.WriteByte(0); // Add a null terminator at the end of the string, _size will be calculated later - data.Position = 0; - return data; + byte[] arr = new byte[Encoding.UTF8.GetByteCount(text) + 1]; // +1 for null terminator + Encoding.UTF8.GetBytes(text, arr); + return new MemoryStream(arr); } private TarHeader GetGnuLongLinkMetadataHeader()