diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index f7e0b17d..18ea1505 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -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; } diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index 2427db37..53ce0ea2 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -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); }