Skip to content

Commit

Permalink
Fixed bug where large (int32+ file size) adds an additional 512 bytes…
Browse files Browse the repository at this point in the history
… of padding in tar files.
  • Loading branch information
cyr committed Aug 27, 2021
1 parent 54fc26b commit 1b661c9
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/SharpCompress/Writers/Tar/TarWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ public void Write(string filename, Stream source, DateTime? modificationTime, lo
header.Name = NormalizeFilename(filename);
header.Size = realSize;
header.Write(OutputStream);

size = source.TransferTo(OutputStream);
PadTo512(size.Value, false);
PadTo512(size.Value);
}

private void PadTo512(long size, bool forceZeros)
private void PadTo512(long size)
{
int zeros = (int)size % 512;
if (zeros == 0 && !forceZeros)
{
return;
}
zeros = 512 - zeros;
int zeros = unchecked((int)(((size + 511L) & ~511L) - size));

OutputStream.Write(stackalloc byte[zeros]);
}

Expand All @@ -108,8 +105,7 @@ protected override void Dispose(bool isDisposing)
{
if (finalizeArchiveOnClose)
{
PadTo512(0, true);
PadTo512(0, true);
OutputStream.Write(stackalloc byte[1024]);
}
switch (OutputStream)
{
Expand Down

0 comments on commit 1b661c9

Please sign in to comment.