Fix tar corruption when sizes mismatch

This commit is contained in:
Adam Hathcock
2024-04-09 08:19:23 +01:00
parent 3612035894
commit bf74dd887a
2 changed files with 15 additions and 2 deletions

View File

@@ -269,8 +269,9 @@ public static class Utility
return sTime.AddSeconds(unixtime);
}
public static long TransferTo(this Stream source, Stream destination)
public static long TransferTo(this Stream source, Stream destination, Int64? size = null)
{
Int64 realSize = size ?? source.Length;
var array = GetTransferByteArray();
try
{
@@ -279,6 +280,18 @@ public static class Utility
{
total += count;
destination.Write(array, 0, count);
if (total + count > realSize)
{
int bytesToWrite = (int)(realSize - total);
destination.Write(array, 0, (int)bytesToWrite);
total = realSize;
break;
}
else
{
total += count;
destination.Write(array, 0, count);
}
}
return total;
}

View File

@@ -91,7 +91,7 @@ public class TarWriter : AbstractWriter
header.Size = realSize;
header.Write(OutputStream);
size = source.TransferTo(OutputStream);
size = source.TransferTo(OutputStream, realSize);
PadTo512(size.Value);
}