5

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.

Julian
  • 8,808
  • 8
  • 51
  • 90
barsju
  • 4,408
  • 1
  • 19
  • 24
  • Can you upload the result of `tar czf - test > tar.redirect` and `tar czf tar.file test` somewhere? Also, what architecture are you running GNU tar 1.15.1 on? – phihag Jul 06 '12 at 11:09
  • 4
    *"For tar and cpio formats, the last block of output is padded to a full block size if the output is being written to standard output or to a character or block device such as a tape drive. If the output is being written to a regular file, the last block will not be padded."* (`man tar`) - Seems the answer is to write to a file... :) – deceze Jul 06 '12 at 11:16
  • Yupp, @deceze. I saw that as well.. Was hoping for a workaround that does not touch the file system.. Maybe a way to stop it from padding or to pipe it through sed or something to strip out the padding.. – barsju Jul 06 '12 at 11:43
  • @phihag I'm running on ARM. Was hoping to avoid touching the file system.. – barsju Jul 06 '12 at 11:44
  • 1
    Have you tried writing the file to a named pipe instead? – OmnipotentEntity Jul 06 '12 at 11:53
  • @OmnipotentEntity no. Sounds like fun.. I'll give it a shot. – barsju Jul 06 '12 at 12:23

2 Answers2

1

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

Giuseppe Scrivano
  • 1,385
  • 10
  • 13
1

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.:

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135