Remove trailing null characters produced by tar

老子叫甜甜 提交于 2019-12-10 15:45:39

问题


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.


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/11360891/remove-trailing-null-characters-produced-by-tar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!