Skip to content

Commit 1b661c9

Browse files
author
cyr
committed
Fixed bug where large (int32+ file size) adds an additional 512 bytes of padding in tar files.
1 parent 54fc26b commit 1b661c9

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

src/SharpCompress/Writers/Tar/TarWriter.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,15 @@ public void Write(string filename, Stream source, DateTime? modificationTime, lo
8787
header.Name = NormalizeFilename(filename);
8888
header.Size = realSize;
8989
header.Write(OutputStream);
90+
9091
size = source.TransferTo(OutputStream);
91-
PadTo512(size.Value, false);
92+
PadTo512(size.Value);
9293
}
9394

94-
private void PadTo512(long size, bool forceZeros)
95+
private void PadTo512(long size)
9596
{
96-
int zeros = (int)size % 512;
97-
if (zeros == 0 && !forceZeros)
98-
{
99-
return;
100-
}
101-
zeros = 512 - zeros;
97+
int zeros = unchecked((int)(((size + 511L) & ~511L) - size));
98+
10299
OutputStream.Write(stackalloc byte[zeros]);
103100
}
104101

@@ -108,8 +105,7 @@ protected override void Dispose(bool isDisposing)
108105
{
109106
if (finalizeArchiveOnClose)
110107
{
111-
PadTo512(0, true);
112-
PadTo512(0, true);
108+
OutputStream.Write(stackalloc byte[1024]);
113109
}
114110
switch (OutputStream)
115111
{

0 commit comments

Comments
 (0)