mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-25 00:15:15 +00:00
Fix tar corruption when sizes mismatch
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user