Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing null characters produced by tar

Tags:

linux

tar

I'm trying tar up some files and pass them along to the user through the php passthru command.

The problem is that even though the tar file should only be like 2k it is always 10240. Funny number right?

So I have broken it down to:

-sh-4.1# tar czf -  test | wc -c
10240

VS:

-sh-4.1# tar czf test.tar.gz test && wc -c test.tar.gz
2052 test.tar.gz

So tar is clearly padding out the file with NULL.

So how can I make tar stop doing that. Alternatively, how can I strip the trailing NULLs.

I'm running on tar (GNU tar) 1.15.1 and cannot reproduce on my workstation which is tar (GNU tar) 1.23, and since this is an embedded project upgrading is not the answer I'm looking for (yet).

Edit: I am hoping for a workaround that does need to write to the file system.. Maybe a way to stop it from padding or to pipe it through sed or something to strip out the padding.

like image 983
barsju Avatar asked Nov 03 '22 20:11

barsju


2 Answers

you can attenuate the padding effect by using a smaller block size, try to pass -b1 to tar

like image 164
Giuseppe Scrivano Avatar answered Nov 07 '22 21:11

Giuseppe Scrivano


You can minimise the padding by setting the block size to the minimum possible value - on my system this is 512.

$ cat test
a few bytes
$ tar -c test | wc -c
10240
$ tar -b 1 -c test | wc -c
2048
$ tar --record-size=512 -c test | wc -c
2048
$

This keeps the padding to at most 511 bytes. Short of piping through a program to remove the padding, rewrite the block header, and recreate the end-of-archive signature, I think this is the best you can do. At that point you might consider using a scripting language and it's native tar implementation directly, e.g.:

  • PHP's PharData (http://php.net/manual/en/class.phardata.php)
  • Perl's Archive::Tar (https://perldoc.perl.org/Archive/Tar.html)
  • Python's tarfile (https://docs.python.org/2/library/tarfile.html)
like image 28
GarethHumphriesAcc Avatar answered Nov 07 '22 22:11

GarethHumphriesAcc