How to compute the size of a tar archive before you tar it

Suppose you have a tree of files and directories, and you want to pack them up into a tar archive. Here is how to find the size of the resulting archive. This is useful if you want to stream an archive out to a browser, but want to send the correct Content-Length header first.

  1. let F = number of files in the tree
  2. let D = number of directories in the tree, counting the root
  3. if there are any non-ordinary files, stop here. results undefined.
  4. let S = 0, H = 0
  5. for each f in F, S += ceil(size(f)/512.0)*512, H += 512 – that’s the size of f rounded up to 512-byte blocks
  6. for each d in D, H += 512
  7. for each p in (F D), if the full path and name of p (as stored in the archive) is greater than 100 characters, H += 1024
  8. The size of the archive is S + H + 1024 bytes of zero-fill at the end of the tar
  9. Round up to (tar blocking factor) * 512 byte records. GNU tar defaults to blocking factor 20. Blocking factor 1 makes the math easier ;)

No, you cannot use ‘du’. I have tried.

Update! Added #7.

Comments are closed.